Learning and using servlet listener (3)

This article shares the specific learning contents of servlet listener for your reference. The specific contents are as follows

1. What is a servlet listener?

Servlet listeners are also called web listeners. Is a special class in servlet. It can help developers monitor specific events in web applications. For example, the creation and destruction of ServletContext, servletsession, ServletRequest, variables, etc.

2 common uses of listeners

Generally, web listeners are used to do the following:

Count the number of people online and use httpsessionliser

Loading initialization information: using servletcontextlistener

Statistics of website visits

Implement access monitoring

3. Classification of listeners

Listeners can be divided into three types according to servlet objects: ServletContext, servletsession and ServletRequest.

4 Application of listener

4.1 ServletContext: the creation and destruction of built-in objects in the corresponding monitoring application.

When the web container is opened, execute the contextinitialized method; When the container is closed or restarted, the contextdestroyed method is executed.

Implementation method: directly implement the servletcontextlistener interface

4.2 httpsession monitoring: the creation and destruction of built-in objects in the corresponding monitoring session.

When opening a new page, open a session and execute the sessioncreated method; The sessiondestroyed method is executed when the page session expires, or when the container is closed and destroyed.

Implementation method: directly implement the HttpSessionListener interface:

4.3 ServletRequest monitoring: monitor the creation and destruction of the built-in object of the corresponding request.

When accessing a page, start a request and execute the requestinitialized method; When the page closes, execute the requestdestroyed method.

Implementation method: directly implement the ServletRequestListener interface:

5 listener web XML configuration

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>