This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Pass session from JSP to servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Pass session from JSP to servlet?" Watch "Pass session from JSP to servlet?" New topic
Author

Pass session from JSP to servlet?

Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
I've been scouring docs, tutorials and forums for a few hours now and can't find out the answer. Is there a way to pass a session from a jsp to a servlet? I have the following setup so far:

index.jsp has a form for username/pw and an action(POST) to send the attributes to a Login servlet.

The login servlet logs them in and with forward(req,res) sends them to welcome.jsp.

Welcome.jsp has a form for users to enter search params which are then sent via a form action(using GET) to Search servlet.

At this point the session is gone and so is the connection to the database and user info.

Any solution to this? Is my design trash? What would you recommend? Thanks.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

At this point the session is gone

What do you mean by "gone" ? It doesn't evaporate
In you login servlet, do you call getSession() ?


[My Blog]
All roads lead to JavaRanch
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
Yes, I call this in Login:

HttpSession userSession = req.getSession(true);

But when I get to the second servlet (ie after Login uses forward(req,res) to welcome.jsp and welcome.jsp calls Search servlet from it's form), there doesn't seem to be a way to get the session.

requst is new so request.getSession() won't work right?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

requst is new so request.getSession() won't work right?

Wrong. New request doesn't mean new session. The session created when getSession(true) was called will be available for all requests.
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
Well, I don't know what's happening. I set up a session and db connection in Login servlet and then do this:

Then in welcome.jsp we have this:

Then in Search servlet I've got this, but I can't get the connection object or anything else from the session. req.getSession() seems to give us a new session and req.getSession(false) doesn't seem to do anything to help. Here's some of the code from Search.java:


Here's the stack trace:


Here is line 86 and 87:
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

HttpSession userSession = req.getSession(true);


Why do you get a pre-existing session in login servlet. If a user visits for the first time then this servlet will return a null.

Instead use

HttpSession userSession = req.getSession();

in your login servlet.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
If I don't put (true) in getSession in Login servlet, the user can't log in. Probably the way the code is written. Either way, that doesn't seem to be the problem in Search servlet does it?
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Mind posting your login servlet code.
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
I can't post the whole thing, but here is all the relevant lines (pretty much the whole thing):
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
You cannot bind an interger to a session.


userSession.setAttribute("userId", userId);


Here userId is an int.
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
Do you think this is the problem? I think it autoboxes to a string or something because userId is available in the jsp which is called by login:

<!-- Hi <%= session.getAttribute("userId") %> -->

shows as:

<!-- Hi 490002 -->

when I view source on the page.

- as I said, the main problem I'm having is that the session is gone when I call a second servlet from a form in a jsp (which was called by Login servlet).
Sarath Mohan
Ranch Hand

Joined: Mar 17, 2001
Posts: 213
As you said your session is getting recreated. where exactly I couldn't figure out. try with .getSession() and see why is it becoming null...

You can opt for a better design where we can avoid db connection in Session.


Sarath Mohan
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
I'd be up for hearing about a better design, but I couldn't figure out a better one. I'm not confident I could set up JNDI and even if I did I might still have this problem if I was using multiple servlets. Maybe I could do it in a way where the servlet doesn't need anything from the session and just gets information via forms, but can I pass an arraylist from the servlet to a jsp in the response without disturbing the user's existing session (since the servlet won't have access to it)?
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Originally posted by Vishnu Prakash:
You cannot bind an interger to a session.



Here userId is an int.


You can when using a 1.5 compliant JVM, autoboxing would turn it into an Integer.


42
Tom Tillinghast
Greenhorn

Joined: Apr 29, 2006
Posts: 4
Lido, your connection is never set to anything other than null. So it should be no surprise that your connection is null in
line 86 of Search.java:
Connection con = (Connection) userSession.getAttribute("connection");

In your Login class:
Connection con = null;
int userId;
try {
userId = validateUser(email, password, con);//does NOT modify con
}
catch (Exception e){
userId = -1;
}
RequestDispatcher rd;
// if uName is negative .. user is not authorized. HttpSession userSession = req.getSession();
userSession.setAttribute("connection", con);//con is still null here

Inside of validateUser(), you set the LOCAL variable con -- INSIDE THAT FUNCTION, but that has no relevance to the outside variable con that you see above. You're not modifying it (it's null anyway so you couldn't), you're just setting the FUNCTION'S con variable to another reference.
You would need to pass in a structure containing a connection object
and then set that structure's connection object inside the function.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5

That looks like you are using the "Invoker" servlet - this causes all sorts of problems and confusion. See this ranch faq.
Sessions can only be shared between JSP and servlets in the same "web application" - you need to be sure that your URLs for JSP and servlets all point to the same web application.
Bill
Lido Collins
Greenhorn

Joined: Apr 27, 2006
Posts: 20
Tom,

Thanks! I did not notice that. I solved the problem by just getting a new connection in the Search servlet, but I will go back and see if I can share the connection that way.

William,

Thanks for the link to the FAQ. I did not uncomment the invoker so I think I'm ok there, but the faq did point out that the web.xml couplets for the servlets should be grouped together by type rather than in pairs. Although mine seems to be working as is, I will change the web.xml to be grouped by type rather than how they are now (in pairs by servlet).

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass session from JSP to servlet?
 
Similar Threads
Will tomcat creates a new session for every forward to a jsp page
interceptors
Struts validate - how-to repopulate lists, bean:write, etc. after validation fails
Confused - MVC, JSP, Servlet interaction
Maintaining sessions