将登录等信息保存到session中和退出session
做项目时,可能会将某些信息保存在session中,如登录等信息,这样方便在某些页面使用这些保存的信息。
要想保存这些信息,需要创建一个类,该类里面定义需要保存的变量等信息,当登录后就通过new一个该类来保存登录等信息,然后放在session中,需要用到这些信息时直接用例如EL表达式等取出来就OK了。例子如下:
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>
- private String id;// 用户ID
- private String loginname;// 登录名
- private String name;// 姓名
- private String ip;// 用户IP
- private String userimg; //图片
- private RegisterUser user; //用户注册信息
- private String usertype;//用户类型
- private String chengsid;//城市编号
- private String cityname;//城市名
- private List<String> resourceList;// 用户可以访问的资源地址列表
- private List<String> resourceAllList;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getChengsid() {
- return chengsid;
- }
- public void setChengsid(String chengsid) {
- this.chengsid = chengsid;
- }
- <span style="white-space:pre"> </span>......
- }</span>
- </span>
- <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><span style="white-space:pre"> </span>@RequestMapping("/login")
- @ResponseBody
- public Json login(Account user, HttpSession session) {
- ......
- WSessionInfo sessionInfo = new WSessionInfo();//保存用户信息的类
- sessionInfo.setId(loginUser.getId());//为类里面的属性赋值,即把登录信息保存在这个类中
- sessionInfo.setLoginname(loginUser.getLoginname());
- sessionInfo.setUsertype(loginUser.getUsertype());
- sessionInfo.setUser(regUser);
- session.setAttribute(GlobalConstant.SESSION_WAP_INFO, sessionInfo);//将信息保存在session中
- }
- </span></span>
- <span style="font-size:14px;"><span style="font-family:Microsoft YaHei;"><%@page import="com.wxm.framework.constant.GlobalConstant"%>
- <%@page import="com.wxm.pageModel.base.WSessionInfo"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <c:set var="ctx" value="${pageContext.request.contextPath}"/>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- <span style="white-space:pre"> </span><strong>WSessionInfo sessionInfo = (WSessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_WAP_INFO);//获取保存的登录等信息</strong>
- if(sessionInfo!=null){//判断保存的信息是否为空,即判断用户是否登录
- response.sendRedirect("/bd/wap/ar.jsp");//如果用户登录后就跳转到某个页面
- }
- %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <base href="<%=basePath%>">
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <meta charset="utf-8">
- <title>登录</title>
- <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
- <meta name="apple-mobile-web-app-capable" content="yes">
- <meta name="apple-mobile-web-app-status-bar-style" content="black">
- </head>
- <body>
- </body>
- </html>
- </span></span>
4.退出session
- <span style="font-family:Microsoft YaHei;"><span style="white-space:pre"> </span>@RequestMapping("/logout")
- @ResponseBody
- public Json logout(HttpSession session) {
- <span style="white-space:pre"> </span>Json j = new Json();//这个类是保存成功或失败的信息
- if (session != null) {
- session.invalidate();//调用session的invalidate()方法,将保存的session删除
- }
- j.setSuccess(true);
- j.setMsg("退出登录成功!");
- return j;
- <span style="white-space:pre"> </span>}</span>