File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HttpSession

 
Mike Yu
Ranch Hand
Posts: 175
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mark Howard
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1400
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will surely work.
 
Rajni Kumar
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark is correct.Session objects will serve multiple request from the same user.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as the class is running in the same JVM as the servlet, this should work.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic