Tuesday, 5 February 2013

LIFE-CYCLE OF SERVLET AND SERVLET API STRUCTURE | J2EE Tutorial pdf

LIFE-CYCLE OF SERVLET AND SERVLET API STRUCTURE

The Life Cycle of a Servlet
The life cycle of a Java servlet follows a very logical sequence. The interface that declares the life-cycle methods is the javax.servlet.Servlet interface. These methods are the init(), the service(), and the destroy() methods. This sequence can be described in a simple three-step process:
1. A servlet is loaded and initialized using the init() method. This method will be called when a servlet is preloaded or upon the first request to this servlet.
2. The servlet then services zero or more requests. The servlet services each request using the service() method.
3. The servlet is then destroyed and garbage collected when the Web application containing the servlet shuts down. The method that is called upon shutdown is the destroy() method.
init() Method
The init() method is where the servlet begins its life. This method is called immediately after the servlet is instantiated. It is called only once. The init() method should be used to create and initialize the resources that it will be using while handling requests. The init() method’s signature is defined as follows:
public void init(ServletConfig config) throws ServletException;
The init() method takes a ServletConfig object as a parameter. This reference should be stored in a member variable so that it can be used later. A common way of doing this is to have the init() method call super.init() and pass it the ServletConfig object.
The init() method also declares that it can throw a ServletException. If for some reason the servlet cannot initialize the resources necessary to handle requests, it should throw a ServletException with an error message signifying the problem.
service() Method
The service() method services all requests received from a client using a simple request/response pattern. The service() method’s signature is shown here:
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException;
The service() method takes two parameters:
A ServletRequest object, which contains information about the service request and encapsulates information provided by the client.
A ServletResponse object, which contains the information returned to the client.
We will not usually implement this method directly, unless we extend the GenericServlet abstract class. The most common implementation of the service() method is in the HttpServlet class. The HttpServlet class implements the Servlet interface by extending GenericServlet. Its service() method supports standard HTTP/1.1 requests by determining the request type and calling the appropriate method.
destroy() Method
This method signifies the end of a servlet’s life. When a Web application is shut down, the servlet’s destroy() method is called. This is where all resources that were created in the init() method should be cleaned up. The following code snippet contains the signature of the destroy() method:
public void destroy();
A Simple Servlet Generating Plain Text:-
LifeCycleDemo.java
import java.io.*;
import javax.servlet.*;
public class LifeCycleDemo extends GenericServlet{
String name=null;
public void init() {
name=" Hello World";
}
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println(name);
}
public void destroy() {
name=null;
}
}
Output:-
Hello World

The Servlet API structure

The Servlet API is specified in two Java extension packages: javax.servlet and javax.servlet.http.
The classes and interfaces in the javax.servlet package are protocol-independent, while the second package, javax.servlet.http, contains classes and interfaces that are specific to HTTP.
Interfaces of package javax.servlet :--
RequestDispatcher- Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server.
Servle t - Defines methods that all servlets must implement.
ServletConfig- A servlet configuration object used by a servlet container used to pass information to a servlet during initialization.
ServletContext- Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
ServletRequest- Defines an object to provide client request information to a servlet.
ServletResponse- Defines an object to assist a servlet in sending a response to the client.
Classes of package javax.servlet :--
GenericServlet- Defines a generic, protocol-independent servlet.
ServletInputStream- Provides an input stream for reading binary data from a client request, including an efficient readLine method for reading data one line at a time.
ServletOutputStream- Provides an output stream for sending binary data to the client.
Interfaces of package javax.servlet.http:--
HttpServletRequest- Extends the ServletRequest interface to provide request information for HTTP servlets.
HttpServletResponse- Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response.
HttpSession- Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.
Classes of package javax.servlet.http :--
Cookie- Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. HttpServlet- Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site.
A Simple HttpServlet Generating Html Page:-
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class htmldemo extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse
res)throws ServletException,IOException{
res.setContentType ("text/html");
PrintWriter out=res.getWriter ();
out.println ("<html><body><center>");
out.println ("<table border=\"1\">");
out.println("<tr><td>Name</td><td>city</td></tr>");
out.println("<tr><td>X</td><td>bangalore</td></tr>");
out.println("<tr><td>y</td><td>pune</td></tr>");
out.println("</table></center</body></html>");
}
}

Output:-

No comments: