The lifecycle of a servlet is controlled by the container in which the servlet has been deployed.
If it needs to remove the servlet, the container finalizes the servlet by calling the servlet’s
destroy method.
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 |
Web context, Session, Request, Page
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:
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); } } ... }
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.