邯郸网站网站建设,免费ppt模板下载简约风,wordpress文章分段,wordpress添加分类图片尺寸1、什么是 Session
HTTP协议 #xff08; http://www.w3.org/Protocols/ #xff09;是“一次性单向”协议。
服务端不能主动连接客户端#xff0c;只能被动等待并答复客户端请求。客户端连接服务端#xff0c;发出一个HTTP Request#xff0c;服务端处理请求#xff0…1、什么是 Session
HTTP协议 http://www.w3.org/Protocols/ 是“一次性单向”协议。
服务端不能主动连接客户端只能被动等待并答复客户端请求。客户端连接服务端发出一个HTTP Request服务端处理请求并且返回一个HTTP Response给客户端本次HTTP Request-Response Cycle结束。
我们看到HTTP协议本身并不能支持服务端保存客户端的状态信息。于是Web Server中引入了session的概念用来保存客户端的状态信息。
这里用一个形象的比喻来解释session的工作方式。假设Web Server是一个商场的存包处HTTP Request是一个顾客第一次来到存包处管理员把顾客的物品存放在某一个柜子里面这个柜子就相当于Session然后把一个号码牌交给这个顾客作为取包凭证这个号码牌就是Session ID。顾客HTTP Request下一次来的时候就要把号码牌Session ID交给存包处Web Server的管理员。管理员根据号码牌Session ID找到相应的柜子Session根据顾客HTTP Request的请求Web Server可以取出、更换、添加柜子Session中的物品Web Server也可以让顾客HTTP Request的号码牌和号码牌对应的柜子Session失效。顾客HTTP Request的忘性很大管理员在顾客回去的时候HTTP Response都要重新提醒顾客记住自己的号码牌Session ID。这样顾客HTTP Request下次来的时候就又带着号码牌回来了。
我们可以看到Session ID实际上是在客户端和服务端之间通过HTTP Request和HTTP Response传来传去的。
我们看到号码牌Session ID必须包含在HTTP Request里面。关于HTTP Request的具体格式请参见HTTP协议http://www.w3.org/Protocols/ 。这里只做一个简单的介绍。 在Java Web Server即Servlet/JSP Server中Session ID用jsessionid表示请参见Servlet规范。 2、HttpSession 的常用方法
String getId() 用于返回当前 HttpSession 对象关联的会话标识号
long getCreationTime() 返回 Session 创建的时间时间戳形式
long getLastAcessTime() 返回客户端上一次发送 Session 相关请求的时间也是时间戳
void setMaxInactiveInterval(int interval) 设置当前会话的超时时间
boolean isNew() 判断当前 HttpSession是否是新创建的
void invalidate() 用于强制使 Session 无效
ServletContext getServletContext() 返回当前Web程序的 ServletContext 对象
void setAttribute(String name,Object value) 将一个名称和一个对象 关联存储到 HttpSession 中
String getAttribute() 用于从当前 HttpSession 对象中返回指定属性对象
void removeAttribute(String name) 用于从当前 HttpSession 中删除指定名称的属性 3、Session 超时管理
在一定时间内当用户客户端一直没有请求访问那么服务器就会认为客户端已经结束请求。并且将客户端会话所对应的 HttpSession 对象变成垃圾等待垃圾收集器将其从内存中彻底清除。反之如果浏览器超时后再次向服务器发起请求那么服务器则会创建一个新的 HttpSession 对象并为其分配一个新的 ID 属性。
我们可以在 web.xml 里设置这个超时时间其默认有 Servlet 容器定义。在tomcat 安装目录 \conf\web.xml 文件中大概 576 行可以找到如下段配置信息 session-config session-timeout30/session-timeout /session-config 在上面的信息中设置的时间是以分钟为单位的即 Tomcat 服务器的默认会话超时时间是 30 分钟。如果设置会话超时时间是 0 或一个负数则永不超时这样会产生大量垃圾。
如果需要单独设置可以在对应项目下的 web.xml 里添加配置。
需要注意的是除了等待会话超时外可以通过 invalidate() 方法强制使会话失效。 4、Session案例1——实现用户登录
老规矩新建 web 项目 ServletTest src下新建 com.liuyanzhao 包
1新建 Login.html
在 WebContent 下新建 Login.html , 代码如下 form action/ServletTest/LoginServlet methodpost 用户名input typetext nameusername / br / 密 码input typepassword namepassword / br / input typesubmit value提交 idsub //form 2新建 LoginServlet 类
在 com.liuyanzhao 包下新建 LoginServlet.java 代码如下 package com.liuyanzhao;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/* * author LiuYanzhao */public class LoginServlet extends HttpServlet{ Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //防止乱码 resp.setContentType(text/html;charsetutf-8); String username req.getParameter(username); String password req.getParameter(password); PrintWriter out resp.getWriter(); //假设正确的账号是admin,密码是123456 if(admin.equals(username)123456.equals(password)) { User user new User(); user.setUsername(username); user.setPassword(password); req.getSession().setAttribute(user, user); resp.sendRedirect(/ServletTest/IndexServlet); } else { out.print(用户名和密码错误登录失败); } } Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }} 3新建 IndexServlet 类
在 com.liuyanzhao 包下新建 IndexServlet.java代码如下 package com.liuyanzhao;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;/* * author LiuYanzhao */public class IndexServlet extends HttpServlet{ Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解决乱码问题 resp.setContentType(text/html;charsetutf-8); PrintWriter out resp.getWriter(); //创建或者获取保存用户信息的Session对象 HttpSession session req.getSession(); User user (User)session.getAttribute(user); if(usernull) { out.print(您还没有登录请a href/ServletTest/Login.html登录/a); } else { out.print(您已登录欢迎你user.getUsername()br/); out.print(a href/ServletTest/LogoutServlet退出/a); //创建 Cookie 存放 Session 的标识号 Cookie cookie new Cookie(JSESSIONID, session.getId()); cookie.setMaxAge(60*30); //30分钟 cookie.setPath(/ServletTest); resp.addCookie(cookie); } } Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }} 4新建 LogoutServlet 类
在 com.liuyanzhao 包下新建 LogoutServlet.java代码如下 package com.liuyanzhao;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.catalina.User;/* * author LiuYanzhao */public class LogoutServlet extends HttpServlet{ Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //将Session对象中的 User 对象移除 req.getSession().removeAttribute(user); resp.sendRedirect(/ServletTest/IndexServlet); } Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }} 5在 web.xml 添加映射 servlet servlet-nameLoginServlet/servlet-name servlet-classcom.liuyanzhao.LoginServlet/servlet-class /servlet servlet-mapping !-- 映射为 LoginServlet -- servlet-nameLoginServlet/servlet-name url-pattern/LoginServlet/url-pattern /servlet-mapping servlet servlet-nameLogoutServlet/servlet-name servlet-classcom.liuyanzhao.LogoutServlet/servlet-class /servlet servlet-mapping !-- 映射为 LogoutServlet -- servlet-nameLogoutServlet/servlet-name url-pattern/LogoutServlet/url-pattern /servlet-mappingservlet servlet-nameIndexServlet/servlet-name servlet-classcom.liuyanzhao.IndexServlet/servlet-class /servlet servlet-mapping !-- 映射为 IndexServlet -- servlet-nameIndexServlet/servlet-name url-pattern/IndexServlet/url-pattern /servlet-mapping 6重启 Tomcat 打开浏览器
首先可以直接输入localhost:8080/ServletTest/IndexServlet ,这里会直接显示未登录如 ③ 这里不截图了
① 在地址栏输入urlhttp://localhost:8080/ServletTest/Login.html ② 点击提交 登录成功能显示用户名
③点击退出 销毁了 session退到了登录页面 本文链接https://liuyanzhao.com/4915.html