JSP listener Usage Analysis and jsp listener usage

Source: Internet
Author: User

JSP listener Usage Analysis and jsp listener usage

This example describes the JSP listener usage. We will share this with you for your reference. The details are as follows:

The Listener is also called Listener, which is the Listener of the servlet service. It can listen to client requests and server operations. For example, count the number of online users. When an HttpSession is added, the sessionCreate (HttpSessionEvent se) method is triggered. In this way, the number of online users can be added. 1. Common listener interfaces are as follows:

1. ServletContextAttributeListener listens for operations on the ServletContext attribute. For example, add, delete, and modify attributes.

2. ServletContextListener listens to ServletContext. When a ServletContext is created, the contextInitialized (ServletContextEvent sce) method is activated. When ServletContext is destroyed, the contextDestroyed (ServletContextEvent sce) method is activated.

3. HttpSessionListener listens for HttpSession operations. When a Session is Created, the session Created (HttpSessionEvent se) method is activated. When a Session is destroyed, the sessionDestroyed (HttpSessionEvent se) method is activated.
4. HttpSessionAttributeListener listens for Attribute operations in HttpSession. When an attribute is added to a Session, the attributeAdded (HttpSessionBindingEvent se) method is triggered;

When a Session deletes an attribute, the attributeRemoved (HttpSessionBindingEvent se) method is triggered;

When the Session attribute is reset, The attributeReplaced (HttpSessionBindingEvent se) method is triggered.

An example of online statistics:

Public class ONline implements ServletContextListener, HttpSessionListener, listener () {private ServletContext application = null; public void contextInitialized (ServletContextEvent arg0) {// initialize ServletContext this based on the response event parameters. application = arg0.getServletContext (); // store an empty user information list in ServletContext application. setAttribute ("users", new ArrayList ();} public void sessionDestroyed (HttpSessionEvent arg0) {List list = (List) application. getAttribute ("users"); String name = arg0.getSession (). getAttribute ("name "). toString (); list. remove (name); application. setAttribute ("users", list);} public void attributeAdded (HttpSessionBindingEvent arg0) {List list = (List) application. getAttribute ("users"); if (arg0.getName (). equals ("name") {list. add (arg0.getValue (). toString ();} application. setAttribute ("users", list );}}

Configuration in the web. xml file:

<listener>   <listener-class>package.classname</listener-class></listener>

Appendix: When will a session be created?

A common misunderstanding is that a session is created when a client accesses it. However, a server is created only when it calls a statement such as HttpServletRequest. getSession (true. Note: If the jsp page does not explicitly use <% page session = "false" %> to close the session, HttpServletRequest is automatically added when the jsp page is compiled into a Servlet page. getSession (true. This is also the history of hiding the object session in jsp.

I hope this article will help you with jsp program design.

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.