Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get the session in java servlet

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!

like image 827
Genom Avatar asked Jul 29 '11 13:07

Genom


People also ask

Which code is used GET session in servlet?

request. getSession() is used to get a HTTP Session object in servlets.

How does session work in Tomcat?

In session management, Tomcat creates a session id whenever client's first request gets to the server (However, other servlet containers may behave differently). Then it inserts this session id into a cookie with a name JSESSIONID and sends along with the response.

Why session is used in servlet?

HTTP is a “stateless” protocol, which means that each time a client requests a Web page, the client establishes a new connection with the Web server, and the server does not retain track of prior requests. The conversion of a user over a period of time is referred to as a session.


1 Answers

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.

like image 90
ddso Avatar answered Sep 28 '22 08:09

ddso