Home > Java > javaTutorial > Introduction to Servlet life cycle

Introduction to Servlet life cycle

不言
Release: 2019-03-25 10:54:09
forward
2849 people have browsed it

This article brings you an introduction to the Servlet life cycle. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The life cycle of a Servlet is controlled by the container it is deployed in. When a request is mapped to a Servlet, the Servlet container performs the following steps.
1 If there is no instance of this Servlet, the container executes:
a. Load the Servlet class
b. Create a Servlet class instance
c. Initialize the Servlet by calling the init method (initialization is covered in Creating and Initializing a Servlet)
2 The container calls the service method of the Servlet, passing in the requset and response parameters. Service methods will be discussed in the Writing Service Methods chapter.
If it needs to remove this Servlet, the container does so by calling the Servlet's destroy method. See the Finalizing a Servlet chapter for more information.

Manage Servlet life cycle events

You can listen to the Servlet life cycle and react by defining a listener for the life cycle event occurrence method. To use these listener objects, you must define and specify the listener classes.

Define the listening class

The listening class you define must be an implementation of the listening interface. The following list defines the events that can be listened to and the corresponding canonical interfaces that must be implemented. When the listener method is called, it will be passed in an event with information appropriate to the event's container. For example, a method of the HttpSessionListener interface will be passed in an HttpSessionEvent, which contains an HttpSession.
Servlet life cycle event list

Web context

event: Initialization and destruction
Listener Interface and Event Class: javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction
Listener Interface and Event Class: javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction
Listener Interface and Event Class: javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction
Listener Interface and Event Class:javax.servlet. ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction
Listener Interface and Event Class:javax.servlet.ServletContextListener and ServletContextEvent

Web context

event:Initialization and destruction
Listener Interface and Event Class :javax.servlet.ServletContextListener and ServletContextEvent

Use the @WebListener annotation to obtain various events of a specific web program. Classes annotated with @WebListener must implement one of the following interfaces.

javax.servlet.ServletContextListener
javax.servlet.ServletContextAttributeListener
javax.servlet.ServletRequestListener
javax.servlet.ServletRequestAttributeListener
javax.servlet..http.HttpSessionListener
javax.servlet..http.HttpSessionAttributeListener
Copy after login

Handling Servlet exceptions

When a Servlet is executed, any number of exceptions may occur. When an exception occurs, the web container automatically generates a default page containing the following information:

A Servlet Exception Has Occurred
Copy after login

But you can also define a specific page for a specific exception for the container to return.

This article has ended here. For more other exciting content, you can pay attention to the Java Video Tutorial column of the PHP Chinese website!

The above is the detailed content of Introduction to Servlet life cycle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template