JSP session Processing

Source: Internet
Author: User

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)

 

 

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.