Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current session object in Java

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
       }

}
like image 940
Manjoor Avatar asked Jul 30 '10 06:07

Manjoor


People also ask

How do you find the current HTTP session object?

How to get the HttpSession object ? The HttpServletRequest interface provides two methods to get the object of HttpSession: public HttpSession getSession():Returns the current session associated with this request, or if the request does not have a session, creates one.

What is session object in Java?

The session object is used to track a client session between client requests. JSP makes use of the servlet provided HttpSession Interface. This interface provides a way to identify a user across.

How can get a session object in Spring?

Getting HttpSession Object in Spring Controller is very easy . Just Put it as a method parameter in controller method and Spring will automatically inject it . There is another approach where we create Session scoped Controller . This Controller get created for each session and controller object is stored in session.

How do I find session attributes?

The client can create session attributes in a request by calling either the PostContent or the PostText operation with the sessionAttributes field set to a value. A Lambda function can create a session attribute in a response.


3 Answers

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.

like image 175
Jon Skeet Avatar answered Nov 15 '22 18:11

Jon Skeet


Generally speaking though, if you want to access the http session from a place where the request object is not available then you may have design problems in your application and you need to rethink about concerns, layering etc.

like image 40
cherouvim Avatar answered Nov 15 '22 17:11

cherouvim


If you implement JSF technology in to your application you can access everything by calling;

FacesContext.getCurrentInstance();

Because this is a singleton instance retrieved, you can access it anywhere in your presentation layer.

However, implementing JSF is probably slightly overkill just for this purpose as you'll probably have to make a lot of changes in your configuration and presentation layers to accommodate for it.

That said, if you do decide to go down that road, you can also think about using MyFaces, which is a fantastic API for those using JSF in their applications.

like image 35
Alasdair Avatar answered Nov 15 '22 19:11

Alasdair