Share with others

Assignment No 08


AIM:


A) Assignment to design form taking all student details and storing in a database using Servlets and JSP.

B) Assignment to display all stored data in above tables in JSP.


THEORY:


Java Servlets

Servlets are server side applets that are loaded and executed by a web server in the same manner that applets are loaded and executed by a web browser. Thus Servlets are useful to create Dynamic pages. Depending upon my input server will an output.


The advantages of using Servlets are

1)    Servlets are persistent. Servlets are loaded only once by the web server and can maintain services between requests while CGI scripts must be loaded and executed by the web server each time a request is made to it.

2)    Servlets are fast.

3)    Servlets are platform independent as they are written in java.

4)    Servlets are secure. The only way to gain access to a Servlets is through a server. If the server is protected, the Servlets is protected as well.


                                
Steps:

1)    The client (web browser) makes a request via an HTTP. The web server receives the request and sends it to the Servlets. If the Servlets is not loaded, the web server will load it into the Java virtual machine and execute it.

2)    The Servlets will receive the HTTP request and perform tasks.

3)    The Servlets will return a response to the web server.

4)    The web server will forward the response to the client.


The Java Servlets Development Kit (JSDK) is used to write Servlets. The Java Web server (JWS) from Java Soft is used to run Servlets. GET methods is not suitable to send large information, as some servers limit the length of URLs and Query strings to about 240 characters. The POST method is suitable to send large information. HEAD, DELETE, PUT, TRACE and OPTIONS are other methods used to send information.


Server documentation: JwsàDocàenà apidocàIndex.html

To start the server: binàhttpd

To close the server: c: =JavaWebServer2.0bin> jsadmin –shutdown


By default the Web server administration tool is installed on port 9090. The administration controls available for the HTTP Web service are Setup, Monitor, Security and servlets.


Life cycle:

Two types of Servlets are present they are generic Servlets and HttpServlets. HttpServlet is derived form GenericServlet .


Generic Servlets:  init( ) à Service( )  à destoy( )

Http Servlets: init( )   à doGet( ) or doPost( )àdestoy( )


A Servlets init (servletConfig), method is called by the server immediately after the server constructs the Servlets instance. doGet( ) method is used when Servlets is called directly. Each time the web server receives a POST request for this Servlets, the server invokes this doPost( ) method, passing an HttpServletRequset object an HttpServletResponse object. The HttpServletRequest represents the client’s request and The HttpServletResponse represents the Servlets response. The server calls a Servlets destoy( ) method when the Servlets is about to be unloaded. All servlets files are kept in servlets directory.


import javax.servlet.*;

import javax.servlet.http.*;

Import java.io.*;


public class courses extends HttpServlet  {


public void doPost( HttpServletRequest req, Httpservletresponse res) throws ServletException,IOException

{

res.setContentType(“text/html”);

Printwriter out = res.getWriter( );

String str;

str=req.getParameter(“name”);

Out.println(“welcome “+name+” “);

}

}


setContentType( ) method of the response object is used to set the content Type(MIME) of its response to “text/html” , which is the standard MIME content type for HTML pages. MIME or Multi-purpose Internet Mail extensions provides a method to exchange files in any format between computers using Internet mail standards. The servlet uses getParameterValues (String) of ServletRequest object to get parameter values. This function returns the values of the specified parameter for the request as an array of strings, or null if the named parameter does not exist. getParameter(string) method can also be used to get parameter value. It uses the getWriter( ) method to retrieve a PrintWriter object which converts java’s Unicode characters to a locale-specific encoding.


JSP

With JSP, web designers and developers can quickly incorporate dynamic elements into web page using embedded java and simple markup tags. JSP offers several benefits as a system for dynamic generation. As a Java-based technology, it enjoys all of the advantages that the java language provides with respect to development and deployment. By taking advantage of JSP’s build in support for Java Beans, it becomes possible to maintain a strict separation between data presentation – the display of information to the end user and the program implementation –  the code used to generate that information.   The JSP’s tags consists of scripting oriented tag inspired by ASP and a full set of tags based on the Extensible Markup Language(XML).


JSP Directives:

Directives do not directly produce an output that is visible to end users when a the page is requested; instead they generate side effects that are change the way the JSP container processes the page.


Page directives:   

<@ page attribute=”value1” attribute=”value2”   %>


Attributes supported by the page directives:


Attribute

Definition

Default

Info

This is used to add a Documentation String indicating the function done by a page.


language

Scripting language

“java”

contentType

MIME type

“text/html”

extends

Class name


import

Class name


session

This is used to indicate whether JSP page participates in session management

“True”

buffer

This attribute controls the use of buffered output for a JSP page

“8kb”  /false

autoFlush

If this is set to true, the output buffer will automatically be flushed.

“true”

isThreadSafe

This attribute is used to check whether it is capable of responding multiple simultaneous requests or not.

“true”

errorPage

This attribute is used to specify an alternate page to display if an error occurs while the JSP container is processing the page.


IsErrorPage

This attribute is used to mark a JSP page that serves as the error page for one or more other JSP pages.

“false”


Request object:

The request object represents the request that triggered the processing of the current page.


Method

Description

getParameter(name)

Returns the value of a single request parameter.

getParameterNames( )

Returns the names of all request parameters.

getParameterValues( name)

Returns all values for a single request parameter.

getHeader(name)


getHeaderNames( )


getHeaders(name)


getCookies( )

Retrieves all of the cookies associated with the request.

getSession(flag)

Retrieves the session data for the request if it is present.

getMethod( )

returns  the HTTP method for the request.

getRequestURL( )

Returns the request URL.

getRequestDispatcher( path )

Creates the request dispatcher for the indicated local URL.

getRemoteHost()

Returns the name of the remote host.

getRemoteAddr( )

Returns the network address of the host that sends the request.

getRemoteUser( )

returns the name of the user that sends the request.


Response Object:

The response object represents the response that will send back to a user a result of processing the JSP page.


setContentType( )


addCookie( cookie)

Adds a specified cookie to the response.

setHeader( name, value)


addHeader( name, value)


         

CONCLUSION


The program to input the data from user and display the data stored in a database are successfully implemented in Servlets and JSP.


Share with others