ASP programmers often need to process application_start and session_start in the global. Asa file.
Events, such as user verification and redirection of different encoding pages, are very convenient. However, the popular JSP does not support such processing, except that the commercial server JRun supports global. in addition to JSA, tomcat, an open source server with extremely high application rate, is not supported, which creates a lot of obstacles for JSP development, although Tomcat (earlier than 5.0) the following methods can be used to process the session-start event:
1. Add a session
Session. Put ("Bind. listener", new mylistener (getservletcontext ())
2. Define the mylistener class
Import javax. servlet. http .*;
Import javax. servlet .*;
Public class mylistener implements httpsessionbindinglistener
{
Servletcontext context;
Public odsessionlistener (servletcontext context)
{
This. Context = context;
}
Public void valuebound (httpsessionbindingevent event)
{
System. Out. println ("bound ");
}
Public void valueunbound (httpsessionbindingevent event)
{
System. Out. println ("unbound ");
}
}
However, the premise is that the session has been established and cannot process verification and other events when the session is created. By studying servlet 2.4 (tomcat5.0 built-in), we finally find a feasible method. The implementation method is as follows:
// ---------------------- Sessionlistener. Java ----------------------------------------------------
Package listeners;
Import javax. servlet. servletcontext;
Import javax. servlet. servletcontextevent;
Import javax. servlet. servletcontextlistener;
Import javax. servlet. http. httpsessionattributelistener;
Import javax. servlet. http. httpsessionbindingevent;
Import javax. servlet. http. httpsessionevent;
Import javax. servlet. http. httpsessionlistener;
// ServletrequestlistenerIs servlet2.4New Interface
Import javax. servlet. servletrequestlistener;
Import javax. servlet. servletrequestevent;
Import javax. servlet. http. httpservletrequest;
Public final class sessionlistener
Implements httpsessionlistener, servletrequestlistener {
Private httpservletrequest request;
Public void requestdestroyed (servletrequestevent SRE ){}
Public void requestinitialized (servletrequestevent SRE)
{
Request = (httpservletrequest) SRE. getservletrequest ();
}
Public void sessioncreated (httpsessionevent event ){
String logmsg = event. getsession (). GETID ()
+ "'' "+ Request. getremoteaddr ()
+ ":" + Request. getremoteport ();
Log ("sessioncreated ('" + logmsg + "')");
}
Public void sessiondestroyed (httpsessionevent event ){
Log ("sessiondestroyed ('" + event. getsession (). GETID () + "')");
}
Private void log (string message ){
System. Out. println ("sessionlistener:" + message );
}
}
When receiving a client request (requestinitialized), the current httprequest object is obtained and stored in the private member request. In this way, the session user can be verified and the session can be closed during session creation, restrict IP address access. Here we only record the session source as an example (sessionlistener. java)
After compiling this class, add the following lines to the host element of Web. xml:
<Listener>
<Listener-class> listeners. sessionlistener </listener-class>
</Listener>
Applicable environment: Tomcat 5.02 and j2se1.41 (commercial servers not supported)