File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes HttpSession Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpSession" Watch "HttpSession" New topic


Mike Yu
Ranch Hand

Joined: Nov 17, 2001
Posts: 175
In a Servlet, when an attribute is set to a Session object, we can use it later in the same session but different request,for example,
public class MyServlet extends HttpServlet{
//some code;
//in a method,
HttpSession mySession= request.getSession(true);
mySession.setAttribute("attr1", obj1);

//other code;
If I do not use a Servlet, but I use a regular class, for example,
import javax.servlet.http.*;
public class MyClass{
//some code;
public void myMethod(HttpRequest request){
HttpSession mySession = request.getSession(true);
mySession.setAttribute("attr1", obj1);
Will myMethod in MyClass do the same thing as that in MyServlet?
[ March 05, 2003: Message edited by: Mike Yu ]

Thanks,<br />Mike
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
Seems reasonable, presuming that the second request comes from the same user.
Session-scope attributes are available to all requests from a specific user.
Have you tried it?
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
It will surely work.

- Varun
Rajni Kumar

Joined: Mar 05, 2003
Posts: 8
Request will always invoke doGET or doPOST method of, if u create instance of your class inside the doGET or doPOST method and call ur method ,then it will surely work.
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
Mark is correct.Session objects will serve multiple request from the same user.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
As long as the class is running in the same JVM as the servlet, this should work.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: HttpSession
It's not a secret anymore!