JSP Listener usage Analysis _jsp programming

Source: Internet
Author: User

This example describes the use of JSP listeners. Share to everyone for your reference, specific as follows:

The listener, also called listener, is the listener for the servlet service. It can listen to client's request, service side operation and so on. For example, count the number of online users. Whenever a httpsession is added, the sessioncreate (httpsessionevent se) method is triggered so that the number of online people can be increased by 1. Common listener interfaces are as follows:

1. Servletcontextattributelistener listens to the operation of the ServletContext property. For example, add, delete, modify attributes.

2. Servletcontextlistener Monitor ServletContext. When the ServletContext is created, the contextinitialized (Servletcontextevent SCE) method is fired, and when the ServletContext is destroyed, the contextdestroyed is fired ( Servletcontextevent sce) method.

3. Httpsessionlistener listens for httpsession operation. When a session is created, the session Created (Httpsessionevent se) method is fired, and the sessiondestroyed (httpsessionevent se) method is fired when a session is destroyed.
4. Httpsessionattributelistener listens for properties in HttpSession. When an attribute is added to the session, the attributeadded (httpsessionbindingevent se) method is excited;

Fires the attributeremoved (httpsessionbindingevent se) method when deleting an attribute in session;

Fires the attributereplaced (httpsessionbindingevent se) method when the session property is reset.

An example of an online statistic:

public class ONline implements Servletcontextlistener,httpsessionlistener,httpsessionattributelistener () {
Private ServletContext application = null;
 public void contextinitialized (Servletcontextevent arg0) {
 //Initialize ServletContext this.application based on response event parameters
 Arg0.getservletcontext ();
 Store an empty list of user information 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"); C16/>if (Arg0.getname (). Equals ("name")) {
  List.add (Arg0.getvalue (). toString ());
 Application.setattribute ("Users", list);
 }


Configuration in Web.xml file:

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

Attachment: When is the session created?

A common misconception is that the session is created when there is a client access, but the fact is that a statement called Httpservletrequest.getsession (TRUE) on a server side is created. Note If the JSP page does not explicitly use <%page session= "false"%> to close the session, the JSP page is automatically added to the servlet page when it is compiled httpservletrequest.getsession (true) this sentence. This is also the origin of the hidden 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.