I have a web site where I have a login form. If the login is successfull I put the object user into the session and I go in another page (in which I need the session attributes) in this way:
我有一个网站,我有一个登录表单。如果登录成功,我将对象用户放入会话中,然后以这种方式进入另一个页面(我需要会话属性):
-Java Servlet-
request.getSession(true).setAttribute("utente", u);
request.getSession(true).setAttribute("abbonamento", a);
request.getRequestDispatcher("HomeUtente.jsp").forward(request, response);
-Jsp page-
<%
Utente u = null;
Abbonamento a = null;
try {
u = (Utente) session.getAttribute("utente");
a = (Abbonamento) session.getAttribute("abbonamento");
} catch (Exception e) {
a = null;
u = null;
e.printStackTrace();
}
%>
Now, If I do this once it's ok, but If I refresh the page it seems that the session will be deleted.
现在,如果我这样做一旦没问题,但如果我刷新页面似乎会删除会话。
I guess this because when I refresh the page (in debug mode) a and u will be both null.
我想这是因为当我刷新页面时(在调试模式下)a和u都将为null。
Any idea?
1 个解决方案
#1
-1
Java Session -
Java会话 -
HttpSession getSession(boolean create)
-
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.
返回与此请求关联的当前HttpSession,如果没有当前会话且create为true,则返回新会话。
So in your program as you are using -
所以在您使用的程序中 -
request.getSession(true)
-
it is always returning a new session and previous session variables are deleted.
它始终返回一个新会话,并删除以前的会话变量。
also you can view the following link for better understanding -
您也可以查看以下链接以便更好地理解 -
How to check if session exists or not?
如何检查会话是否存在?
#1
-1
Java Session -
Java会话 -
HttpSession getSession(boolean create)
-
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.
返回与此请求关联的当前HttpSession,如果没有当前会话且create为true,则返回新会话。
So in your program as you are using -
所以在您使用的程序中 -
request.getSession(true)
-
it is always returning a new session and previous session variables are deleted.
它始终返回一个新会话,并删除以前的会话变量。
also you can view the following link for better understanding -
您也可以查看以下链接以便更好地理解 -
How to check if session exists or not?
如何检查会话是否存在?