无法在java servlet中获取会话 [英] Can't get the session in java servlet

查看:105
本文介绍了无法在java servlet中获取会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用servlet,但是我取得了很多进展。我的servlets运行良好。所以我决定把一个认证机制,创建一个会话,如果用户提供正确的密码和ID的。但会议对我来说是全新的。所以我不太关注逻辑,但我已经开始理解了。



正如我之前提到的,我的一个servlet专门用于登录。如果密码正确会话被创建(我不会在会话中存储任何对象/数据),并且通知客户端(remoteUser)密码被接受并且会话被创建。客户端做的是在同一个应用程序中访问其他servlet。其他servlet会检查会话是否已创建并且有效(未超时)。为了这个目的,我在其他servlet中获得会话:

  HttpSession session = req.getSession(false); // false,因为这不是创建会话的地方。会话只能在登录servlet中创建。 

但是这会返回null。所以我试过了:

  HttpSession session = req.getSession(); 

并使用session.isNew()进行检查。我是一个新的会议。所以我在login servlet中创建的会话不能用req.getSession()来调用;在另一个servlet中。



PS:当在登录servlet中创建会话时:session.setMaxInactiveInterval(300); // 5分钟

非常感谢您的回应!

解决方案

使用Google App Engine时,您必须特别启用会话支持。请参阅 http://code.google.com/appengine/docs/ java / config / appconfig.html#Enabling_Sessions


I am using servlets for the first time but I made a lot of progress. My servlets are working well. So I decided to put an authentication mechanism, which creates a session, if users give the right password and id's. But sessions are totally new for me. So I don't quite follow the logic but I have started to understand.

As I mentioned before one of my servlets is dedicated for logging in. If password is correct a session is created (I don't store any object/data in sessions) and client (remoteUser) is notified that the password is accepted and session is created. What client does is to reach any other servlet in the same application. Other servlets get the session to check if it is created and valid (not timed out). For that purpose in those other servlets I get the session with:

HttpSession session = req.getSession(false); //false because this is not the place to create a session. sessions should only be created in the login servlet.

But this returns a null. So I have tried:

HttpSession session = req.getSession();

And checked with session.isNew(); and I it was a new session. So the session I have created in login servlet can't be called with req.getSession(); in another servlet.

PS: When session is created in login servlet: session.setMaxInactiveInterval(300); //5 minutes

Thanks a lot for any response!

解决方案

When using Google App Engine, you have to specifically enable session support. See http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions.

这篇关于无法在java servlet中获取会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆