Java EE learning path-servlet Lifecycle

Source: Internet
Author: User


The lifecycle of a servlet is controlled by the container in which the servlet has been deployed. When a request was mapped to a servlet, the container performs the following steps.

    1. If an instance of the Servlets does not exist, the Web container:

      1. Loads the Servlet class

      2. Creates an instance of the Servlet class

      3. Initializes the servlet instance by calling the init method (initialization is covered increating and Initializing a Serv Let

    2. The container invokes service the method, passing request and response objects. Service methods is discussed inwriting service methods.

If it needs to remove the servlet, the container finalizes the servlet by calling the Servlet's destroy method. For more information, see Finalizing a Servlet.

17.2.1Handling Servlet Lifecycle Events

You can monitor and react the events in a servlet's lifecycle by defining listener objects whose methods get invoked when L Ifecycle events occur. To use these listener objects, you must define and specify the listener class.

17.2.1.1Defining the Listener Class

You define a listener class as an implementation of a listener interface. Table 17-1 lists the events that can is monitored and the corresponding interface that must be implemented. When a listener method is invoked, it's passed an event that contains information appropriate to the event. For example, the methods in the HttpSessionListener interface is passed an HttpSessionEvent , and which contains an HttpSession .

Table 17-1 Servlet Lifecycle Events

Object Event Listener Interface and Event Class

Web context

Initialization and destruction

javax.servlet.ServletContextListenerandServletContextEvent

Web context

Attribute added, removed, or replaced

javax.servlet.ServletContextAttributeListenerandServletContextAttributeEvent

Session

Creation, invalidation, activation, passivation, and timeout

javax.servlet.http.HttpSessionListener, javax.servlet.http.HttpSessionActivationListener , andHttpSessionEvent

Session

Attribute added, removed, or replaced

javax.servlet.http.HttpSessionAttributeListenerandHttpSessionBindingEvent

Request

A servlet request has started being processed by Web Components

javax.servlet.ServletRequestListenerandServletRequestEvent

Request

Attribute added, removed, or replaced

javax.servlet.ServletRequestAttributeListenerandServletRequestAttributeEvent


Use the @WebListener annotation to define a listener to get events for various operations on the particular Web application conte Xt. Classes annotated with @WebListener must implement one of the following interfaces:

Javax.servlet.ServletContextListenerjavax.servlet.ServletContextAttributeListenerjavax.servlet.ServletRequestListenerjava X.servlet.servletrequestattributelistenerjavax.servlet. http. Httpsessionlistenerjavax.servlet. http. Httpsessionattributelistener

For example, the following code snippet defines a listener that implements, the these interfaces:

Import Javax.servlet.servletcontextattributelistener;import Javax.servlet.servletcontextlistener;import Javax.servlet.annotation.WebListener, @WebListener () public class Simpleservletlistener implements Servletcontextlistener,        servletcontextattributelistener {    ...
17.2.2Handling Servlet Errors

Any number of exceptions can occur when a servlet executes. When an exception occurs, the Web container generates a default page containing the following message:

A Servlet Exception has occurred

But can also specify so the container should return a specific error page for a given exception.

Java EE learning path-servlet Lifecycle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.