SSH学习三 SESSION
一、session方法
Session:由同一个IE窗体向同一个WEBAPP发的全部请求的总称,一个会话
同一个会话的多个额请求能够从前到后多个请求。??祖给孙。孙不给祖
浏览器:搜集sessionID信息。并发到server。
没有就不发送。
查找sessionID。若找到,看servlet是否须要session,须要就从server内存提取旧的session对象。否则维持旧的Session不动。改动session的使用时间。
假设没找到。看是否须要session,再创建session对象,而且保持session对象在server中。把sessionID写到IE中。
sessionID放到IE浏览器。浏览器通过request把id带到server端
HttpSession session = request.getSession(false);// 这里false仅仅能用就得Session
True有旧的找旧的。否则建新的
Session.setMaxInactiveInterval两次请求之间的最长的时间间隔单位:s。相当于Session最大存活时间,超过之后server销毁这个session。比方登陆之后有一定时间,超出就销毁。
0表示马上过期,-1表示永只是期
设置最大时间的原因:
(1)Session是容器。要长时间占用内存。所以限定最大时间间隔
(2)安全考虑
Session.isNew()是不是新的
自杀。用于安全退出或者清空购物车
下面參考:http://copperfield.iteye.com/blog/890018
session.invalidate()是销毁跟用户关联session,比如有的用户强制关闭浏览器,而跟踪用户的信息的session还存在,但是用户已经离开了。
尽管session 生命周期浏览默认时间30分,可是在30分钟内别的用户还能够訪问到前一个用户的页面,需销毁用户的session。
session.removeAttribute()移除session中的某项属性。
在spring样例中宠物商店的注销登录的代码:
request.getSession().removeAttribute("userSession");
// 注销用户,使session失效。
request.getSession().invalidate();
二、session使用 以登录为例
业务逻辑:login.jsp提交表单,loginServlet推断usernamepassword是不是对,假设不正确转到login.jsp,假设对转到DealServlet(这个类推断是否登录还有效。无效就返回login,比方163邮箱),也能够通过该容器logout,能够logout返回login页面或者主页
事实上两个Servlet也能够做登陆
AServlet推断usernamepassword,正确就到Bservlet。B能够跳出,B中加上推断 假设找不到logid。就返回login,能找到就正常显示,这样仅仅要login之后。开心的标签页,还能够正常打开B
UserLoginServlet
public class UserLoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String logid = request.getParameter("logid");
String logpwd = request.getParameter("logpwd");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>logincontent</TITLE></HEAD>");
out.println(" <BODY>");
int flag=0;
HttpSession session = request.getSession();
if("Admin".equals(logid) && "123".equals(logpwd)){
session.setAttribute("userid", logid);
session.setMaxInactiveInterval(20);
//System.out.println("here");
//response.sendRedirect("http://localhost:8888/TestmyJSP/logout.servlet");
response.sendRedirect("deal.servlet"); // 不是类名而是url
}else{
out.write("<script>alert('login error'); history.go(-1);</script>");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
DealLoginServlet
public class DealLoginServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
HttpSession session = request.getSession(false);
if(session == null){
response.sendRedirect("login.jsp");
}else{
Object o = session.getAttribute("userid");
if(null == o){
response.sendRedirect("login.jsp");
}else{
out.write(o.toString());
}
}
out.println("<a href='logout.servlet'>登出</a> ");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
LogoutServlet
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
HttpSession session = request.getSession();
session.setMaxInactiveInterval(0);
session.invalidate();
response.sendRedirect("login.jsp");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
SSH学习三 SESSION的更多相关文章
- Hibernate基础学习(三)—Session
一.概述 Session接口是Hibernate向应用程序提供的操纵数据库最主要的接口,它提供了基本的保存.更新.删除和加载Java对象的方法. Session具有一个缓存,位于缓 ...
- Hibernate学习三----------session详解
© 版权声明:本文为博主原创文章,转载请注明出处 如何获取session对象 1. openSession 2. getCurrentSession - 如果使用getCurrentSession需要 ...
- Struts2框架学习(三) 数据处理
Struts2框架学习(三) 数据处理 Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理. 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言. 值栈:Value ...
- DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件
DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件 本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...
- day91 DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件
DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件 本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...
- day 89 DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件
DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件 本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...
- HTTP学习三:HTTPS
HTTP学习三:HTTPS 1 HTTP安全问题 HTTP1.0/1.1在网络中是明文传输的,因此会被黑客进行攻击. 1.1 窃取数据 因为HTTP1.0/1.1是明文的,黑客很容易获得用户的重要数据 ...
- TweenMax动画库学习(三)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) ...
- Servlet的学习之Session(3)
在上一篇<Servlet的学习之Session(2)>我们知道了Session能实现一个会话过程中保存数据或者多个会话中实现同一个Session的关键因素就是Cookie,只是Cookie ...
随机推荐
- 算法笔记_142:无向图的欧拉回路求解(Java)
目录 1 问题描述 2 解决方案 1 问题描述 John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8 ...
- 百度地图API拾取坐标网址
http://api.map.baidu.com/lbsapi/getpoint/index.html
- 【转发】jQuery1.9.1至最高版本针对checkbox的调整
在jquery 1.8.x中的版本,我们对于checkbox的选中与不选中操作如下: 判断是否选中 $('#checkbox').prop('checked') 设置选中与不选中状态: $('#che ...
- Android蓝牙——HID开发
代码地址如下:http://www.demodashi.com/demo/13891.html 原文地址: https://blog.csdn.net/VNanyesheshou/article/de ...
- PHP-数据库长连接mysql_pconnect的细节
PHP的MySQL持久化连接,美好的目标,却拥有糟糕的口碑,往往令人敬而远之.这到底是为啥么.近距离观察后发现,这家伙也不容易啊,要看Apache的脸色,还得听MySQL指挥. 对于作为Apache模 ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- mosquitto $SYS下topic
$SYS/broker/clients/connected
- VS2010/MFC编程入门之四十四:定时器Timer
前面一节鸡啄米讲了CTime类和CTimeSpan类的使用,本节继续讲与时间有关的定时器.定时器并不是一个类,主要考虑到,提起时间的话就不能不说定时器,所以就把它放到CTime和CTimeSpan之后 ...
- tp请求和响应
一.请求参数 use think\Request; 1.获取方法如下: http://w.tp.com/index/index/index/user/AAA $this->request-> ...
- Spring Boot 概念知识
转 http://rapharino.com/coder/Spring-Boot-Induction/ Spring Boot Induction 发表于 2016-10-29 | 分类于 c ...