wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpSession" Watch "HttpSession" New topic
Author

HttpSession

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
Greenhorn

Joined: Mar 05, 2003
Posts: 8
Request will always invoke doGET or doPOST method of servlet.so, 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: HttpSession
 
Similar Threads
using servlet to create a html page with embedded Image
Loosing sessions between pages
About Servlet theadsafe issue.
ssession doesn't seem to be storing/holding object on server-works local
Sync block and variable references