#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    63
    Rep Power
    16

    Session storing custom classes


    I have a wrapper class for an HttpSession that helps store and retrieve the session information. Basically, it just stores the attribute Strings and has get and set methods.

    A couple of the attributes I store are custom classes (the custom classes just store Strings and ints). The custom classes are used frequently throughout the application and perform perfectly.

    Initially, the custom class values are stored completely fine and can be retrieved by subsequent jsp's. However, if I recompile any of the classes within the entire application the custom class attributes are retrieved as "null" but the other regular String attributes are retrieved completely fine.

    Does anyone have an idea why this would occur?

    I use Tomcat 6.0.1.4 on Windows. I have the context as reloadable.

    Upon login, I initialize the "userSession" using the following and then set the individual attributes:
    Code:
    UserSession userSession = new UserSession(request.getSession(true));
    When I begin a jsp, I reobtain the session:
    Code:
    UserSession userSession = new UserSession(session);
    Code:
    public class UserSession {
        static private String className = "UserSession";
        
        //wrapper class for a session for a user
        static private String userIdString="userId";
        static private String usernameString="username";
        static private String authorityTypeString="authorityType";
        static private String authorityNameString="authority";
        static private String userSubscriptionString="userSubscription";
    
        private javax.servlet.http.HttpSession session;
    
        public UserSession(javax.servlet.http.HttpSession session) throws Exception{
            this.session = session;
            if(session.isNew()){
                //make sure write possible
                setUsername(""); 
                if(getUsername()==null){ //write failed because could not retrieve information
                    throw new CookieException("UserSession", "Could not create cookie");
                }
                //continue initializing values
                setUserId(-1);
                setAuthority(new AuthorityVO());
                setAuthorityType("");
                setUserSubscription(new UserSubscriptionVO());
                session.setMaxInactiveInterval(3600);//can be inactive for 1 hour (=60s x 60m) before session invalidated
            }
        }
        
        public boolean isNew(){
            if(getUsername().equals("")){ //must check value because creation defines valid session
                return true;
            }else{
                return false;
            }
        }
        public void invalidate(){
            session.invalidate();
        }
        //---GET--------------------------------------------------
        public String getUsername(){
            return (String) session.getAttribute(usernameString);
        }
        public int getUserId(){
            return Integer.parseInt((String) session.getAttribute(userIdString));
        }
        public AuthorityVO getAuthority(){
            return (AuthorityVO) session.getAttribute(authorityNameString);
        }
        public String getAuthorityType(){
            return (String) session.getAttribute(authorityTypeString);
        }
        public UserSubscriptionVO getUserSubscription(){
            return (UserSubscriptionVO) session.getAttribute(userSubscriptionString);
        }
        //---SET---------------------------------------------------
        public void setUsername(String username){
            session.setAttribute(usernameString, username);
        }
        public void setUserId(int userId){
            //session can only store Objects, Strings
            session.setAttribute(userIdString, String.valueOf(userId));
        }
        public void setAuthorityType(String authorityType){
            session.setAttribute(authorityTypeString, authorityType);
        }
        public void setAuthority(AuthorityVO authority){
            session.setAttribute(authorityNameString, authority);
        } 
        public void setUserSubscription(UserSubscriptionVO userSubscription){
            session.setAttribute(userSubscriptionString, userSubscription);
        }
    }
    Last edited by tabbycat; October 22nd, 2007 at 02:30 PM.
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    122
    I am not sure I follow. Are you saying that when you change a class file in the application (like a utility class or a servlet) that the application restarts and you can call userSession.getAuthorityType() and get a string, but if you call userSession.getUserSubscription() you get a null?

    If the server is indeed restarting, all session information should be lost, at least with the app servers I am familiar with (Weblogic, Websphere). Maybe Tomcat is doing some serialization behind the scenes to retain a session, in which case making your custom classes serializable may "fix" your problem.

    Comments on this post

    • tabbycat agrees
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    63
    Rep Power
    16
    When a class changes, such as a utility class, the server itself is not restarting but just reloads the classes. Since I defined the context of application is running to be reloadable, I do not need to restart the server.

    After recompiling a class in the application, the userSession.getAuthorityType() returns the valid String (eg. "UserA"), but calling userSession.getUserSubscription() returns a null.

    I'll give it a try to make the custom classes serializable. After testing, I'll provide an update.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    63
    Rep Power
    16
    Having the custom classes stored in the session as serializable did the trick. Thanks so much for the tip.

IMN logo majestic logo threadwatch logo seochat tools logo