Session Monitor class Httpsessionlistener introduction and GET request in listener

Source: Internet
Author: User

Session Monitor class Httpsessionlistener introduction and GET request in listener

Servlet-api.jar provides a listener class Httpsessionlistener, the main methods are two, sessioncreated, sessiondestroyed, According to the method name can understand is the listener session creation and destruction.

In the first place, it is unavoidable to get the request object in the Sessioncreated method (and then the user IP, browser and other attributes), but Httpsessionevent obviously cannot get the request object directly.

Under research, it is found that spring (mainly the context jar package) provides the Listener class:Requestcontextlistener, which can be directly fitted to Web. XML, through which the request context can be directly obtained. Examples are as follows:

Java code

HttpServletRequest request = ((servletrequestattributes) requestcontextholder.getrequestattributes ()). GetRequest () ;

System.out.println (Request.getheader ("user-agent"));



--------------------------------
In addition, although the sessioncreated can obtain the request object, but must not write Request.getsession (true) similar method, this will cause the cycle of death, the reason is sessioncreated listener session is created, The session is not fully built at this point because using Request.getsession (true) does not get the session instance we want (we all know that request.getsession (true) means getting the session object , if the session does not exist, it will be created again).

So it will be tragic fall into: the monitoring method with request to get the session, did not get, create session, monitoring method and effective, after the entry into force to continue to get the session, did not get, create session ... In such a loop.

In summary, the session instance should be taken from httpsessionevent.

Session Monitor class Httpsessionlistener introduction and GET request in listener

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.