Maintain session when calling servlet from form in a JSP
I have the following set up:
index.jsp calls login servlet from the action tag in a form.
Login servlet handles the login, stores the user info and db connection in the session and uses forward(req,res) to call another jsp.
That jsp has a form where user enters search info and in that form's action tag there is a search servlet. In the search servlet I am unable to access the session.
Is there any way to pass the session to the servlet from that jsp using a form/action?
>In the search servlet I am unable to access the session.
Things to look out for
1. The session may have expired.
2. Does the browser allow cookies?
You should be accessing the session in one of the below two ways.
HttpSession session = request.getSession(false); //handle to existing session; null if no session present.
HttpSession session = request.getSession(true); //handle to existing session; else creates and returns a new session
>Is there any way to pass the session to the servlet from that jsp using >a form/action?
You neednt do any thing special for this. The session if created in the first request, would be available in the second as long as it hasnt expired (and the default expiry is 30 mins unless you have it set it to some other value)
ram.
Thanks Ram,
If that is true, then I must just have some problem in my code. I've read elsewhere that if you go from a jsp to a servlet that the request object is cleared of any attributes from the previous request. I actually have that exact line in my code (in the second servlet) but for some reason, it doesn't retrieve the session info and gives me a null pointer exception when I try to use the connection object stored in the session.
Here's the design:
index.jsp calls> [Login servlet] calls> welcome.jsp calls>[Search servlet]
both servlets are called from a form's action directive so the text input can be passed in the request.
Here's the line(s) from the Search servlet:
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
RequestDispatcher rd;// set up vars and objects for the doPost
ResultSet rs = null;
HttpSession userSession = req.getSession(false);
Connection con = (Connection) userSession.getAttribute("connection");
Statement stmt = con.createStatement();
That last line is where I get the null pointer exception.
>I've read elsewhere that if you go from a jsp to a servlet that the >request object is cleared of any attributes from the previous request.
which is correct. But arent we speaking about session object here? A request object is valid for a request - ie the phase from where the server receieves a hit for a resource upto the point it sends the output for that request.
A session spans multiple requests.
>it doesn't retrieve the session info and gives me a null pointer >exception when I try to use the connection object stored in the session.
Bad bad bad . Why do you store Connection objects in session? Create them when necessary or use a connection pool. Do you for example clean up the connections when the session expires. What if its a 30 minute session and the user hits every say 15 minutes with a request. Why do you need to hold on to the Connection in the intervening interval when the user's session is inactive?
>gives me a null pointer exception when I try to use the connection object stored in the session.
which means that the Connection object is null - not the session object.
>That last line is where I get the null pointer exception.
And that is
Statement stmt = con.createStatement();
?
Same answer as above.
If the session object was null,
userSession.getAttribute("connection");
would have thrown a NPE.
ram.
So do you think this system:
jsp > servlet > jsp > servlet
is ok and the second servlet should have access to the session object?
If that is the case, then I can just try to create a new connection in every servlet.
For now (esp since this is a toy project for school) I will not worry about the robustness and how efficiently the connections are used, I want to just get it working.
> So do you think this system:
>
> jsp > servlet > jsp > servlet
>
> is ok and the second servlet should have access to
> the session object?
>
>
Yep. As I said in the earlier post, you get your session object allright, its just the connection object there which is null. Create a new Connection every time for a new request.
ram.
ram.
Hi guys, first post here...
I'm not a Java developer, just know a few things, can fix a simple code, etc.
It happened that I need to develop login functionality for a website with session handling. I know it sounds crazy as I'm not a developer but please help me, it is a very, vary bad situation for me and I need it done...
Could somebody help me with it? Does anybody have a working code of handling session in login servlet with cookie and using it in other jsps/servlets similar situation as LidoShuffle has?
Please help me. If you guys can not provide me with sample codes - please point me to the place where I can pay for this to be developed for me.
Thanks in advance,
Axel