【问题标题】:session is not releasing in servlets会话未在 servlet 中释放
【发布时间】:2012-06-11 02:51:28
【问题描述】:

我正在使用servlet制作我的网站并在释放会话时遇到问题,请检查并找出错误:

login.java :
     ResultSet res=smt.executeQuery("Select * from admin where name='"+user+"' and password='"+password+"'");
    while(res.next())
    {
        String name=res.getString("name");
        String pwd=res.getString("password");
    if(category.equals("Admin"))
    {
        if(name.equals(user) && pwd.equals(password))
        {
                           ServletContext context=request.getServletContext();
                           context.setAttribute("name", name);
                            HttpSession session=request.getSession();
            session.setAttribute("name", name);
            RequestDispatcher view=request.getRequestDispatcher("welcome_admin.jsp");
            view.forward(request, response);
        }
        else
        {
            response.sendRedirect("index.jsp");
        }
    }

logout.jsp 是:

   <body>
    <%

    session.removeAttribute("name");

    session.invalidate();



    %>
    <b>
        <%
        RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
        rd.include(request, response);
        %>
    </b >
</body>

但是会话没有释放,如果我点击后退按钮,它会再次将我重定向到页面!有人请帮帮我!

【问题讨论】:

  • 建议:写一个过滤器来检查会话属性。

标签: java jsp session servlets


【解决方案1】:

那是因为你的浏览器已经缓存了那个页面,它不再来自服务器了,你需要instruct your browser not to cache pages by adding a Filter

【讨论】:

  • 那么有什么方法可以通过一些编码来防止这种情况发生吗??
  • 你检查过答案中的链接吗?
【解决方案2】:

问题在于缓存。

一种解决方法是将其添加到您的 servlet:

Java servlet how to disable caching of page

// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate");

// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多