The use of Servletrequestlistener, Httpsessionlistener and Servletcontextlistener can bring request,session, And the life cycle of application.
> Requests: is a request that is destroyed when a response is returned. is created when a request is sent. Note: The process of requesting forwarding is a request object.
Note: Properties placed on the page are not available in the servlet because the request has been destroyed after the page responds.
Note: Redirection is a two request.
> Session: When you first access a JSP or servlet for your web app, you also need to create a session object in that JSP or servlet.
Session Destruction: Session expires, directly call the session Invilidate method, the current Web application is uninstalled (session can be persisted)
* Close the browser does not mean that the session is destroyed, you can also find the session object in the server via SessionID
> Application: Throughout the life cycle of your current WEB application. When the current web app is loaded, the Application object is created, and the current web app is
Destroys the Application object when unloaded.
Servletcontextlistener
1). What: Monitor the Servlet listener that the ServletContext object was created or destroyed
2). How:
> Create a class that implements the Servletcontextlistener, and implement two of these methods
public class Helloservletcontextlistener implements Servletcontextlistener
> Configuring Listener in the Web. xml file
<listener>
<listener-class>com.java.javaweb.test.HelloServletContextListener</listener-class>
</listener>
3). Why:servletcontextlistener is the most commonly used listener that initializes the relevant resources for the current web app when the current web app is loaded:
Create a database connection pool, create a spring IOC container, read the initialization parameters of the current WEB app ...
4). API:
| Method Summary |
void |
contextDestroyed(ServletContextEvent sce)
Notification that, the servlet context is on to being shut down. |
void |
contextInitialized(ServletContextEvent sce)
Notification the Web application initialization process is starting. |
Javaweb Understanding the Domain object life cycle through listener