Servlet Lifecycle
The lifecycle of a servlet is controlled by the container in which the servlet has been deployed.
- If an instance of the servlet does not exist, the web container
- Loads the servlet class.
- Creates an instance of the servlet class.
- Initializes the servlet instance by calling the init method.
- Invokes the service method, passing request and response objects.
If it needs to remove the servlet, the container finalizes the servlet by calling the servlet’s
destroy method.
Handling Servlet Lifecycle Events
You can monitor and react to events in a servlet’s lifecycle by defining listener objects whose
methods get invoked when lifecycle events occur.
Object | Event | Listener Interface and Event Class |
Web context | Initialization and destruction | javax.servlet.ServletContextListener, javax.servlet.ServletContextEvent |
Attribute added, removed, or replaced | javax.servlet.ServletContextAttributeListener, javax.servlet.ServletContextAttributeEvent | |
Session | Creation, invalidation, activation, passivation, and timeout | javax.servlet.http.HttpSessionListener, javax.servlet.http.HttpSessionActivationListener, javax.servlet.http.HttpSessionEvent |
Attribute added, removed, or replaced | javax.servlet.http.HttpSessionAttributeListener, javax.servlet.http.HttpSessionBindingEvent | |
Request | A servlet request has started being processed by web components | javax.servlet.ServletRequestListener, javax.servlet.ServletRequestEvent |
Attribute added, removed, or replaced | javax.servlet.ServletRequestAttributeListener, javax.servlet.ServletRequestAttributeEvent |
Sharing Information
Using Scope Objects
Web context, Session, Request, Page
Controlling Concurrent Access to Shared Resources
In a multithreaded server, shared resources can be accessed concurrently. In addition to scope
object attributes, shared resources include in-memory data, such as instance or class variables,
and external objects, such as files, database connections, and network connections.
Concurrent access can arise in several situations:
- Multiple web components accessing objects stored in the web context.
- Multiple web components accessing objects stored in a session.
- Multiple threads within a web component accessing instance variables. To ensure that a servlet instance handles only one request at a time, a servlet can implement the SingleThreadModel interface. If aservlet implements this interface, no two threads will execute concurrently in the servlet’s service method. A web container can implement this guarantee by synchronizing access to a single instance of the servlet or by maintaining a pool of web component instances and
dispatching each new request to a free instance. This interface does not prevent synchronization problems that result from web components’ accessing shared resources, such as static class variables or external objects.
Service Method
public abstract class HttpServlet extends GenericServlet
implements Serializable
{
...
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if(method.equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
doGet(req, resp);
} else
{
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if(ifModifiedSince < (lastModified / 1000L) * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else
{
resp.setStatus(304);
}
}
} else
if(method.equals("HEAD"))
{
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else
if(method.equals("POST"))
{
doPost(req, resp);
} else
if(method.equals("PUT"))
{
doPut(req, resp);
} else
if(method.equals("DELETE"))
{
doDelete(req, resp);
} else
if(method.equals("OPTIONS"))
{
doOptions(req, resp);
} else
if(method.equals("TRACE"))
{
doTrace(req, resp);
} else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object errArgs[] = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
...
}
SessionTracking
If your application uses session objects, you must ensure that session tracking is enabled by
having the application rewrite URLs whenever the client turns off cookies. You do this by
calling the response’s encodeURL(URL) method on all URLs returned by a servlet. This method
includes the session ID in the URL only if cookies are disabled; otherwise, the method returns
the URL unchanged.