javaweb学习6_Httpsession接口常用的方法及HttpServletRequest接口中session方法Demo

 1.HttpSession 常用方法

getId()

getCreationTime()

getLastAccessedTime()

setMaxInactiveInterval()

getMaxInactiveInterval()

isNew()  如果客户端请求消息中返回了一个与Servlet程序当前获得的HttpSession对象的会话标识号相同的会话标识号,则认为这个HttpSession对象不是新建的。

invalidate()

getServletContext()

setAttribute()

getAttribute()

removeAttribute()

getAttributeNames()

 

2.HttpServletRequest接口中的Session方法

 

getSession()

   public HttpSession getSession(boolean create)

   public HttpSession getSession()

isRequestedSessionIdValid()

isRequestedSessionIdFromCookie()

isRequestedSessionIdFromURL()

 

3. 综合Dome

总结理解 session 的方法

代码:login.jsp  hello.jsp, logoff.jsp

 

  1)login.jsp

 

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>session login JSP</title>
 8 </head>
 9 <body>
10 
11     SessionId :<%= session.getId() %>
12     <br><br>
13     
14     IsNew:<%= session.isNew() %>
15     <br><br>
16     
17     MaxInactiveInterval:<%= session.getMaxInactiveInterval() %>
18     <br><br>
19     
20     CreateTime:<%= session.getCreationTime() %>
21     <br><br>
22     
23     LastAccessTime:<%= session.getLastAccessedTime() %>
24     <br><br>
25     <%
26         Object username = session.getAttribute("username");
27         if(username == null){
28             username = "";
29         }     
30     %>
31     
32     <form action="./hello.jsp" method="post">
33     
34         username:<input type="text" name="username" value="<%= username %>"/>
35         <input type="submit" value="submit"/>
36     </form>
37 
38 </body>
39 </html>

 

 

   效果图:

    

 

经过注销后的 login界面

    

 

  2) hello.jsp

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>session login JSP</title>
 8 </head>
 9 <body>
10 
11     SessionId :<%= session.getId() %>
12     <br><br>
13     
14     IsNew:<%= session.isNew() %>
15     <br><br>
16     
17     MaxInactiveInterval:<%= session.getMaxInactiveInterval() %>
18     <br><br>
19     
20     CreateTime:<%= session.getCreationTime() %>
21     <br><br>
22     
23     LastAccessTime:<%= session.getLastAccessedTime() %>
24     <br><br>
25     
26     hello : <%= request.getParameter("username") %>
27     <%
28         session.setAttribute("username", request.getParameter("username"));
29     %>
30     <br><br>
31     <a href="login.jsp">重新登录</a>
32     &nbsp;&nbsp;&nbsp;&nbsp;
33     <a href="logoff.jsp">注销</a>
34 </body>
35 </html>

 

 

 效果图:

    

 

 

 logoff.jsp

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>logoff JSP</title>
 8 </head>
 9 <body>
10     
11     SessionId :<%= session.getId() %>
12     <br><br>
13     
14     IsNew:<%= session.isNew() %>
15     <br><br>
16     
17     MaxInactiveInterval:<%= session.getMaxInactiveInterval() %>
18     <br><br>
19     
20     CreateTime:<%= session.getCreationTime() %>
21     <br><br>
22     
23     LastAccessTime:<%= session.getLastAccessedTime() %>
24     <br><br>
25     
26     Bye : <%= session.getAttribute("username") %>
27     
28     <br><br>
29     
30     <a href="login.jsp">重新登录</a>
31     &nbsp;&nbsp;&nbsp;&nbsp;
32     <%
33         session.invalidate();
34     %>
35 
36 
37 </body>
38 </html>

 

 

 

 效果图:

    

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值