目录
1会话技术
1.1概念
日常生活中:从拨通电话到挂断电话之间的一连串你问我答的过程就是一个会话。
B/S架构中 : 从浏览器第一次给服务器发送请求时,建立会话;直到有一方断开,会话结束。
一次会话:包含多次请求响应。
1.2会话技术
作用:保存会话过程中产生的数据。
客户端会话技术:cookie
服务器端会话技术:session
2cookie
2.1概述
将产生的数据保存在浏览器端。
2.2基本使用
cookie是在服务器端创建的,是一种key/value的格式,通过响应写回浏览器,下次请求的时候按照一定的规则携带cookie到服务器上。
构造器:
new Cookie(String name,String value)
写回浏览器:
response.addCookie(Cookie c)
获取cookie:
request.getCookies(); 返回cookie的数据
cookie方法:
getName() 获取cookie的名字;
getValue() 获取cookie的值。
cookie怎么传输:
响应的cookie是通过set-cookie的响应头带回给浏览器。
请求的cookie是通过cookie请求头发送给服务器的。
public class OneServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//有数据产生 需要被保存
Cookie cookie = new Cookie("jiu" , "学习");
//保存数据 将cookie交给响应对象 响应给了浏览器
response.addCookie( cookie );
}
}
public class TwoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据请求获得cookie数据
Cookie[] cookies = request.getCookies();
//判断cookie
if(cookies != null){
//遍历
for (Cookie cookie : cookies) {
//获得cookie的名称 和 cookie值部分
System.out.println(cookie.getName() +"@@" + cookie.getValue());
}
}else{
System.out.println("暂时还没有获取到cookie的信息");
}
}
}
会话级别cookie: 浏览器打开服务器到浏览器关闭的时候,此次会话过程中数据保存,会话结束cookie就消失了。
2.3API
setMaxAge(int 秒数) 设置cookie在浏览器的存活时间,设置持久化级别的cookie。
setPath("路径") 设置cookie的路径,规定访问页面时携带哪些cookie
2.3.1设置存活时间:
-
单位是秒,int值
-
若为正整数的时候,设置指定存活时间
-
若为负数的时候,浏览器关闭的时候,此cookie的销毁
-
若为0的时候,立即销毁此cookie(前提:必须路径一致)
2.3.2设置路径
1.cookie的默认路径为:访问的servlet的路径,从项目应用路径开始,到最后一个"/"结束
例如:访问路径为/cookie/demo1 ,cookie的默认路径/cookie
访问路径为/cookie/a/demo1 ,cookie的默认路径/cookie/a
2.当我们访问的url包含此cookie的路径,就会携带此cookie;若不包含就不携带
cookie1的路径: /cookie/a/b
cookie2的路径: /cookie/a
cookie3的路径: /cookie
访问的路径为 http://localhost/cookie/a/b/1.html的时候,会携带3个cookie
访问的路径为 http://localhost/cookie/a/1.html的时候,会携带2和3这俩cookie
访问的路径为 http://localhost/cookie/1.html的时候,会携带第3个cookie
若真需要设置路径的时候,一般设置为项目的应用路径 request.getContextPath();
2.4cookie的特点
- 一个浏览器下,一个名字和一个路径只能确定一个cookie,如果同名则会覆盖。
- 名字或者路径不同,cookie就可以同时存在。
- cookie是不可以跨浏览器的。
- cookie的value可以存在中文,但是不可以存在空格 “,” “;”
- 若有这些特殊符号的时候,我们就需要先将value进行编码,获取到值之后再按指定的编码进行解码即可
- 编码的时候建议使用URLEncode.encode("要编码的字符串","utf-8")
- 解码的时候建议使用URLDecode.decode("要解码的字符串","utf-8");
- 423原则
- 4kb 一个cookie的值,最大长度限制4kb的数据
- 20个 一个网站,最多支持20个cookie
- 300个 所有网站,加起来最多300个cookie
//特殊字符 " " "," ";"
public class TeShu_Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s = "我爱你,中 国;";
String s_8 = URLEncoder.encode(s, "utf-8");
Cookie cookie = new Cookie("demo1", s_8);
response.addCookie(cookie);
//第一次获取不到此cookie的,但是第一次之后再访问就可以获取了
Cookie[] cookies = request.getCookies();
if (cookies!=null&&cookies.length>0) {
for (Cookie c : cookies) {
if ("demo1".equals(c.getName())) {
String value = c.getValue();
value = URLDecoder.decode(value,"utf-8");
System.out.println(value);
}
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
3session
session基于cookie实现,也可以不基于cookie实现。
3.1概述
当浏览器访问服务器的时候,服务器会看浏览器是否携带了id(JSESSIONID)。
若没有带,服务器会在服务器上创建一块空间,然后操作数据,服务器会将这个空间的id(JSESSIONID)通过响应返回给浏览器。
若携带了,服务器获取这个id,拿着这个id去自己的内存中查找有无这块空间。
若找到了,直接把数据存入这块空间。
若没有找到,服务器会在服务器上创建一块空间,操作数据,服务器会将这个空间的id(JSESSIONID)通过响应返回给浏览器。session底层是依赖于cookie,cookie中存放的就是JSESSIONID的值
3.2基本使用
获取session的方法:
request.getSession() 返回值:HttpSession
无论之前有没有session,都会有返回一个session,若之前的找到了就使用之前的session;若之前的session没有找到,就创建一个session。
常用方法:xxxAttribute(...)
public class SessionOneServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得session(第一次调用会创建session对象)
HttpSession session = request.getSession();
System.out.println(session);
System.out.println(session.getId());//手动获得session的id
System.out.println(session.isNew());//是不是新创建的
session.setAttribute("jiujiu","java");
}
}
public class SessionTwoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得session(第一次调用会创建session对象)
HttpSession session = request.getSession();
System.out.println(session);
System.out.println(session.getId());//手动获得session的id
System.out.println(session.isNew());//是不是新创建的
System.out.println( session.getAttribute("jiujiu") );
}
}
3.3生命周期
创建: 默认情况 第一次访问request.getSession() 创建。
销毁:1.默认tomcat服务器中,超时三十分钟销毁(一个session如果30分钟没有任何人操作,自动销毁)
2.手动调用invalidate方法销毁。
3.服务器非正常关闭销毁。
服务器正常关闭: 将内存的session数据序列化到了本地磁盘上(work下 sessions文件中) ,再次启动服务器 加载session文件 到内存,数据再加载回来 数据并没有消失。
3.4URL重写
其实就是在访问地址后面自己拼接一个jsessionid而已。
当用户禁用浏览器的cookie之后,浏览器就不能存放cookie了,但是session底层依赖于cookie,若还想使用session的话,就需要对所有的url进行url重写。
HttpServletResponse接口中提供了重写的方法,这时候JSESSIONID就编写了url的一部分。
例如:
/study/demo2;jsessionid=02B0C7BD37C1A2DE9A6BDC291D08A336
方法:
encodeUrl(url):其他的连接使用此方法
encodeRedirectUrl(url):重定向的连接就使用此方法
public class URLServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session
HttpSession session = request.getSession();
session.setAttribute("username","url重写");
String url=request.getContextPath();
System.out.println(response.encodeRedirectURL(url));
response.setContentType("text/html;charset=utf-8");
//response.getWriter().print("<a href='"+url+"'>返回首页</a><br>");
String url2 = request.getContextPath()+"/demo2";
url2=response.encodeURL(url2);
System.out.println(url2);
response.getWriter().print("<a href='"+url2+"'>获取数据</a>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
4cookie和session的对比
cookie : 数据存储在浏览器 , cookie存储的数据有限 , cookie 不安全
session: 数据存储在服务器 , session存储数据无限(取决于服务器的大小) session 相对安全
session是一个域对象,cookie不是。
session的底层是基于cookie的。
cookie默认是在关闭浏览器时销毁的。
session默认是在超时三十分钟后销毁。
5三个域对象
域对象: 在一定的范围内可以共享数据
5.1API
setAttribute(String key , Object value)
getAttribute(String key)
removeAttribute(String key
5.2ServletContext
创建:tomcat启动时。
销毁:服务器关闭时。
共享数据:整个项目共享。
5.3HttpServletRequest
创建:一次请求开始。
销毁:一次响应结束。
经过http协议将请求的数据发送给服务器,,服务器tomcat会为此次请求创建request和response对象,,并且将http协议数据放入其中。
共享数据:在一次请求响应的过程中。
5.4HttpSession
创建:第一次调用request.getSession();
销毁:1.长时间不使用session,30分钟后销毁。
2.调用invalidate方法手动销毁。
3.服务器非正常关闭。
数据共享:一次会话中,所有请求,共享同一个session。
5.5数据共享范围
ServletContext>HttpSession>HttpServletRequest
以上就是cookie和session的部分知识点啦,后续会继续补充。各位大佬如发现有知识点错误或者有不同的建议与意见,欢迎评论、私信指正,本人才疏学浅还需向各位大佬学习,还请不吝赐教!在此感谢各位的观看!谢谢!