将登录等信息保存到session中和退出session

做项目时,可能会将某些信息保存在session中,如登录等信息,这样方便在某些页面使用这些保存的信息。

要想保存这些信息,需要创建一个类,该类里面定义需要保存的变量等信息,当登录后就通过new一个该类来保存登录等信息,然后放在session中,需要用到这些信息时直接用例如EL表达式等取出来就OK了。例子如下:

1.保存用户信息的类

  1. <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;">public class WSessionInfo implements java.io.Serializable {<span style="white-space:pre">    </span>  
  2.   
  3.     private String id;// 用户ID  
  4.     private String loginname;// 登录名  
  5.     private String name;// 姓名  
  6.     private String ip;// 用户IP  
  7.     private String userimg; //图片  
  8.     private RegisterUser user;  //用户注册信息      
  9.     private String usertype;//用户类型  
  10.     private String chengsid;//城市编号  
  11.     private String cityname;//城市名  
  12.     private List<String> resourceList;// 用户可以访问的资源地址列表  
  13.     private List<String> resourceAllList;  
  14.       
  15.     public String getId() {  
  16.         return id;  
  17.     }  
  18.   
  19.     public void setId(String id) {  
  20.         this.id = id;  
  21.     }  
  22.     public String getChengsid() {  
  23.         return chengsid;  
  24.     }  
  25.   
  26.     public void setChengsid(String chengsid) {  
  27.         this.chengsid = chengsid;  
  28.     }  
  29. <span style="white-space:pre">    </span>......  
  30. }</span>  
  31.   
  32. </span>  
2.在Controller中保存登录等信息

  1. <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><span style="white-space:pre">   </span>@RequestMapping("/login")  
  2.     @ResponseBody  
  3.     public Json login(Account user, HttpSession session) {  
  4.         ......  
  5.         WSessionInfo sessionInfo = new WSessionInfo();//保存用户信息的类  
  6.         sessionInfo.setId(loginUser.getId());//为类里面的属性赋值,即把登录信息保存在这个类中  
  7.         sessionInfo.setLoginname(loginUser.getLoginname());  
  8.         sessionInfo.setUsertype(loginUser.getUsertype());  
  9.         sessionInfo.setUser(regUser);  
  10.         session.setAttribute(GlobalConstant.SESSION_WAP_INFO, sessionInfo);//将信息保存在session中   
  11.     }  
  12. </span></span>  
3.在页面使用

  1. <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><%@page import="com.wxm.framework.constant.GlobalConstant"%>  
  2. <%@page import="com.wxm.pageModel.base.WSessionInfo"%>  
  3. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  4. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
  5. <c:set var="ctx" value="${pageContext.request.contextPath}"/>  
  6. <%  
  7. String path = request.getContextPath();  
  8. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  9.   
  10. <span style="white-space:pre">    </span><strong>WSessionInfo sessionInfo = (WSessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_WAP_INFO);//获取保存的登录等信息</strong>  
  11.                   
  12.     if(sessionInfo!=null){//判断保存的信息是否为空,即判断用户是否登录  
  13.         response.sendRedirect("/bd/wap/ar.jsp");//如果用户登录后就跳转到某个页面  
  14. }  
  15. %>  
  16.   
  17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  18. <html xmlns="http://www.w3.org/1999/xhtml">  
  19. <head>  
  20. <base href="<%=basePath%>">  
  21. <meta http-equiv="content-type" content="text/html; charset=UTF-8">  
  22. <meta charset="utf-8">  
  23. <title>登录</title>  
  24. <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">  
  25. <meta name="apple-mobile-web-app-capable" content="yes">  
  26. <meta name="apple-mobile-web-app-status-bar-style" content="black">  
  27. </head>  
  28.   
  29. <body>  
  30. </body>  
  31. </html>  
  32. </span></span>  

4.退出session

  1. <span style="font-family:Microsoft YaHei;"><span style="white-space:pre">   </span>@RequestMapping("/logout")  
  2.     @ResponseBody  
  3.     public Json logout(HttpSession session) {  
  4. <span style="white-space:pre">        </span>Json j = new Json();//这个类是保存成功或失败的信息  
  5.         if (session != null) {  
  6.             session.invalidate();//调用session的invalidate()方法,将保存的session删除  
  7.         }  
  8.         j.setSuccess(true);  
  9.         j.setMsg("退出登录成功!");  
  10.         return j;  
  11. <span style="white-space:pre">    </span>}</span>  









原文地址:https://www.cnblogs.com/jpfss/p/8078682.html