Cookie对象与HttpSession对象简介
Cookie对象与HttpSession对象的作用是维护客户端浏览器与服务端的会话状态的两个对象。由于HTTP协议是一个无状态的协议,所以服务端并不会记录当前客户端浏览器的访问状态,但是在有些时候我们是需要服务端能够记录客户端浏览器的访问状态的,如获取当前客户端浏览器的访问服务端的次数时就需要会话状态的维持。在Servlet中提供了Cookie对象与HttpSession对象用于维护客户端与服务端的会话状态的维持。二者不同的是Cookie是通过客户端浏览器实现会话的维持,而HttpSession是通过服务端来实现会话状态的维持。
Cookie
Cookie是一种保存少量信息至浏览器的一种技术,第一请求时,服务器可以响应给浏览器一些Cookie信息,第二次请求,浏览器会携带之前的cookie发送给服务器,通过这种机制可以实现在浏览器端保留一些用户信息.为服务端获取用户状态获得依据
Cookie对象的特点
- Cookie使用字符串存储数据
- Cookie使用Key与Value结构存储数据
- 单个Cookie存储数据大小限制在4097个字节
- Cookie存储的数据中不支持中文,Servlet4.0中支持
- Cookie是与域名绑定所以不支持跨一级域名访问
- Cookie对象保存在客户端浏览器内存上或系统磁盘中
- Cookie分为持久化Cookie(保存在磁盘上)与状态Cookie(保存在内存上)
- 浏览器在保存同一域名所返回Cookie的数量是有限的。不同浏览器支持的数量不同,Chrome浏览器为50个
- 浏览器每次请求时都会把与当前访问的域名相关的Cookie在请求中提交到服务端。
Cookie对象的创建
// 通过new关键字创建Cookie对象
Cookie cookie = new Cookie("key","value")
// 通过HttpServletResponse对象将Cookie写回给客户端浏览器。
response.addCookie(cookie)
Cookie中数据的获取
// 通过HttpServletRequest对象获取Cookie,返回Cookie数组。
Cookie[] cookies = request.getCookies()
Cookie不支持中文解决方案
在Servlet4.0版本之前的Cookie中是不支持中文存储的,如果存储的数据中含有中文,代码会直接出现异常。我们可以通过对含有中文的数据重新进行编码来解决该问题。在Servlet4.0中的Cookie是支持中文存储的。
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
// 可以使用对中文进行转码处理 , 将内容按照指定的编码方式做URL编码处理。
URLEncoder.encode("content","code")
Cookie持久化和状态Cookie
状态Cookie:浏览器会缓存Cookie对象。浏览器关闭后Cookie对象销毁。
持久化Cookie:浏览器会对Cookie做持久化处理,基于文件形式保存在系统的指定目录中。在Windows10系统中为了安全问题不会显示Cookie中的内容。当Cookie对象创建后默认为状态Cookie。可以使用Cookie对象下的cookie.setMaxAge(60)方法设置失效时间,单位为秒。一旦设置了失效时间,那么该Cookie为持久化Cookie,浏览器会将Cookie对象持久化到磁盘中。当失效时间到达后文件删除。
Cookie跨域问题
域名分类:域名分为顶级域、顶级域名(一级域名)、二级域名。
域名等级的区别:一级域名比二级域名更高级,二级域名是依附于一级域名之下的附属分区域名,即二级域名是一级域名的细化分级。例如:baidu.com 为一级域名,news.baidu.com为二级域名。Cookie不支持一级域名的跨域,支持二级域名的跨域。
案例:
1.向浏览器响应Cookie
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
@WebServlet(urlPatterns = "/servlet1.do")
public class Servlet1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 通过响应对象,向浏览器响应一些Cookie
Cookie c1=new Cookie("age","10");// 状态Cookie 重启即清除
Cookie c2=new Cookie("gender", "男");//持久化Cookie 让浏览器保留1分钟
//c2.setMaxAge(60);// 秒钟 持久化Cookie 让浏览器保留1分钟
resp.addCookie(c1);
resp.addCookie(c2);
}
}
2.读取请求中的Cookie
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebEndpoint;
import java.io.IOException;
@WebServlet(urlPatterns = "/servlet2.do")
public class Servlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 读取请求中的Cookie
Cookie[] cookies = req.getCookies();
//cookies不为null
if(null != cookies){
for (Cookie cookie : cookies) {
System.out.println(cookie.getName()+"="+cookie.getValue());
}
}
}
}
Cookie对象总结
Cookie对于存储内容是基于明文的方式存储的,所以安全性很低。不要在Cookie中存放敏感数据。在数据存储时,虽然在Servlet4.0中Cookie支持中文,但是建议对Cookie中存放的内容做编码处理,也可提高安全性。
案例:通过Cookie判断用户是否访问过当前Servlet
需求:当客户端浏览器第一次访问Servlet时返回“您好,欢迎您第一次访问!”,第二次访问时返回“欢迎您回来!”
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/servlet3.do")
public class Servlet3 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 如果是第一访问当前Servlet.向浏览器响应一个cookie ("servlet3","1")
// 如果是多次访问,就再次数上+1
Cookie[] cookies = req.getCookies();
boolean flag =false ;
if(null !=cookies){
for (Cookie cookie : cookies) {
String cookieName = cookie.getName();
if(cookieName.equals("servlet3")){
// 创建Cookie次数+1
Integer value = Integer.parseInt(cookie.getValue())+1;
Cookie c=new Cookie("servlet3", String.valueOf(value));
resp.addCookie(c);
System.out.println("欢迎您第"+value+"访问");
flag=true;
}
}
}
if(!flag){
System.out.println("欢迎您第一次访问");
Cookie c=new Cookie("servlet3", "1");
resp.addCookie(c);
}
}
}
HttpSession对象
HttpSession是一种保存少量信息至服务器端的一种技术 , 第一请求时 , 服务器会创建HttpSession , 我们可以在HttpSession对象中保存一些关于用户的状态信息,并将HttpSession的JSESSIONID以Cookie形式响应给浏览器 ,第二次请求,浏览器会携带之前的JSESSIONID的Cookie,发送给服务器,服务器根据JSESSIONID获取对应的HttpSession对象.通过这种技术可以解决HTTP协议本身无法记录用户状态情况.
HttpSession对象的特点
- HttpSession保存在服务端
- HttpSession可以存储任何类型的数据
- HttpSession使用Key与Value结构存储数据 value是Object类型
- HttpSession存储数据大小无限制
HttpSession对象的创建
HttpSession对象的创建是通过request.getSession()方法来创建的。客户端浏览器在请求服务端资源时,如果在请求中没有JSESSIONID,getSession()方法将会为这个客户端浏览器创建一个新的HttpSession对象,并为这个HttpSession对象生成一个JSESSIONID,在响应中通过Cookie写回给客户端浏览器,如果在请求中包含了JSESSIONID,getSession()方法则根据这个ID返回与这个客户端浏览器对应的HttpSession对象。
getSession()方法还有一个重载方法getSession(true|false)。当参数为true时与getSession()方法作用相同。当参数为false时则只去根据SessionID查找是否有与这个客户端浏览器对应的HttpSession,如果有则返回,如果没有SessionID则不会创建新的HttpSession对象。
案例:API方法
// 将数据存储到HttpSession对象中
session.setAttribute("key",value)
// 根据key获取HttpSession中的数据,返回Object
Object value = session.getAttribute("key")
// 获取HttpSession中所有的key,返回枚举类型
Enumeration<String> attributeNames = session.getAttributeNames()
// 根据key删除HttpSession中的数据
session.removeAttribute("key")
// 根据获取当前HttpSession的SessionID,返回字符串类型
String id = session.getId()
案例: 创建HttpSession并存储数据
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(urlPatterns = "/servlet1.do")
public class Servlet1 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获得HttpSession对象 是一种保存更多数据在服务器端的一种技术
// 一般保存当前登录 的用户
// 用户的权限
// 用户的其他信息 ... ...
/*
* getSession方法执行内容
* 从request中尝试获取JSESSIONID的Cookie
*
* A如果获取失败,
* 认为上次会话已经结束,在这里要开启新的会话,创建一个新的HttpSession并返回
* 将新的HttpSession对象的JSESSIONID以Cookie的形式放在Response对象,响应给浏览器
*
* B如果获取成功
* 根据JSESSIONID在服务器内找对应HttpSession对象
* 1) 找到了,返回找到的HttpSession
* 2) 没找到,创建新的HTTPSession并将SESSIONID以Cookie的形式放在Response对象,响应给浏览器
*
* */
HttpSession httpSession=req.getSession();
// 向HttpSession中存放一些数据
httpSession.setAttribute("username", "msb");
httpSession.setAttribute("password", "1234");
httpSession.setAttribute("level", "A");
//httpSession.invalidate();// 手动设置HTTPSession不可用 退出登录
}
}
案例:获取上次创建的HttpSession并获取数据和session的其他信息
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(urlPatterns = "/servlet2.do")
public class Servlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取HTTPSession
HttpSession session = req.getSession();
// 尝试从HTTPSession中获取数据
String username = (String)session.getAttribute("username");
String password = (String)session.getAttribute("password");
String level = (String)session.getAttribute("level");
System.out.println(username);
System.out.println(password);
System.out.println(level);
// 获取Session对象的其他信息
System.out.println("创建时间:"+session.getCreationTime());
System.out.println("最后一次访问时间:"+session.getLastAccessedTime());
System.out.println("最大不活动时间:"+session.getMaxInactiveInterval());
}
}
HttpSession的两种销毁方式
- 通过web.xml文件指定超时时间(最大不活动时间)
- 通过HttpSession对象中的invalidate()方法销毁当前HttpSession对象。我们可以在web.xml文件中指定HttpSession的超时时间,当到达指定的超时时间后,容器就会销该HttpSession对象,单位为分钟。该时间对整个web项目中的所有HttpSession对象有效。时间的计算方式是根据最后一次请求时间作为起始时间。如果有哪个客户端浏览器对应的HttpSession的失效时间已到,那么与该客户端浏览器对应的HttpSession对象就会被销毁。其他客户端浏览器对应的HttpSession对象会继续保存不会被销毁。
<session-config>
<session-timeout>1</session-timeout>
</session-config>
我们也可以在Tomcat的web.xml文件中配置HttpSession的销毁时间,默认是30分钟。如果在Tomcat的web.xml文件中配置了HttpSession的超时时间对应的是Tomcat中所有的Web项目都有效。相当于配置了全局的HttpSession超时时间。如果我们在Web项目中配置了超时时间,那么会以Web项目中的超时时间为准。
invalidate()方法是HttpSession对象中所提供的用于销毁当前HttpSession的方法。我们通过调用该方法可以销毁当前HttpSession对象。
HttpSession生命周期
在HttpSession对象生命周期中没有固定的创建时间与销毁时间。何时创建取决于我们什么时候第一次调用了getSession()或getSession(true)的方法。HttpSession对象的销毁时间取决于超时时间的到达以及调用了invalidate()方法。如果没有超时或者没有调用invalidate()方法,那么HttpSession会一直存储。默认超时时间为30分钟(Tomcat的web.xml文件配置的时间就是默认超时时间)。
HttpSession对象总结
HttpSession与Cookie的区别:
- cookie数据存放在客户的浏览器或系统的文件中,而HttpSession中的数据存放在服务器中。
- cookie不安全,而HttpSession是安全的。
- 单个cookie保存的数据不能超过4K,很多浏览器都限制一个域名保存cookie的数量。而HttpSession没有容量以及数量的限制。
HttpSession的使用建议
HttpSession对象是保存在服务端的,所以安全性较高。我们可以在HttpSession对象中存储数据,但是由于HttpSession对象的生命周期不固定,所以不建议存放业务数据。一般情况下我们只是存放用户登录信息。
案例:通过HttpSession判断用户是否登录
需求: 实现登录一次即可,在一次会话内,可以反复多次访问WEB-INF/ welcome.html,如果没有登录过,跳转到登录页,登录成功后,可以访问
组件介绍:
login.html:登录页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="get" action="loginServlet.do">
用户名:<input type="text" name="username" ><br/>
密码:<input type="password" name="password" ><br/>
<input type="submit" >
</form>
</body>
</html>
main.html:登录成功之后可以访问的资源
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
this is main page
</body>
</html>
LoginServlet:用来校验登录的,登录成功将用户信息存户HttpSession,否则回到登录页
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(urlPatterns = "/loginServlet.do")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
// 如果用户名和密码为 msb 1234
if("admin".equals(username) && "123456".equals(password)){
// 将用户信息放在HTTPSession中
User user =new User(null, null, "admin", "123456");
HttpSession session = req.getSession();
session.setAttribute("user", user);
// 登录成功 跳转至 main.html
resp.sendRedirect(req.getContextPath()+"/mainServlet.do");
}else{
// 登录失败 回到login.html
resp.sendRedirect(req.getContextPath()+"/login.html");
}
}
}
MainServlet:用来向main.html中跳转的,同时验证登录,登录过,可以直接跳转,否则回到登录页
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.logging.Handler;
@WebServlet(urlPatterns = "/mainServlet.do")
public class MainServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//跳转至main.html
HttpSession session = req.getSession();
User user = (User)session.getAttribute("user");
if(null != user){
// 判断如果登录过 允许跳转 HTTPSession中如果有登陆过的信息
req.getRequestDispatcher("/WEB-INF/main.html").forward(req,resp);
}else{
// 如果没有登录过 回到登录去登录 HTTPSession中如果有登陆过的信息
resp.sendRedirect("login.html");
}
}
}
Session钝化和活化
Session序列化和反序列化
- 序列化与反序列:把对象转化为字节序列的过程称为序列化(保存到硬盘,持久化),把字节序列转化为对象的过程称为反序列化(存放于内存)
- 序列化的用途:把对象的字节序列永久保存到硬盘上,通常放到一个文件中。把网络传输的对象通过字节序列化,方便传输本节作业
Session钝化和活化案例:
要想实现序列化和反序列化需要手动配置,新建文件如图所示:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Manager className="org.apache.catalina.session.PersistentManager">
<Store className="org.apache.catalina.session.FileStore" directory="d:/session"/>
</Manager>
</Context>
实体类必须实现serializable 接口
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String pwd;
开发登录信息输入页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title%sSourceCode%lt;/title>
</head>
<body>
<form action="loginController.do" method="post">
用户名:<input type="text" name="user"> <br/>
密码:<input type="password" name="pwd"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
3开发登录信息验证Servlet
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/loginController.do")
public class LoginController extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("user");
String pwd = req.getParameter("pwd");
// user
User user =new User(username,pwd);
// session
HttpSession session = req.getSession();
session.setAttribute("user", user);
}
}
开发校验当前是否已经登录的Controller
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(urlPatterns = "/loginCheckController.do")
public class LoginCheckController extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 判断是否登录
HttpSession session = req.getSession();
Object user = session.getAttribute("user");
Object listener = session.getAttribute("listener");// 获得对应的监听器
String message ="";
if(null != user){
message="您已经登录过";
}else{
message="您还未登录";
}
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().println(message);
}
}
测试, 先登录,然后请求loginCheckController.do 校验是否登录过,然后重启项目,再次请求loginCheckController.do 校验是否登录过,发现重启后,仍然是登录过的