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