Session is NOT working - GAE/J

2019-08-03 03:39发布

I am fairly new to GAE/J development and trying to use GAE Seesion. Somehow when i try to retrieve value stored in session, its coming back null only. Here is what i have.

I have loginservlet where i am setting String variable to session.

HttpSession hs=req.getSession(true);
if(hs.isNew()){
hs.setAttribute("logininfo", "mylogininfo");
}

then I try to retrieve this information inside JSP page as below -

<%
HttpSession hs = request.getSession(false);
String logininfo = (String) hs.getAttribute("logininfo");
%>

I am always getting "logininfo" string as null - not sure why.

I have session enabled inside appengine-web.xml

<sessions-enabled>true</sessions-enabled>

Above is just a trial example I am trying to run, i really want to pass 'accessToken' to JSP page from servlet for facebook login.

1条回答
干净又极端
2楼-- · 2019-08-03 04:34

I don't know in what order your pages are called, but the most probable culprit is the use of isNew() method.

Sessions are established when server sends a cookie to users browser. This happens on first request of any page, authorized or non-authorized.

So, isNew() is true only on the first request to any of your pages, while user has not yet received any cookie. On any consecutive request (even days later, depends on Cookie Expiration: setting in GAE admin) it will be false.

查看更多
登录 后发表回答