Java Web Listener Listener Simple implementation of online statistics

Source: Internet
Author: User

Today, we re-learned the basic Java Web Listener Technology, with Sessionlistener Simple implementation of the Web page statistics online number (there is a bug that when the page is closed when the session does not close still exists, Unless you shut down the server), the code is as follows (the front-end page code is not displayed):

Loginservlet.java:

1 Importjavax.servlet.ServletException;2 ImportJavax.servlet.annotation.WebServlet;3 ImportJavax.servlet.http.HttpServlet;4 Importjavax.servlet.http.HttpServletRequest;5 ImportJavax.servlet.http.HttpServletResponse;6 Importjavax.servlet.http.HttpSession;7 Importjava.io.IOException;8 9@WebServlet (name = "Loginservlet", Urlpatterns = "/login")Ten  Public classLoginservletextendsHttpServlet { One @Override A     protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -String Username=request.getparameter ("username"); -HttpSession session=request.getsession (); theSession.setattribute ("username", username); -         //Redirect to index.jsp -Response.sendredirect ("index.jsp"); -     } +  - @Override +     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { A  at     } -}

Exitservlet.java:

1  Packageservlet;2 3 Importjavax.servlet.ServletException;4 ImportJavax.servlet.annotation.WebServlet;5 ImportJavax.servlet.http.HttpServlet;6 Importjavax.servlet.http.HttpServletRequest;7 ImportJavax.servlet.http.HttpServletResponse;8 Importjavax.servlet.http.HttpSession;9 Importjava.io.IOException;Ten  One@WebServlet (name = "Exitservlet", Urlpatterns = "/exit") A  Public classExitservletextendsHttpServlet { - @Override -     protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { the     //Remove the LoginName parameter from the session -Response.getwriter (). Write ("2222"); -HttpSession session=request.getsession (); -Session.removeattribute ("username"); +         //Redirect to login.jsp -Response.sendredirect ("login.jsp"); +     } A  at @Override -     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { - DoPost (request, response); -     } -}

Onlinelistener.java:

1  PackageListener;2 3 ImportBean. User;4 5 Importjavax.servlet.ServletContextEvent;6 ImportJavax.servlet.ServletContextListener;7 ImportJavax.servlet.http.HttpSessionAttributeListener;8 Importjavax.servlet.http.HttpSessionEvent;9 ImportJavax.servlet.http.HttpSessionListener;Ten Importjavax.servlet.http.HttpSessionBindingEvent; One  A @javax. Servlet.annotation.WebListener () -  Public classOnlinelistenerImplementsServletcontextlistener, - Httpsessionlistener, Httpsessionattributelistener { the      PublicOnlinelistener () { -     } - @Override -      Public voidcontextinitialized (Servletcontextevent sce) { +       -     } + @Override A      Public voidcontextdestroyed (Servletcontextevent sce) { at      -     } - @Override -      Public voidsessioncreated (httpsessionevent se) { -System.out.println ("Session created"); -     } in @Override -      Public voidsessiondestroyed (httpsessionevent se) { toSYSTEM.OUT.PRINTLN ("Session destroyed"); +     } -      the @Override *      Public voidattributeadded (httpsessionbindingevent sbe) { $       //add a user to the session scopePanax Notoginseng User.adduser (string.valueof (Sbe.getvalue ())); -System.out.println ("Session increment attribute is" +sbe.getname () + "value:" +Sbe.getvalue ()); the     } + @Override A      Public voidattributeremoved (httpsessionbindingevent sbe) { the      //to remove a user from session sessions + User.removeuser (string.valueof (Sbe.getvalue ())); -System.out.println (Sbe.getvalue () + "property has been removed"); $     } $ @Override -      Public voidattributereplaced (httpsessionbindingevent sbe) { -       //get the old property value theString oldvalue=string.valueof (Sbe.getvalue ()); -         //gets the new property valueWuyiString newvalue=string.valueof (Sbe.getsession (). getattribute (Sbe.getname ())); the         //Remove old properties, add new properties - User.removeuser (oldValue); Wu User.adduser (newvalue); -     } About}

Run the results in two different browsers as shown in:

Project Engineering directory such as:

(Keep learning!) Try to refuel! Encourage yourself)

Java Web Listener Listener Simple implementation of online statistics

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.