简体   繁体   English

无法在java servlet中获取会话

[英]Can't get the session in java servlet

I am using servlets for the first time but I made a lot of progress. 我是第一次使用servlet但是我取得了很大的进步。 My servlets are working well. 我的servlet运行良好。 So I decided to put an authentication mechanism, which creates a session, if users give the right password and id's. 所以我决定建立一个认证机制,如果用户提供正确的密码和id,就会创建一个会话。 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. 正如我之前提到的,我的一个servlet专门用于登录。如果密码正确,则创建会话(我不在会话中存储任何对象/数据),并通知客户端(remoteUser)密码被接受并且会话是创建。 What client does is to reach any other servlet in the same application. 客户端做的是访问同一应用程序中的任何其他servlet。 Other servlets get the session to check if it is created and valid (not timed out). 其他servlet获取会话以检查它是否已创建且有效(未超时)。 For that purpose in those other servlets I get the session with: 为此,在其他servlet中,我得到了以下会话:

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. 但是这会返回null。 So I have tried: 所以我试过了:

HttpSession session = req.getSession();

And checked with session.isNew(); 并使用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(); 因此,我在登录servlet中创建的会话无法使用req.getSession()调用; in another servlet. 在另一个servlet中。

PS: When session is created in login servlet: session.setMaxInactiveInterval(300); PS:在登录servlet中创建会话时:session.setMaxInactiveInterval(300); //5 minutes //5分钟

Thanks a lot for any response! 非常感谢您的回复!

When using Google App Engine, you have to specifically enable session support. 使用Google App Engine时,您必须专门启用会话支持。 See http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions . 请参阅http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM