一、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的更多相关文章

  1. Hibernate基础学习(三)—Session

    一.概述      Session接口是Hibernate向应用程序提供的操纵数据库最主要的接口,它提供了基本的保存.更新.删除和加载Java对象的方法.      Session具有一个缓存,位于缓 ...

  2. Hibernate学习三----------session详解

    © 版权声明:本文为博主原创文章,转载请注明出处 如何获取session对象 1. openSession 2. getCurrentSession - 如果使用getCurrentSession需要 ...

  3. Struts2框架学习(三) 数据处理

    Struts2框架学习(三) 数据处理 Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理. 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言. 值栈:Value ...

  4. DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件

    DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件   本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...

  5. day91 DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件

    DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件   本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...

  6. day 89 DjangoRestFramework学习三之认证组件、权限组件、频率组件、url注册器、响应器、分页组件

    DjangoRestFramework学习三之认证组件.权限组件.频率组件.url注册器.响应器.分页组件   本节目录 一 认证组件 二 权限组件 三 频率组件 四 URL注册器 五 响应器 六 分 ...

  7. HTTP学习三:HTTPS

    HTTP学习三:HTTPS 1 HTTP安全问题 HTTP1.0/1.1在网络中是明文传输的,因此会被黑客进行攻击. 1.1 窃取数据 因为HTTP1.0/1.1是明文的,黑客很容易获得用户的重要数据 ...

  8. TweenMax动画库学习(三)

    目录               TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)           ...

  9. Servlet的学习之Session(3)

    在上一篇<Servlet的学习之Session(2)>我们知道了Session能实现一个会话过程中保存数据或者多个会话中实现同一个Session的关键因素就是Cookie,只是Cookie ...

随机推荐

  1. mysql c语言 动态链接库

    下载地址 https://dev.mysql.com/downloads/connector/c/ 使用libmysql.lib和libmysql.dll进行操作mysql

  2. Android 4.4 KitKat 支持 u 盘功能

    Android U 盘功能实现和分析 u 盘功能实现结果: u 盘会当成 usb storage 在 Settings Storage 里面显示. 准备工作 内核需支持 usb host,需支持 FU ...

  3. linux 下处理大文件

    .head tail more .先把大文件进行分割 split split 参数: -a, --suffix-length=N 指定输出文件名的后缀,默认为2个 -b, --bytes=SIZE 指 ...

  4. activeMQ Jms Demo

    概述 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经 ...

  5. HTTP 错误状态码讯息

    HTTP 错误讯息解读   4xx: Client Error 使用者端(浏览器)错误讯息 错误码   错误讯息说明 400 Bad Request    错误的要求 401 Unauthorized ...

  6. Spring velocity 中文乱码 解决方案

    主要有这么几步,在spring web 的  [sevlet-name]-servlet.xml文件中,修改为: 黑体字体为关键,其它根据你的实际情况配置: <!-- ============= ...

  7. shell中的find和xargs详细解析

  8. css 只改变父元素的透明度,不改变子元素透明度rgba+opacity

    给元素加透明度时,通常写法为:opacity:0.5,filter:alpha(opacity=50); 我们通常也会遇到,在给父元素背景设置透明度时,子元素内容继承了父元素的透明度. 如何让子元素脱 ...

  9. Apache多虚拟主机多版本PHP(5.2+5.3+5.4)共存运行配置全过程

    因为某种需求,可能是因为早期的项目需要低版本的php,和目前开发所用的版本不太一致,我们需要给不同的虚拟主机配置不同版本的PHP.避免去额外配置多个Apache,等iis和apache共存的麻烦. 下 ...

  10. bootstrap-fileinput文件上传组件和laravel引用(未完)

    前言:之前的三篇介绍了下bootstrap table的一些常见用法,发现博主对这种扁平化的风格有点着迷了.前两天做一个excel导入的功能,前端使用原始的input type='file'这种标签, ...