在 Java 中获取当前会话对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3369127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:38:51  来源:igfitidea点击:

Getting current session object in Java

javasessionservlets

提问by Manjoor

Can I access session object from within a function where request object is not present?

我可以从不存在请求对象的函数中访问会话对象吗?

I know in Java we access session like this:

我知道在 Java 中我们像这样访问会话:

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

我有一个 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
       }

}

采纳答案by Jon Skeet

It depends what you mean by "when request object is not present". You couldhave 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 sessioninstead of the current requestin the thread-local variable.)

这取决于您所说的“当请求对象不存在时”是什么意思。你可以有一个线程局部变量,它在 servlet 的早期或你正在运行的任何东西中设置“当前对该线程的请求”(你还没有说清楚)。然后您可以从任何地方获取“此线程中的当前请求”,并以这种方式获取会话。(或者沿着相同的路线,您可以在线程局部变量中设置当前会话而不是当前请求。)

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.

不过它并不是非常漂亮,如果你想在不同的线程上做一些事情,你会遇到问题。将请求或会话作为依赖项向下传递通常更清晰。

回答by sohilv

passing request object is the solution,

传递请求对象是解决方案,

public class ABC extends HttpServlet
{
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {
             ..........
             ...........
        myFun1(x,y,z,req);  
    }

      private void myFun1(int x, int y,long z,HttpServletRequest req)
      {
            .........
            myFun2(a,b,req);    
       }

      private void myFun2(int a,String b,HttpServletRequest req)
      {
            .........

            //      Need Session here
       }

}

回答by cherouvim

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.

不过一般来说,如果您想从请求对象不可用的地方访问 http 会话,那么您的应用程序可能存在设计问题,您需要重新考虑关注点、分层等。

回答by Alasdair

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

如果您在应用程序中实现JSF技术,您可以通过调用访问所有内容;

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.

然而,仅仅为了这个目的实现 JSF 可能有点过分,因为您可能必须在配置和表示层中进行大量更改以适应它。

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.

也就是说,如果您决定走这条路,您也可以考虑使用MyFaces,对于在其应用程序中使用 JSF 的人来说,这是一个很棒的 AP​​I。

回答by Pravin Kumar singh

We have to configure one listener of type HttpSessionListenerits method will be called at session creation time and distraction time. If you us this by preparing the object of this class you can get the Session object simply...

我们必须配置一个监听器类型,HttpSessionListener它的方法将在会话创建时间和分心时间被调用。如果你通过准备这个类的对象来使用它,你可以简单地获得 Session 对象......