会话:
有状态会话:
客户端知道发起请求的是谁
无状态会话:
不知道发起请求的是谁 只知道有请求
http是无状态请求
保存会话信息的两种技术:
可以通过Cookie和Session储存会话信息
cookie:客户端技术 信心存在客户端
由服务器创建,通过响应发送给客户端,并且保存在客户浏览器的一块信息 浏览器下次向服务器发起请求时会携带这块信息并发送到服务器上
工作流程
后端中设置cookie
package com.qcby.test;
import java.io.IOException;
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;
/**
* Servlet implementation class SetCookie
*/
@WebServlet("/SetCookie")
public class SetCookie extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SetCookie() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
//服务器创建cookie对象
Cookie cookie=new Cookie("username","admin");
//设置cookie有效期 有效期小于0说明是临时的 浏览器关掉就没有了 =0则是删除这个cookie
cookie.setMaxAge(60*60);
//将cookie相应给客户端
response.addCookie(cookie);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
后端中读取cookie
package com.qcby.test;
import java.io.IOException;
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;
/**
* Servlet implementation class GetCookie
*/
@WebServlet("/GetCookie")
public class GetCookie extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetCookie() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
Cookie[] cookies=request.getCookies();
for(int i=0;i<cookies.length;i++)
{
System.out.println(cookies[i].getName()+"-->"+cookies[i].getValue());
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
js中创建cookie
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
document.cookie="password=123456;max-age="+30*60;
var getCookie=function(name){
var cookies=document.cookie.split(";");//以";"做分割
for(var i=0;i<cookies.length;i++){
if(cookies[i].trim().indexOf(name)==0)//子串中是否包含name
{
return cookies[i].trim().substring(name.length+1)
}
}
return ""
}
console.log(getCookie("password"))
</script>
</head>
<body>
</body>
</html>
使用js的cookie插件创建cookie
jQuery Cookie 插件 | 菜鸟教程 去这下插件 有用法解释
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://cdn.staticfile.org/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
$.cookie("color","pink")
</script>
</head>
<body>
</body>
</html>
cookie的优缺点
优点:
可设置到期规则 重复访问时不需要再次登录
包含简单的键值对 数据持久
缺点:
大小受限制
浏览器可以设置cookie禁用
cookie可被篡改 不安全
session:服务端技术 信息存在服务端 只能在后端中进行
为每次会话创建一个会话对象 也就是session对象 存储和取数据都在这个对象中
工作流程:
创建session
package com.qcby.test;
import java.io.IOException;
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;
/**
* Servlet implementation class SetSession
*/
@WebServlet("/SetSession")
public class SetSession extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SetSession() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
HttpSession session=request.getSession();
session.setAttribute("account", "admin");
session.setAttribute("password", "123456");
session.setAttribute("demo", "777");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
获取
package com.qcby.test;
import java.io.IOException;
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;
/**
* Servlet implementation class GetSession
*/
@WebServlet("/GetSession")
public class GetSession extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public GetSession() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
HttpSession session=request.getSession();
System.out.println(session.getId());//获取jsessionid
System.out.println(session.getAttribute("account"));
System.out.println(session.getAttribute("password"));
System.out.println(session.getAttribute("demo"));
session.removeAttribute("demo");//删除demo
session.invalidate();//销毁session 关掉浏览器也可以实现 jsessionid就会改变
session.setMaxInactiveInterval(60*60);//设置有效期
//session生命周期
//第一次使用到session的请求创建session
//浏览器关闭或者服务器关闭或者手动销毁或者到期超时失效
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
标签:request,javax,HttpServlet,Session,Cookie,import,servlet,response
From: https://blog.csdn.net/2403_87729201/article/details/145110676