Results 1 to 2 of 2

Thread: Only one object saved in ServletContext

  1. #1
    Join Date
    Mar 2009
    Posts
    2

    Default Only one object saved in ServletContext

    Helle all.

    Sorry for my bad english : i'm french

    Briefly : when two users use my application, only the last one is saved in ServletContext.

    NB: I use ServletContextAware due to the null pointer exception return by this.getThreadLocalRequest().getSession(). After a long search on web someone tells to use ServletContextAware. I also try to set the session using this method. The result is exactly the same !

    I explain my architecture :
    • I use hibernate + spring for my core library.
    • I use GWT + spring for my web app.


    Users enters the apps by a login form. This form call a rpc service which checks user in database. If user is valid => user saved in session.

    Here is my rpc implementation :
    Code:
    public class SessionServiceImpl extends RemoteServiceServlet 
         implements SessionService, ServletContextAware {
    
        private ServletContext servletContext;
        
        @Override
        public UserProperties checkLogin(UserProperties userToCheck){
    
            UserProperties user = serv.isValidUser(userToCheck);
    
            if (user != null) {
                servletContext.setAttribute("USER", user);
            }
            return user;
        }
    
        @Override
        public UserProperties isSessionAlive() {
            UserProperties bean = (UserProperties) servletContext.getAttribute("USER");
            if ((bean != null) && (bean.getLogin().length() != 0)) {
                return bean;
            }
            return null;
        }
    }

    I test with two diffrents browser Firefox (FF) and Chrome (CH).
    1. FF log with user1
    2. FF check session -> user1 is logged
    3. CH log with user2
    4. CH check session -> user2 is logged
    5. FF check session : user2 is logged


    Can my problem come from web.xml?

    Many thanks for reading !

    Pierre
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2005
    Location
    Mobile, AL
    Posts
    345

    Default

    You should be storing the user on the HttpSession and NOT the ServletContext. The ServletContext is typically a single instance per Servlet container. This explains why when the second user logs in it stomps on the first user's user reference.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •