Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass session from JSP to servlet?

 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() ?
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mind posting your login servlet code.
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't post the whole thing, but here is all the relevant lines (pretty much the whole thing):
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot bind an interger to a session.


userSession.setAttribute("userId", userId);


Here userId is an int.
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lido Collins
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Tillinghast
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic