一、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. Python list删除元素

    pop()方法 pop(n) 从list删除元素Paul同学刚来几天又要转走了,那么我们怎么把Paul 从现有的list中删除呢?如果Paul同学排在最后一个,我们可以用list的pop()方法删除: ...

  2. mmap 函数

    头文件:#include <unistd.h>    #include <sys/mman.h> 定义函数:void *mmap(void *start, size_t len ...

  3. Redhat 5禁止IPv6

    Redhat  5禁止IPv6 IPv6还没有全然普及,可是安装完系统之后IPv6是有效的,在一定程度上影响网络性能,所以在我们在全然不使用IPv6的情况下.最好关闭IPv6.如今我们就在本文以完整的 ...

  4. 虚拟互换(virtual swap)

    虚拟互换(virtual swap) 经济学中的互换(Swap)指的是这么一个东西:有2个交易方A.B须要进行跨国交易.各自都须要另外一个国家的某个商品.他们本来能够通过标准的汇率到各自国家的银行办理 ...

  5. dom与jquery互相转换

    /*取得<input>标签中的value属性的内容[dom对象->jquery对象] var inputElement = document.getElementById(" ...

  6. Linux下将/TMP和/Var移动到共享分区

    2007-03-09 03:25:08    整理数据 首先,必须创建一个新分区专门用于存储频繁修改的文件.您可能希望将这个分区置于单独的磁盘上以增强性能.接下来,我将逐步说明将 /tmp 和 /va ...

  7. EL表达式中fn函数

    JSTL 使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及 session中的数据的访问非常方便,但是我们在实际应用中经常需 ...

  8. Oracle 10g 数据库的备份和还原

    一.备份数据库 1.在图形工具中,如sqldeveloper,pl/sqldeveloper用以下这句查找空表 select 'alter table '||table_name||' allocat ...

  9. ubuntu下载linux内核源码

    ubuntu仓库里面关于源码部分配置的好全啊,什么都有,ps:包括vim的各种插件居然也有人打包放到仓库里,真是方便. 1.首先查看一下本系统使用的内核版本号: cat /proc/version L ...

  10. linux下C++的man文件安装

    GCC提供了一份c++的man文档,地址:ftp://gcc.gnu.org/pub/gcc/libstdc++/doxygen/ 下载最新版的文档,比如:libstdc++-api.20140403 ...