Httpsession wrapper

Discussions

Web tier: servlets, JSP, Web frameworks: Httpsession wrapper

  1. Httpsession wrapper (6 messages)

    I've been googling around for info/tips on implementing a httpsession wrapper. Basically, I'm trying to implement a custom session object that stores session info into a database table.

    I want to make this transparent to an application, such that the custom session object works the same as httpsession, apart from storing session info in a database.

    Is this achievable? Any example code?

    Yng

    Threaded Messages (6)

  2. Httpsession wrapper[ Go to top ]

    Hi William,

    If you are using a Servlet 2.3 compliant container you can easily set up a filter to wrap the existing request with your own custom request which will return your custom session object.

    Later,
    Rob Misek
    Tangosol, Inc.
    Coherence: Cluster your Work. Work your Cluster.
  3. Httpsession wrapper[ Go to top ]

    Hi Rob,

    My problem is that I'm using a web container (ServletExec 3.1) that supports Servlet 2.2 only. Are there other ways (even if more difficult) of intercepting a session?

    Many thanks for replying.

    Yng
  4. Httpsession wrapper[ Go to top ]

    Well our upcoming Session Management Module will be able to handle this scenario and more. It will handle Servlet 2.2 and up, plus allow you to plug-in your wn custom session.

    Later,
    Rob Misek
    Tangosol Coherence: Cluster your Work. Work your Cluster.
    Coherence Forums.
  5. Httpsession wrapper[ Go to top ]

    How about this? In this case, All the servlets should subclass your class.(Just like Struts implementation).



    Here is the code sample:

    abstract public class ExtendedServlet extends HttpServlet {


    public void doGost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{

    //First get the session from request, load the session from DB, Change the refernce to DB one
    HttpSession ses = req.getSession();
    ses = loadSessionFromDB(ses.getId());

    // Now invoke the abtsract method, which will be invoked by the subclass
    process(req, resp);

    //At the end, store the session into DB
    storeSessionToDB(ses);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{

    //First get the session from request, load the session from DB, Change the refernce to DB one
    HttpSession ses = req.getSession();
    ses = loadSessionFromDB(ses.getId());

    // Now invoke the abtsract method, which will be invoked by the subclass
    process(req, resp);

    //At the end, store the session into DB
    storeSessionToDB(ses);
    }

    private void storeSessionToDB(HttpSession ses) {
    // Call your Session Synchronizer

    SessionSynchronizer sesSync = new SessionSynchronizer();
    sesSync.store(ses.getId(), ses);
    }

    private HttpSession loadSessionFromDB() {
    // Call your Session Synchronizer

    SessionSynchronizer sesSync = new SessionSynchronizer();
    return sesSync.load(ses.getId());
    }

    // This method has to be implemented by the subclass
    protected abstract void process(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException;

    }

    public class SessionSynchronizer {
         public void store(String sesId, HttpSession ses) {
              .....
              // Store it in database.
         }

         public HttpSession load(String sesId) {
              .....
              //load from db
              return ses;
         }


    }

    Hope this will work,
    Thanks,
    Senthil.
  6. Httpsession wrapper[ Go to top ]

    I am sorry, my previous code has some errors. Here is the right one

    The logic is look for the cookie, jsessionid, if it is not there look for jsessionid parameter(URL rewriting), if it is stiil not there, create a session object by invoking req.getSession(true). If the parameter is there, load the id's data from database and set it in the request.

    I think the session variable name is sessionid in all containers, atleast in OC4J, the name is sessionid. Please refer your container.


    public void doGost(HttpServletRequest req, HttpServletResponse resp)
                      throws IOException, ServletException{

               HttpSession ses = null;

                   // Define this variable outside the class
                   public static final String sessionIdName = "jsessionid";

                   String sessionIdValue = null;
                   Cookie[] cookie = request.getCookies();

                   if(cookie != null) {
                         for(int i = 0; i< cookie.length; i++) {
                               if(cookie[i].equalsIgnoreCase(sessionIdName)) {
                                     sessionIdValue = cookie[i].getValue();
                               }
                         }

                    }

                    // If the sessionId cookie is not there, there may be a chance that the client might have disabled the cookies, so look for URL rewriting

                    if(sessionIdValue == null) {
                           sessionIdValue = request.getParameter(sessionIdName);
                    }

                    // If Still the sessionId Value is null, then there is no session created previosly, so create one.
                    if(sessionIdValue == null) {
                           ses = request.getSession(true);
                    } else {
                           ses = loadSessionFromDB(sessionIdValue);
                    }

                     // Now invoke the abtsract method, which will be invoked by the subclass
                    process(req, resp);

                    //At the end, store the session into DB
                    storeSessionToDB(ses);
    }

    Hope this helps,
    Senthil.
  7. Httpsession wrapper[ Go to top ]

    I'm trying to implement a custom session object that stores session info into a database table. I'm using Servlets2.3. Inorder to implement HttpSession looks like we need to provide the implementations for several methods? I am not sure how to implement those. Any suggestions/ideas welcome