Can I access session object from within a function where request object is not present?
I know in Java we access session like this:
HttpSession session = request.getSession(true);
But what if we want to access session when request object is not present?
Is it possible? is there an alternate way to get session object?
Edit
I have a servlet
public class ABC extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
..........
...........
myFun1(x,y,z);
}
private void myFun1(int x, int y,long z)
{
.........
myFun2(a,b);
}
private void myFun2(int a,String b)
{
.........
// Need Session here
}
}
It depends what you mean by “when request object is not present”. You could have a thread-local variable which sets the “current request for this thread” early in the servlet or whatever you’re running (you haven’t made it clear). Then you could get at “the current request in this thread” from anywhere, and get the session that way. (Or along the same lines, you could set the current session instead of the current request in the thread-local variable.)
It’s not terribly pretty though, and you have problems if you want to do something on a different thread. Passing the request or session down as a dependency is generally cleaner.