The moose likes Servlets and the fly likes per-session instance of servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "per-session instance of servlet?" Watch "per-session instance of servlet?" New topic

per-session instance of servlet?

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I read in another post by William Brogden that there's only one instance of a servlet, not one per user session like with beans. Maybe I read that wrong, but if that's true, how do I access user info from that servlet?
For example, I've got a form that submits to a JSP, and the JSP can use the usebean tag and pull info from the user's account info bean to find out what info the user has access to. I'm trying to convert that form to post to a servlet instead, but in that servlet, I no longer have access to the user's account info bean, and I wouldn't want to just trust the request parameters, because someone may be trying to fool the system into giving them someone else's information...
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
A servlet may have many sessions, a session may have many requests
a user may have one session ( okay, they may be using multiple browsers and have multiple sessions, but only if you let them log in more than once)
if you store the userbean in the session, you'll have access to it from your servlet for the life of the session.
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
So, how do you access those beans from servlets? Is there something like a JSP's usebean tag for servlets?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Why not look at the JavaDocs for HttpSession in the javax.servlet.http package? Or maybe buy a good book on servlets or find a tutorial. Hint - the javabean object will be in the session under the name used in the JSP. Take a look at the servlet code produced by the JSP compiler to see where it happens.
It really doesn't make any sense to try to pick up your knowledge of servlets one question at a time when all these great resources exist.
[ February 18, 2004: Message edited by: William Brogden ]
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I agree. Here's the link:
subject: per-session instance of servlet?
jQuery in Action, 3rd edition