Configuring Resin


The Servlet Life Cycle

In the past, all Web pages had an extension like .htm or .html, but now you might have a JSP file on the server with an extension of .jsp. As we explained in the introduction, servlets are Java classes that extend the functionality of a Web server. These classes are kept on the server and normally don't have an extension—only the classname. This means that to use a servlet, you might have a URL like this:

  • http://localhost:8080/hello/loginServlet

Obviously, a normal Web server cannot handle this type of request; therefore, you must host the servlet with an application server—in our case, Resin. You have to configure the Resin server to both recognize the path to the servlet as well as invoke the servlet. We cover this configuration in the next section.

When the application server receives a request to execute a servlet, it first verifies the path to the servlet based on the server configuration. It then attempts to find the CLASS file for the servlet name. Remember that all servlets are Java classes and therefore must be CLASS files. Resin attempts to find the servlet starting at the WEB-INF/classes directory (unless a different directory is specified). As you saw in Chapter 3, Resin automatically compiles the Java source found in the WEB-INF/classes directory, so if you've previously placed the servlet source in that directory, more than likely Resin has already compiled the code.

Once the servlet CLASS file has been located or immediately compiled, Resin creates an instance of the class for execution. The first method called against the server is init(). The init() method is designed to contain constructor-like code. The method is executed only once during the lifetime of the Servlet object.

The signature for the init() method is:

 public void init() throws ServletException() 

The default init() method doesn't perform any work and should be overridden if the servlet requires resources or database access. If the initialization is unable to complete successfully and the functionality of the servlet cannot be used, the init() method throws UnavailableException.

After the call to init(), the servlet attempts to service the HTTP POST or GET request. The servicing takes place in either the doPost() or doGet() method, both of which are overridden methods. The method is passed a Request object (with information from the user) and a Response object (with a container to hold a response).

In most cases, the Servlet object is kept in the application server's memory for use by another request. Note that the init() method is not executed again. If the application server determines that it needs to remove the servlet from memory, it makes a call to the servlet's destroy() method before completely removing it.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net