会话
分为有状态会话和无状态会话
在HTML中,"会话"一般指的是Web服务器与客户端(通常是浏览器)之间进行的一系列请求和响应。它是一种在网络上模拟人与人之间通信的方式,常见于Web应用程序中。
会话、Cookie和Session都是Web开发中用于跟踪和管理用户状态的重要概念。下面我将对它们进行简要解释和比较:
总结起来,会话和Cookie都是用于跟踪和管理用户状态的技术,但它们在存储位置、数据安全性、数据容量和生命周期等方面存在差异。根据具体需求和应用场景,开发人员可以选择使用会话、Cookie或它们的组合来实现用户状态的跟踪和管理。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、服务端创建cookie对象
Cookie cookie = new Cookie("role", "aa"); //键、值,在有效期内,如果键相同,值不同,关闭再次打开,值会覆盖之前的;如果键不同,会重新创建一个cookie
//2、设置cookie有效期
// >0 cookie有效期
// <0 临时存储,是一个会话,关闭浏览器就消失了
// 0 删除,刷新页面就没有了
cookie.setMaxAge(0);
//3、将cookie响应给客户端
response.addCookie(cookie);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取cookie信息
Cookie[] cookies = request.getCookies();
for(int i=0;i"+cookies[i].getValue()); //获取关键字和值
}
}
Insert title here
hello
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session对象
HttpSession session = request.getSession();
//设置值
session.setAttribute("key", "value");
session.setAttribute("account", "xixi");
//重写url追加jsessionid
String newUrl = response.encodeRedirectURL("getSession");
//重定向
response.sendRedirect(newUrl);
/*//获取sessionid
System.out.println(session.getId());
//获取session信息
System.out.println(session.getAttribute("key"));
System.out.println(session.getAttribute("account"));
//删除
session.removeAttribute("key");
System.out.println(session.getAttribute("key"));
System.out.println(session.getAttribute("account"));
//销毁session
session.invalidate();*/
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session对象
HttpSession session = request.getSession(); //如果有jsessionid会到服务端找到对应的session,如果没有则创建一个新的session对象
System.out.println(session.getId());
//获取session信息
System.out.println(session.getAttribute("key"));
System.out.println(session.getAttribute("account"));
//删除
/*session.removeAttribute("key");
System.out.println(session.getAttribute("key"));
System.out.println(session.getAttribute("account"));*/
//销毁session
session.invalidate();
}
cookie禁用后,请求转发和重定向
当用户禁用了Cookie时,请求转发和重定向的行为可能会受到一些影响。下面我将简要解释这些影响:
需要注意的是,即使Cookie被禁用,某些情况下仍然可以使用其他技术来跟踪用户状态,例如通过URL参数、隐藏表单字段或服务器端会话来传递必要的信息。然而,这些方法可能不如使用Cookie方便和高效。
因此,当设计Web应用程序时,开发人员应该考虑到用户可能禁用Cookie的情况,并采取适当的措施来确保应用程序的正常运行和用户体验。
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;
/**
* Servlet implementation class S01
*/
@WebServlet("/S01")
public class S01 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public S01() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String account = request.getParameter("account");
String password = request.getParameter("password");
System.out.println("s01账号:"+account);
System.out.println("s01密码:"+password);
System.out.println("s01");
//请求转发
//可以转发至网站内任意资源
//一次请求,数据在request域中共享
//地址栏不发生改变
//不能跨域
//服务端行为
//request.getRequestDispatcher("S02").forward(request, response);
//request.getRequestDispatcher("main.html").forward(request, response);
//request.getRequestDispatcher("WEB-INF/demo.html").forward(request, response);
//重定向,不能访问到WEB-INF里面的,WEB-INF屏蔽了所有客户端行为,WEB-INF是针对服务端的
//客户端行为
//两次请求,数据在request域中不共享
//可以重定向到任意地址(可以跨域)
//response.sendRedirect("S02");
//response.sendRedirect("main.html");
//response.sendRedirect("https://www.baidu.com/");
}
/**
* @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);
}
}
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;
/**
* Servlet implementation class S02
*/
@WebServlet("/S02")
public class S02 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public S02() {
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
String account = request.getParameter("account");
String password = request.getParameter("password");
System.out.println("s02账号:"+account);
System.out.println("s02密码:"+password);
System.out.println("s02");
}
/**
* @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);
}
}