Session内置对象

Session内置对象
  Request内置对象中的属性只是在当次请求中有效(经过客户端跳转之后就无效,因为客户端跳转属于第二次请求)
  也就是说request只代表当次请求的对象,如果要让客户端跳转之后保存的属性还有效,则可以使用ssion内置对象。
  用户的信息应保存在表示一个用户的内置对象中,就是session内置对象,因为Session就算客户端跳转了,保存的属性还是有效的。
  session内置对象的类型是“javax servethttp HttpSession",

Session的存活时间
  经过客户端跳转页可以获取保存在 session 内置对象中的信息, 因为 session 表示的是一个用户.
  不关闭浏览器 session 就存在(默认时间是30分钟), 可以设置 session的超时时间
Demo: 设置 session 的超市时间
  在 Tomcat 下的 confweb.xml 中设置, 修改为60分钟
<!-- ==================== Default Session Configuration ================= -->
<!-- You can set the default session timeout (in minutes) for all newly -->
<!-- created sessions by modifying the value below. -->

<session-config>
<session-timeout>60</session-timeout>
</session-config>

常见的方法有:
  public void setAttribute( java lang. String nase. java. lang 0bject value)
    保存属性
  public java. lang. 0bject getAttr ibute( java. lang. String name
    根据属性名获取值(只能获取使用 setAttribute( ) 保存的数据值)
  public void removeValue( java. lang. String name)
    根据属性名称刪除对应的值, 只能删除使用 setAttribute( ) 保存的数据值)
  public void boolean isNew( )
    判断当前访问的用户是否是第一次访问
  public void invalidate
    销毁当前 session, 一般用来实现用户的注销功能
  public java.lang.String getId( )
    获取 session 的编号, 这个编号和浏览器中名字叫做 JSEESSIONID 的 Cookie 的值是一样的

Demo: 获取 session 的编号

1 @SuppressWarnings("serial")
2 public class EmpServlet extends HttpServlet {
3     @Override
4     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
5         HttpSession session = req.getSession();
6         String id = session.getId();
7         System.out.println(id);
8     }
9 }

Demo: 实现简单登录的验证
1.定义表单 (实现登录)

 1 <body>
 2     <form action="emp/login" method="POST">
 3         <fieldset>
 4             <legend>请登录!</legend>
 5             用户名: <input type="text" name="username"><br><br>
 6&nbsp;&nbsp;&nbsp;码: <input type="password" name="pwd"><br/><br/>
 7             <input style="margin-left:60px" type="submit" value="提交">
 8             <input type="reset" value="重置">
 9         </fieldset>
10     </form>
11 </body>

2.登录判断 (删除数据的时候需要进行登录验证)

 1 @SuppressWarnings("serial")
 2 public class EmpServlet extends HttpServlet{
 3     //获取业务层实现类对象
 4     private IEmpService empservice = (IEmpService)ServiceFactory.getInstance(EmpServiceImpl.class);
 5     @Override
 6     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 7         //处理中文乱码
 8         req.setCharacterEncoding("utf-8");
 9         String pathInfo = req.getPathInfo();
10         try {
11             if ("/login".equals(pathInfo)) {
12                 this.login(req,resp);
13             } else if("/remove".equals(pathInfo)) {
14                 this.removeById(req, resp);
15             } else if ("/logout".equals(pathInfo)) {
16                 this.logOut(req, resp);
17             }
18         } catch (Exception e) {
19             e.printStackTrace();
20         }
21     }
22     //删除数据的方法
23     public void removeById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
24         Integer empno = Integer.parseInt(req.getParameter("id"));
25         if (req.getSession().getAttribute("emp")==null) {
26             req.setAttribute("msg", "只有登录之后才能删除数据! ");
27             req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
28         } else {
29             System.out.println(empservice.removeEmpById(empno));
30         }
31     }
32     //负责登录的方法
33     public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
34         String name = req.getParameter("username");
35         String pwd = req.getParameter("pwd");
36         //查询数据库中的用户密码和用户输入的进行对比
37         Emp emp = empservice.findLogin(name, pwd);
38         if (emp != null) {
39             //将用户的信息保存到 seesion 内置对象
40             req.getSession().setAttribute("emp", emp);
41             // 客户端跳转
42             resp.sendRedirect("/MvcPro/pages/welcome.jsp");
43         } else {
44             // 重新返回登录页面再次登录(服务器端跳转)
45             req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
46         }
47     }
48     //注销的方法
49     public void logOut(HttpServletRequest req, HttpServletResponse resp) throws Exception  {
50         req.getSession().invalidate();
51         resp.sendRedirect("/MvcPro/pages/login.jsp");
52     }
53     @Override
54     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
55         this.doGet(req, resp);
56     }
57 }
原文地址:https://www.cnblogs.com/yslf/p/10745571.html