JSP & Servlet session control
Author: guipei
Foreword
In the JSP and Servlet specification for J2EE, session processing has a very important location. At present, many of the information is very detailed to explain how conversation is handled. However, rarely involve session control, originally in the servlet specification, servlet provides the HTTPSessionContext interface handling session control function, but after servlet API 2.1, this feature is canceled, refer to the original (AS of Java (TM) Servlet API 2.1 for Security Reasons, With No Replacement. This Interface Will Be Removed In A Future Version of this API.).
In this article, the author will introduce you to a method of session control, using Listener technology to implement HTTPSESSESCONTEXT functional replacement. Many developers will use this feature to complete certain tasks in some occasions, such as: online personnel information viewing, online personnel control, etc.
analysis
This article uses an example mode to introduce the session control function. Use a number of JSP pages, and a Java class completes the entire functional demo. See the table below for details:
Component function com.guipei.listener. SessionListener listening component, complete HTTPSessionContext function index.jsp implement user login, create new session logout.jsp implement users to exit, users automatically delete session display.jsp Show user login information, after login Automatically transfer to session.jsp list all the current session kill.jsp killed the specified session so that this user connection is invalid
achieve
Monitor class com.guipei.listener.SessionListener implements the listening function of Web Application, which implements the HttpSessionListener interface to listen to the sessioncreated (httpsessionEvent SE) method, so we can easily create and destroy events in Session. Treatment of session during the process.
In this class, we create a static instance variable HashTable HT, which is the advantage of using HashTable is that it is a set class for thread security, no need to do more thread processing. Use this Collection class to save the session object we have to control. Easy to process related tasks in listening events.
See all code:
Package com.guipei.listener;
Import java.util.hashtable;
Import java.util.iterator;
Import javax.servlet.http.httpsession;
Import javax.servlet.http.httpsessionEvent;
Import javax.servlet.http.httpsessionListener;
Public class sessionListener implements httpsessionListener {
// Collect object, save the reference to the session object
Static hashtable ht = new hashtable ();
/ / Implement the HTTPSessionListener interface to complete the session creation event control
Public void sessioncreated (httpsessionever session) {httpsession session = arg0.getations ();
HT.PUT (session.getid (), session);
System.out.println ("CREATE Session:" session.getID ());
}
/ / Implement the HTTPSessionListener interface to complete the session destruction event control
Public void sessiondestroyed (httpsessioneverive) {
HttpSession session = arg0.getsession ();
System.out.println ("Destory session:" session.getID ());
HT.Remove (session.getid ());
}
/ / Return to all session objects
Static public iterator getSet () {
Return ht.values (). Iterator ();
}
/ / Return the specified session object based on the session ID
Static public httpsession getsession (String sessionID) {
Return (httpsession) ht.get (sessionid);
}
}
The page index.jsp hands the user login and creates a new session. After completing the verification, jump to the Display.jsp page.
<% @ Page ContentType = "TEXT / HTML; Charset = GB2312"%> <% string strname = null; string straw = null; try { STRNAME = Request.getParameter ("name"); strast.getParameter ("think"); if ((strname == null) || (strname.length () == 0)) {throw new exception ("null ");} if (straw == null) || (" Null Strth "); // Add session session.setttribute (" name ", strname) Session.SetaTribute ("Thing", StRTHING; Response.sendRedirect ("Display.jsp");} Catch (Exception E) {}%>