Servlet Lifecycle

Servlet Lifecycle

 

Servlet life cycle consists of three main stages 

 

1.  initialization          2.  service                 3.   destroy

 

Methods are also defined for representing each stages of the servlet life cycle.

 

The methods are init(), service () and destroy ().

 

When the servlet is first created, its init method is invoked, so that is where you put one-time setup code. After this, each user request results in a thread that calls the service method of the previously created instance. Multiple concurrent requests normally result in multiple threads caling service simultaneously, alt6hough your servlet can implement a special interface that stipulates that only a single thread is permuitted to run at any one time. the service method then calls doGet, do Post, or another doXxx method, depending on the type of HTTP request it received. Finally, when the server decides to unload a servlet, it first calls the servlet's destory method. The following example shows the different stages of servlet life cycle. The following class extends Generic Servlet (we can also user Http Servlet), overrides the init(), service() and destroy() methods. You can find the corresponding  messages given in each life cycle methods in the console of the web server.

 

Handling HTTP Clients 

An HTTP Servlet handles client requests through its service method.  The service method supports standard HTTP client requests by dispatching each requests to a method designed to handle that requests. For example, the service method calls the do Get method shown earlier example. (Example  1)

 

Requests and Responses 

 

Methods in the HttpServlet class the handle client requests take two arguments:

 

1. An HttpServlet Request object, which encapsulates the data from the client

 

2. An HttpServlet Request object, which encapsulates the response to the client

 

Http Servlet Request Objects

 

An Http Servlet Request object provides access to HTTP header data, such as any cookies found in the request and the HTTP method with which the request was made. The Http Servlet request object also allows you to obtain the arguments that the client sent as part of the request 

 

To access client data:



·                 The getParameter method returns the value of a named parameter. If your parameter could have more than one value, use getParameter Values instead. The get ParameterValues method returns and array of values for the named parameter. (The method get parameter Names provides the names of the parameters.)

·                 For HTTP GET requests, the getQueryString method returns a String of raw data from the client. You must parse this data yourself to obtain the parameters and values.

·                 For HTTP POST, and DELETE requests,

 

-  If you expect text data, the get Reader method returns a Buffered Reader for you to use to read the raw data.

-   If you expect binary data, the getInput Stream method returns a Servlet InputStream for you to use to read the raw data.

 

Http Servlet Response Objects 

An Http Servlet Response object provides two ways of returning data to the user:

·                 The get writer method returns a Writer

·                 The getOutputStream method returns a Servlet Output Stream

Use the getwriter method to return text data to the user, and the getOutputStream method for binary data.

Closing the Writer or ServletOutputStream after you send the response allows the server to know when the response is complete.

 

HTTP Header Data

 

You must set HTTP header data before you access the Writer or OutputStream. The HttpServletResponse class provides methods to access the header data.  For example, the set content Type() method sets the content type. (This header is often the only one manually set.)

 

Handling GET and POST Requests    

 

The methods to which the service method delegates HTTP requests include, 

·                 doGet, for handling GET, conditional GET, and HEAD requests

·                 doPost, for handling POST requests

·                 doPut, for handling PUT requests

·                 doDelete, for handling DELETE requests

 

By default,these ,methods return a BAD-REQUEST (400) error. You servlet should override the method or methods designed to handle the HTTP interactions that it supports. This section shows you to implement methods that handle the most common HTTP requests: GET and POST.

The HTTP Servlet's service method also calls the do Options method when the servlet receives an OPTIONS request, and doTrace when the servlet receives a TRACE request. The default implementation of do Options automatically determines what HTTP options are supported and returns that information. The default implementation of do Trace causes a response with a message containing all of the headers sent in the trace request. These methods are not  typically overridden.

 

Threading Issues

 

HTTP  servlets are typically capable of serving multiple clients concurently. If the methods in your servlet that do work for clients access a shared  resource, then you can handle the concurency by creating a servlet that handles only one client request at a time. (You could also synchronize access to the resource, a general topic in the java programming language that is note covered in this tutorial.)

 

To have your servlet handle only one client at a time, have your servlet implement the  single Thread Model interface in adddition to extening the HttpServlet class.

 

Implementing the Single Thread Model interface does not involve writing any extra methods. You merely declare that the servlet implements the interface, and the server makes sure that your servlet runs only one service method at a time.

 


No comments:

Post a Comment