| |
"Pluggable Session Management for Java Servlet-Based Web Applications"
Vol. 5, Issue 11, p. 14
Listing 1: State maintenance using a simple cookie
public class CookieServlet extends HttpServlet
{
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
Integer nof = new Integer(0);
Cookie cookies[] = req.getCookies();
for (int i = 0; i < cookies.length; i++)
if (cookies[i].getName().equals("CounterCookie"))
{
String nofS = cookies[i].getValue();
try {
nof = Integer.valueOf (nofS);
}
catch (Exception nfe) {}
break;
}
nof = new Integer (nof.intValue()+1);
Cookie c = new Cookie ("CounterCookie", nof.toString());
res.addCookie(c);
// ...
out.println("You have visited this page" + nof + " times");
// ...
}
}
Listing 2: State maintenance using a servlet server-side session
public class SessionServlet extends HttpServlet
{
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
// this will create the session if it doesn't exist
HttpSession session = req.getSession (true);
PrintWriter out = res.getWriter();
out.println("<HEAD><TITLE> " + "SessionServlet Output " + "</TITLE></HEAD><BODY>");
out.println("<h1> SessionServlet Output </h1>");
Integer ival =
(Integer) session.getValue("sessiontest.counter");
if (ival==null)
ival = new Integer(1);
else
ival = new Integer(ival.intValue() + 1);
session.putValue("sessiontest.counter", ival);
out.println("You have hit this page <b>" +
ival + "</b> times.<p>");
// Session ID encoded in the response URL
out.println("<a href=\"" + res.encodeUrl (myUrl) +
"\">Click here </a> if your browser doesn't support cookies");
out.println("</BODY>");
}
}
Listing 3: Base Class for all pluggable session managers
/*
* Every session manager supported by iWS must extend this class
*/
package com.netscape.server.http.session;
...
public abstract class NSHttpSessionManager {
/**
* Initializes session manager. This method will only be called once
*
* @param props A collection of init parameters passed by webserver
*/
public abstract void init (Properties props);
/**
* Creates a session with the specified session id
*
* @param id session ID
* @return newly created session
*/
public abstract HttpSession createSession(String id);
/**
* Delete a specified session
*
* @param session
*/
public abstract void deleteSession(HttpSession session);
/**
* Request an existing session with the specified session id
*
* @param id session ID
*/
public abstract HttpSession getSession(String id);
/**
* Get default session expiration timeout in seconds
*/
public abstract int getDefaultTimeOut();
/**
* a method which will be called periodically to let the
* session manager expire old sessions
*/
public abstract void reaper ();
/**
* Update method will be called by the server at the end
* of every request to give
* the session manager an opportunity to store the session
* into reliable storage if needed
*
* @param session The session object
*/
public void update (HttpSession session); // does nothing by default
/**
* Generate random session ID
*
* @return random unique string which represents random ID
*/
public String generateSID ();
// generates cryptographically-strong ID by default
public int getMaxSession(); // returns 0 by default
public int getSessionCount(); // returns 0 by default
}
|
|