页面刷新后删除了Java会话 [英] Java session deleted after page refresh

查看:55
本文介绍了页面刷新后删除了Java会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,我有一个登录表格.如果登录成功,则将对象用户放入会话中,然后以这种方式进入另一个页面(需要会话属性):

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-

-Java Servlet-

 request.getSession(true).setAttribute("utente", u);
 request.getSession(true).setAttribute("abbonamento", a);
 request.getRequestDispatcher("HomeUtente.jsp").forward(request, response);

-Jsp页面-

    <%
    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.

我猜是因为在刷新页面(在调试模式下)时,a和u都为空.

I guess this because when I refresh the page (in debug mode) a and u will be both null.

有什么主意吗?

推荐答案

Java会话-

HttpSession getSession(boolean create)-

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

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

因此,在您使用程序的过程中-

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 -

如何检查会话是否存在?

这篇关于页面刷新后删除了Java会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆