本博客为原创:综合 尚硅谷(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>
&nbsp;&nbsp;&nbsp;&nbsp;
<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>
&nbsp;&nbsp;&nbsp;&nbsp;
<%
session.invalidate();
%> </body>
</html>

效果图:

    

最新文章

  1. Javascript的二进制数据处理学习 ——nodejs环境和浏览器环境分别分析
  2. spark-shell和scala错误
  3. UIDynamicAnimator UIKit动力学
  4. php代码习惯(一)
  5. 用Wireshark简单分析HTTP通信
  6. hdu 4607 Park Visit
  7. 微信JSSDK上传多张图片
  8. MySQL --log-slave-updates
  9. android布局之线性布局
  10. Smarty3配置
  11. retina屏实现border边框1px
  12. 软件测试基础(软件测试分类和工具组)firebug、firepath的安装
  13. gitlab wiki 500
  14. [Luogu4149][IOI2011]Race
  15. IntelliJ IDEA上操作GitHub
  16. 学习 Vim —— Vimtutor 总结笔记
  17. 008_python内置语法
  18. docker-compose.yml(2)
  19. VC++每个版本对应的库
  20. 关于使用JPA中@Query注解传递表名/视图名参数的问题

热门文章

  1. 初探boost之smart_ptr库学习笔记
  2. 解决zabbix“ZBX_NOTSUPPORTED: Timeout while executing a shell script”报错
  3. java swing内嵌浏览器,隐藏滚动条
  4. CentOS 下Mysql数据库的安装与配置
  5. 盘古分词demo,盘古分词怎么用
  6. linux权限的深入讨论
  7. 基于vue + axios + lrz.js 微信端图片压缩上传
  8. JAVA中的继承特点1
  9. Oracle PL/SQL 高级编程
  10. maven 工程聚合插件