gfelmer
gfelmer

Reputation: 31

getThreadLocalRequest().getSession(false) always null

i have some problems trying code login+cookies .. when the user is login i create the session

getThreadLocalRequest().getSession(true)

and when i want to check if session its still alive always return null

HttpSession session = getThreadLocalRequest().getSession(false);    
        if (session != null) {
           }

When i check the HttpRequest (when i check session alive) this have

Cookie: JSESSIONID=a1042a8e-9ebc-45b8-a3d8-12161885be96

and the cookie is ok.

I use Eclipse+Development mode

Server side code :

public String login(String rut, String pass) throws AuthenticationException {
    //if UserPassMatch ...

session = this.getThreadLocalRequest().getSession(true);
    //set user bean 
session.setAttribute("beanSession", us);

HttpServletResponse response = getThreadLocalResponse();
Cookie usernameCookie = new Cookie("JSESSIONID", us.getSessionID());
usernameCookie.setPath("/");
usernameCookie.setMaxAge(60 * 60 ); //1 hora
response.addCookie(usernameCookie); 


}

@Override
public String checkIfSessionStillActive(String token) { 

HttpServletRequest request = getThreadLocalRequest();   
    //Here ALWAYS return null   
HttpSession session = request.getSession(false);

if (session != null) {
        //check sessionId and search User Bean 
}

    return token;
}

From client side nothing special just call checkIfSessionStillActive for check if session exists and go throw the token or go to login if it's not. When the user is logged in still return session NULL. I use MVP pattern and i call from AppController and i use the same rpcService. At once the user login i check session and its exists, but if i call checkIfSessionStillActive this not found any session. Really i read a lot of code and what i found its almost the same thing, ¿can any help me?

Upvotes: 0

Views: 2599

Answers (2)

user558190
user558190

Reputation: 51

Are you using a subclass extending RemoteServiceServlet and calling on a object created somewhere else (for example in spring context) and have extended RemoteServiceServlet? If yes following will solve your problem

For each request a new instance of RemoteServiceServlet is created. Problem is that the thread local variables defined in super class of RemoteServiceServlet are not static, hence for each object you have different variable. When ever you process call in above scenario, your request response thread local variables are initialized for the object which receives but it does not sets thing for object on which you are invoking a method.

I used a workaround by creating a Class with static threadlocal varrible and set values before invoking on second object. Now swcond object can also access them.

Upvotes: 1

Neil Coffey
Neil Coffey

Reputation: 21815

Have you tried using setMaxInactiveInterval() rather than messing with the cookie directly?

Upvotes: 0

Related Questions