java 第二次访问时会话变量为空

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

Session variable is null when accessed second time

javajspsessionservlets

提问by NewBoyInCity

I am new to this forum. I am designing a JSP-Servlet application and using session variables to store objects. Problem is, when I access the session variables for second time it returns null. Here is the flow of my app -

我是这个论坛的新手。我正在设计一个 JSP-Servlet 应用程序并使用会话变量来存储对象。问题是,当我第二次访问会话变量时,它返回 null。这是我的应用程序的流程 -

I have a jsp page with mulitple link buttons. When user clicks on any button a servlet is called with respective parameters. eg 1 is passed if button1 is clicked, 2 if button2 is clicked,etc.

我有一个带有多个链接按钮的 jsp 页面。当用户单击任何按钮时,将使用相应的参数调用 servlet。例如,如果单击 button1,则传递 1,如果单击 button2,则传递 2,等等。

Servlet gets an arraylist from session variable which is already created earlier and depending on requested parameter gets the data from arraylist, processes it and sends response to another jsp say jsp2.

Servlet 从之前已经创建的会话变量中获取一个数组列表,并根据请求的参数从数组列表中获取数据,处理它并将响应发送到另一个 jsp,比如 jsp2。

jsp2 also has similar link buttons and should do the same task. When the same Servlet is called from jsp2 or even if the page is refreshed, the session variable is null this time.

jsp2 也有类似的链接按钮,应该做同样的任务。当jsp2调用同一个Servlet或者刷新页面时,这次session变量为null。

In my web.xml file under the session-config tag I have set the timeout to -1 so that session never expires.

在 session-config 标记下的 web.xml 文件中,我已将超时设置为 -1,以便会话永不过期。

<session-config>
      <session-timeout>-1</session-timeout> 
   </session-config>

In the servlet I get the session variable as-

在 servlet 中,我将会话变量作为-

HttpSession session = request.getSession(false);

ArrayList<String> list = new ArrayList<String>();
list = (ArrayList<String>)session.getAttribute("mylist");

When the servlet is called second time or even if the page is refreshed the list is null. I put few lines to check if session is valid-and it prints the second time.

当 servlet 被第二次调用时或者即使页面被刷新,列表为空。我放了几行来检查会话是否有效 - 它第二次打印。

if(!request.isRequestedSessionIdValid()) {
 System.out.println("Session is Expired !!");
}

and the session.getAttribute("mylist")returns NullPointerException.

session.getAttribute("mylist")回报NullPointerException

I am sure that I have not used session.invalidate()anywhere by chance to end the session. I dont understand what is going wrong..can someone can explain me ?

我确信我没有session.invalidate()偶然使用任何地方来结束会话。我不明白出了什么问题..有人可以解释我吗?

Many thanks in advance.

提前谢谢了。

回答by Satheesh Cheveri

You would need to try with getSession(true)for the first time or getSession(). False argument will not return session if the session doesn't exist. Only for the first time you would need to supply true in to the getSessioncall.

您需要getSession(true)第一次尝试使用或getSession()。如果会话不存在,则 False 参数将不会返回会话。只有第一次你需要在getSession调用中提供 true 。

HttpSession session = request.getSession(true);

According to servlet specification,

根据servlet规范,

HttpSession getSession(boolean create)

Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.

If create is false and the request has no valid HttpSession, this method returns null.

To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.

HttpSession getSession(布尔创建)

返回与此请求关联的当前 HttpSession,或者,如果没有当前会话并且 create 为 true,则返回一个新会话。

如果 create 为 false 并且请求没有有效的 HttpSession,则此方法返回 null。

为确保会话得到正确维护,您必须在提交响应之前调用此方法。如果容器使用 cookie 来维护会话完整性,并在响应提交时被要求创建新会话,则会抛出 IllegalStateException。

Edit - Session from session id

编辑 - 来自会话 ID 的会话

 HttpSessionContext sc=request.getSession().getSessionContext();
    HttpSession session=sc.getSession(session_id);

Note- This method is deprecated now due to security reason.

注意 - 由于安全原因,现在不推荐使用此方法。

回答by Mohsin

Try sending the response to some other jsp page and see if you are able to access session variable from there. If yes then there is problem in your current jsp. Certainly the session is getting expired somewhere for sure otherwise request.getSession() will definitely return the previously created session.

尝试将响应发送到其他一些 jsp 页面,看看您是否能够从那里访问会话变量。如果是,那么您当前的jsp 中存在问题。当然会话肯定会在某处过期,否则 request.getSession() 肯定会返回之前创建的会话。

回答by Adrian Adamczyk

Set timeout to 0, not -1if you want unlimited session time.

将超时设置为0,而不是-1如果您想要无限的会话时间。

回答by Olaf Dietsche

You need to set the attribute somewhere, before you can retrieve it:

您需要在某处设置属性,然后才能检索它:

session.setAttribute("mylist", list);