java WEB学习笔记32:HttpSession 接口常用方法 及 HttpServletRequest接口中的Session方法 Demo
2016-05-31 21:37
846 查看
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在0221
QQ:951226918
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>session login JSP</title> </head> <body> SessionId :<%= session.getId() %> <br><br> IsNew:<%= session.isNew() %> <br><br> MaxInactiveInterval:<%= session.getMaxInactiveInterval() %> <br><br> CreateTime:<%= session.getCreationTime() %> <br><br> LastAccessTime:<%= session.getLastAccessedTime() %> <br><br> <% Object username = session.getAttribute("username"); if(username == null){ username = ""; } %> <form action="./hello.jsp" method="post"> username:<input type="text" name="username" value="<%= username %>"/> <input type="submit" value="submit"/> </form> </body> </html>
效果图:
经过注销后的 login界面
2) hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>session login JSP</title> </head> <body> SessionId :<%= session.getId() %> <br><br> IsNew:<%= session.isNew() %> <br><br> MaxInactiveInterval:<%= session.getMaxInactiveInterval() %> <br><br> CreateTime:<%= session.getCreationTime() %> <br><br> LastAccessTime:<%= session.getLastAccessedTime() %> <br><br> hello : <%= request.getParameter("username") %> <% session.setAttribute("username", request.getParameter("username")); %> <br><br> <a href="login.jsp">重新登录</a> <a href="logoff.jsp">注销</a> </body> </html>
效果图:
logoff.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>logoff JSP</title> </head> <body> SessionId :<%= session.getId() %> <br><br> IsNew:<%= session.isNew() %> <br><br> MaxInactiveInterval:<%= session.getMaxInactiveInterval() %> <br><br> CreateTime:<%= session.getCreationTime() %> <br><br> LastAccessTime:<%= session.getLastAccessedTime() %> <br><br> Bye : <%= session.getAttribute("username") %> <br><br> <a href="login.jsp">重新登录</a> <% session.invalidate(); %> </body> </html>
效果图:
相关文章推荐
- 网络第一课
- xListView(上拉刷新下拉加载,xml解析)中添加网络请求
- 关于人工智能的幻想(九)人工智能与网络游戏第二部分
- 虚拟机网络类型选择
- #码神学习#第四天
- 记一下机器学习笔记 Rosenblatt感知机
- TCP定时器介绍
- 使用Jmeter进行http接口测试 ---------成都杀手
- 网络管理 -- TCP/IP协议簇
- Mac系统开发常见问题-80端口被占用的解决方案1-使用pfctl做网络层转发
- TCP的拥塞避免、超时重传、快速重传、快速恢复
- Linux 网络配置
- HttpUtils请求数据
- TCP 客户端 服务端详细代码
- HTTP消息头的讲解
- Kerberos(转:http://www.cnblogs.com/jankie/archive/2011/08/22/2149285.html)
- http
- HTML5+CSS3-第五节(边框背景图、多背景图、CSS选择器、网络字体、边框背景图片、过渡、放大、旋转)
- 在Android应用中使用自定义证书的HTTPS连接(下)
- Tcp/Ip通信协议