开发者

Problem with request.getSession

I have a servlet LogMeOut.java where I am writing the following code:

HttpSession session = request.getSession(false);
if (session != null){
   //statements to delete my cookies //
   session.invalidate();
}

My problem is when I am calling the LogMeOut for the first time from a jsp, it gives a request object with no session. So my session.invalidate is not working. But if I go back to my jsp and then call the same LogMeOut, this time the request object contain开发者_运维百科s the correct session and it works. Any idea why? Or where should I start looking?


If you call getSession(false) on a request with no existing session, then it will return null. This is expected, documented behaviour.

If you call getSession(true) or just getSession(), then a session will be created if none exists.

Executing a JSP will generally also create a session automatically, so the next time your code is executed, a session will be present.

However, if all you want to do is invalidate an existing session, then your code looks fine to me - no need to create a session just to immediately invalidate it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜