是request.getSession()保存用户信息,然后invalidate();进行注销出现问题

FiShelly_ 2015-05-11 10:35:19
前提:
使用了hibernate3.6的框架来访问数据库
问题:
用户登录之后,servlet用request.getSession()的形式保存了用户的登录信息,如账号,密码,地址,优先级等,然后使用request.getSession().invalidate()来注销用户信息。同时跳转到登录页面,但是当我点击退出之后,是跳转到登录页面了,可是当我在浏览器点击后退的时候,返回上一个页面,然后点击刷新还能进行旧页面的访问,还能够像已经登录的一样,进行增删改查的操作。然后我在servlet的登录方法里面进行调试输出,发现当点击刷新之后,String loginId = request.getParameter("lg_id");
String password = request.getParameter("lg_password");还能够取到值。所以才导致无法注销。所以问问各位大神,为什么会造成注销之后,在旧页面按刷新还能够取到用户名等信息?
登录代码:

public void adminLogin(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pages = "loginUI/login.jsp";
String loginId = request.getParameter("lg_id");
String password = request.getParameter("lg_password");
System.out.println(loginId + password);
AdminUsers admin = new AdminUsers();
admin.setLoginId(loginId);
admin.setPassword(new MD5Code().getMD5ofStr(password));
String flag = "0";
IAdminUsersDAO adminDao = DaoFactory.getIAdminUsersDaoInstance();
try {
List<AdminUsers> list = adminDao.findLogin(admin);
if(!list.isEmpty()) {
admin = list.get(0);
request.getSession().setAttribute("admin", admin) ;
flag = "1";
pages = "modelOne/model1_admin_index.jsp";
} else {
request.setAttribute("info", MessageUtil.get("adminlogin.false"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
adminDao.close();
}
request.setAttribute("flag", flag);
request.getRequestDispatcher(pages).forward(request, response);
}


注销代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pages = "loginUI/login.jsp";
request.getSession().invalidate();
System.out.println(1132131);
request.getRequestDispatcher(pages).forward(request, response);
}

顺便在问问大神们,在已经登录的情况下,按浏览器的刷新按钮,为什么servlet会取得取得loginId以及password。
...全文
1877 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
很倔也很天真 2018-11-20
  • 打赏
  • 举报
回复
9楼那个 如同
Integer[] nums = new Integer[]{1,2,3};
Integer a = nums[0];
nums[0] = 0;
System.out.println(nums[0]);
System.out.println(a);
-----------
你说 a 它为啥会为null呢. 当然是你引用了它的对象
至于楼主那个 注销是注销session,不是request,只要你没有redirect他的值就还在
清雨♎ 2018-10-07
  • 打赏
  • 举报
回复
楼主解决没有,我遇到了同样的问题,也是用的hibernate框架(但是好像跟这个没关系),用户点击退出登录以后再返回刷新页面,我判断了如果session为空跳转login.jsp页面,但是没跳,依然在index.jsp页面,当前登录的用户信息也还在,测试了一下,request.getSession().removeAttribute("userSession");之后还会打印session中的信息
/**
* 退出登录
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserInfo ui = (UserInfo)request.getSession().getAttribute("userSession");
System.out.println(ui.getUser_code());//输出zhangsan,是正确的
request.getSession().removeAttribute("userSession");
System.out.println(ui.getUser_code());//这里也输出zhangsan,为毛还会输出,不应该为null么
response.sendRedirect("login.jsp");
}
一大三千 2015-05-13
  • 打赏
  • 举报
回复
引用 6 楼 FiShelly_ 的回复:
[quote=引用 4 楼 u011726984 的回复:] 试试这样行不行


public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pages = "loginUI/login.jsp";
        request.getSession().removeAttribute("admin");//加上这句
        request.getRequestDispatcher(pages).forward(request, response);
    }
同样不行,已经尝试过了,,,[/quote] 那就再改成下面这样吧!

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pages = "loginUI/login.jsp";
        if (request.getSession().getAttribute("admin") !=null) {
              request.getSession().removeAttribute("admin");//加上这句
              request.getSession(false);//再加上这句,加上这句还不行,就在你的登陆代码那边做判断。
        }
      
        request.getRequestDispatcher(pages).forward(request, response);
    }
五道口.香锅 2015-05-12
  • 打赏
  • 举报
回复
invalidate()只是取消了与sessionID的关联关系,session还存在,在invalidate()之前最好先执行removeAttribute,删除所有的attribute,这样后退以后再刷新应该不会有值了
FiShelly_ 2015-05-12
  • 打赏
  • 举报
回复
引用 5 楼 tanak 的回复:
你将invalidate 的注销方式 改成 request.getSession().removeAttribute("uid"); 就ok
试过了,,,不行~还是一样问题
FiShelly_ 2015-05-12
  • 打赏
  • 举报
回复
引用 4 楼 u011726984 的回复:
试试这样行不行


public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pages = "loginUI/login.jsp";
        request.getSession().removeAttribute("admin");//加上这句
        request.getRequestDispatcher(pages).forward(request, response);
    }
同样不行,已经尝试过了,,,
张师旗 2015-05-12
  • 打赏
  • 举报
回复
百度下 应该是你没有安顺序close request session 貌似都得关闭 清除
FiShelly_ 2015-05-12
  • 打赏
  • 举报
回复
为嘛没人捏,,,求大神指点~
王念晨 2015-05-12
  • 打赏
  • 举报
回复
你将invalidate 的注销方式 改成 request.getSession().removeAttribute("uid"); 就ok
一大三千 2015-05-12
  • 打赏
  • 举报
回复
试试这样行不行


public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pages = "loginUI/login.jsp";
        request.getSession().removeAttribute("admin");//加上这句
        request.getRequestDispatcher(pages).forward(request, response);
    }

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧