Java 如何检查会话是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2818251/
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 13:13:51  来源:igfitidea点击:

How to check if session exists or not?

javasessionservlets

提问by sarah

I am creating the session using

我正在使用创建会话

HttpSession session = request.getSession();

Before creating session I want to check if it exists or not. How would I do this?

在创建会话之前,我想检查它是否存在。我该怎么做?

回答by aioobe

There is a function request.getSession(boolean create)

有一个功能 request.getSession(boolean create)

Parameters:
    create- true to create a new session for this request if necessary; false to return null if there's no current session

参数:
    create- 必要时为该请求创建一个新会话;如果没有当前会话,则返回 null

Thus, you can simply pass falseto tell the getSessionto return null if the session does not exist.

因此,如果会话不存在,您可以简单地通过false告诉getSession返回 null。

回答by rakke

HttpSession session = request.getSession(true); if (session.isNew()) { ...do something } else { ...do something else }

HttpSession session = request.getSession(true); if (session.isNew()) { ...做某事} else { ...做其他事}

the .getSession(true)tells java to create a new session if none exists.

.getSession(true)如果不存在,则告诉 java 创建一个新会话。

you might of course also do:

你当然也可以这样做:

if(request.getSession(false) != null){
    HttpSession session = request.getSession();
}

have a look at: http://java.sun.com/javaee/6/docs/api/javax/servlet/http/HttpServletRequest.html

看看:http: //java.sun.com/javaee/6/docs/api/javax/servlet/http/HttpServletRequest.html

cheers, J?rgen

干杯,J?rgen

回答by BalusC

If you want to check this beforecreating, then do so:

如果您想创建之前检查这一点,请执行以下操作:

HttpSession session = request.getSession(false);
if (session == null) {
    // Not created yet. Now do so yourself.
    session = request.getSession();
} else {
    // Already created.
}

If you don't care about checking this aftercreating, then you can also do so:

如果你不在乎创建检查这个,那么你也可以这样做:

HttpSession session = request.getSession();
if (session.isNew()) {
    // Freshly created.
} else {
    // Already created.
}

That saves a line and a boolean. The request.getSession()does the same as request.getSession(true).

这样可以节省一行和一个boolean. 在request.getSession()不一样的request.getSession(true)

回答by Ahmed Hassan Hegazy

if(null == session.getAttribute("name")){  
  // User is not logged in.  
}else{  
  // User IS logged in.  
}  

回答by Olav Gr?n?s Gjerde

I would like to add that if you create a new session for every new user connecting to your website then your performance will take a hard hit. Use request.getSession(false) to check if a user has a session. With this method you don't create a new session if you're going to render a view based on if a user is authenticated or not.

我想补充一点,如果您为每个连接到您网站的新用户创建一个新会话,那么您的性能将受到严重打击。使用 request.getSession(false) 检查用户是否有会话。如果您要根据用户是否通过身份验证来呈现视图,则使用此方法无需创建新会话。