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 ...
随机推荐
- this详解与面向对象编程
原文链接:http://www.cnblogs.com/kongxy/p/4581223.html JS中的this对象详解 声明:文章转载自上面地址,版权归阿孔所有,这里仅供本人学习笔记使用 t ...
- OFBiz:组件装入位置
默认的,OFBiz会在framework.applications.specialpurpose.hot-deploy这几个目录寻找组件,在themes目录中寻找主题.OFBiz是通过framewor ...
- Android开发之语音识别
2013-07-03 语音识别 2008年Google语音搜索在iphone平台上线,Android 1.5 将语音识别应用到搜索功能上. 手动输入是目前主要与手机互动的方式,语音搜索宗旨是最大限度地 ...
- 总结js(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作
http://hi.baidu.com/yashua839/blog/item/131fdb2fe547ef221f3089af.html一.Iframe 篇 //&&&&am ...
- mysql(表类型的选择)
1.查询mysql所支持的存储引擎 第一种方法:show engines \G
- Linux下源码安装JDK7
安装说明 安装环境:Red Hat Enterprise Linux7.1安装方式:源码安装 软件:jdk-7u80-linux-x64.gz 安装 #首先查看系统原有JDK信息 rpm -qa | ...
- 架构(三层架构)、框架(MVC)、设计模式三者异同点
前言: 本博客主要针对架构.框架和设计模式三者的区别.还有三层和MVC的区别进行讨论.对于这三者一点都不了解的.请点在维基和百度百科上补补课.这里就不发链接了 软件架构(software archit ...
- 如何修改浏览器默认的alert样式?
window.alert = function(str) { var shield = document.createElement("DIV"); shield.id = &qu ...
- quartz cron表达式在线生成
近期使用了quartz定时器,有感于起cron表达式有点复杂.且无法实时推断定时时间是否正确,因此写了个在线表达式及依据表达式获得前10次运行时间. 訪问地址例如以下:http://cron.g2ro ...
- C++ 编译,执行过程 具体解释。
要更深入了解C++, 必需要知道一个程序从開始到结束都干了些什么, 怎么干的. 所以我从C++编译到执行过程,解析下程序是怎么跑的. 首先,初略的说一下之前C++的编译过程.C++编译过程包含预编译- ...