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

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

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

<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             }
14         } catch (Exception e) {
15             e.printStackTrace();
16         }
17     }
18     
19     //删除数据的方法
20     public void removeById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
21         Integer empno = Integer.parseInt(req.getParameter("id"));
22         if (req.getSession().getAttribute("emp")==null) {
23             req.setAttribute("msg", "只有登录之后才能删除数据! ");
24             req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
25         } else {
26             System.out.println(empservice.removeEmpById(empno));
27         }
28     }
29     
30     //负责登录的方法
31     public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
32         String name = req.getParameter("username");
33         String pwd = req.getParameter("pwd");
34         //查询数据库中的用户密码和用户输入的进行对比
35         Emp emp = empservice.findLogin(name, pwd);
36         if (emp != null) {
37             //将用户的信息保存到 seesion 内置对象
38             req.getSession().setAttribute("emp", emp);
39             // 客户端跳转
40             resp.sendRedirect("/MvcPro/pages/welcome.jsp");
41         } else {
42             // 重新返回登录页面再次登录(服务器端跳转)
43             req.getRequestDispatcher("login.jsp").forward(req, resp);
44         }
45     }
46     @Override
47     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
48         this.doGet(req, resp);
49     }
50 }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄