【JavaWeb】Maven、Servlet、cookie/session

article/2025/7/19 16:57:42

目录

  • 5. Maven
  • 6. Servlet
    • 6.1 Servlet 简介
    • 6.2 HelloServlet
    • 6.3 Servlet原理
    • 6.4 Mapping( **<font style="color:rgb(44, 44, 54);">映射 ** )问题
    • 6.5 ServletContext
    • 6.6 HttpServletResponse
      • <font style="color:rgb(232, 62, 140);background-color:rgb(246, 246, 246);">常见应用
    • 6.7 HttpServletRequest
  • 7. cookie/session
    • 7.1 会话
    • 7.2 保存会话的两种技术
    • 7.3 Cookie
    • 7.4 Session (重点)

5. Maven

Maven是一个强大的项目管理和理解工具,主要用于Java项目的构建、依赖管理和文档生成。以下是Maven的简明概述:

  • 核心概念:POM(Project Object Model),它是一个XML文件,包含了项目的配置信息,如依赖、构建目标等。
  • 依赖管理:自动处理项目依赖的库,通过中央仓库或自定义仓库下载所需的JAR包,并解决版本冲突。
  • 构建生命周期:定义了构建过程的标准阶段,包括验证、编译、测试、打包、集成测试、验证、部署等。
  • 插件支持:提供了多种插件来扩展其功能,比如编译代码、创建Javadoc以及运行单元测试等。
  • 多模块项目:支持复杂项目的分模块构建,方便大型项目的维护和管理。

Maven通过提供统一的构建系统、**约定优于配置 **的原则以及强大的依赖管理能力,极大地简化了Java项目的开发流程。

  • Maven项目的标准结构:
myproject/
├── src/
│   ├── main/
│   │   ├── java/Java 源代码
│   │   └── resources/    ← 资源文件(如 .properties, XML 等)
│   └── test/
└── pom.xml

6. Servlet

6.1 Servlet 简介

  • Servlet就是sun公司开发动态web的一门技术。
  • Sun在这些API中提供一个接口叫做:Servlet,如果你想开发一个 Servlet 程序,只需要完成两个小步骤:
    1. 编写一个类,实现 Servlet 接口;
    2. 把开发好的Java类部署到web服务器中。
      把实现了Servlet接口Java程序叫做 Servlet

6.2 HelloServlet

Serlvet接口Sun公司有两个默认的实现类: HttpServlet GenericServlet,如图所示, HttpServlet 继承自 GenericServlet ,我们手写一个servlet就需要继承自 HttpServlet

  1. 构建一个普通的Maven项目,删掉里面的src目录,在这个项目里面建立Moudel;

这个空的工程就是Maven主工程;

  1. 关于Maven父子工程的理解:
    父工程中会有:
<modules><module>servlet-01</module>
</modules>

子项目中会有:

<parent><groupId>org.example</groupId><artifactId>javaweb-servlet</artifactId><version>1.0-SNAPSHOT</version>
</parent>

父项目中的jar包依赖,子项目可以直接使用,反过来则不可以,这就是 继承

  1. Maven环境优化
  2. 编写一个Servlet程序
    1. 编写一个普通的类 HelloServlet
    2. 我们实现Servlet接口,这里我们直接继承 HttpServlet
public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("hello servlet");PrintWriter writer = resp.getWriter();writer.println("Hello Servlet");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req,resp);}
}
  1. 编写Servlet的映射
    为什么需要映射 :我们写的是Java程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务中注册我们写的Servlet,还需给他一个浏览器能够访问的路径。
<!--注册servlet-->
<servlet><servlet-name>helloservlet</servlet-name><servlet-class>com.sunyiwenlong.servlet.HelloServlet</servlet-class>
</servlet>
<!--servlet请求路径-->
<servlet-mapping><servlet-name>helloservlet</servlet-name><url-pattern>/hello</url-pattern>
</servlet-mapping>
  1. 配置tomcat
    注意:配置项目发布路径就可以了
  2. 启动测试

6.3 Servlet原理

6.4 Mapping( **映射 ** )问题

<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>my-web-app</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><dependencies><!-- Servlet API --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope></dependency><!-- JSP API --><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.3.3</version><scope>provided</scope></dependency><!-- Spring MVC 示例 --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.20</version></dependency>
</dependencies><build><finalName>mywebapp</finalName><plugins><!-- 编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin><!-- WAR 插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.2.3</version><configuration><failOnMissingWebXml>false</failOnMissingWebXml></configuration></plugin></plugins>
</build></project>
  1. 一个Servlet可以指定一个映射路径 "/hello"
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {protected void doGet(HttpServletRequest req, HttpServletResponse resp) {// 处理逻辑}
}
  1. 一个Servlet可以指定多个映射路径 {"/hello", "/hi", "/greeting"}
@WebServlet({"/hello", "/hi", "/greeting"})
public class HelloServlet extends HttpServlet {protected void doGet(HttpServletRequest req, HttpServletResponse resp) {// 多个路径都能访问到这个 Servlet}
}
  1. 一个Servlet可以指定通用映射路径 "/user/*"
@WebServlet("/user/*")
public class UserServlet extends HttpServlet {protected void doGet(HttpServletRequest req, HttpServletResponse resp) {// 所有以 /user/ 开头的请求都会进入该 Servlet}
}
  1. 指定一些后缀或者前缀等等…
@WebServlet("*.do")
public class ActionServlet extends HttpServlet {protected void doGet(HttpServletRequest req, HttpServletResponse resp) {// 所有以 .do 结尾的请求都进入此 Servlet}
}
@WebServlet("/api/*")
public class ApiServlet extends HttpServlet {protected void doGet(HttpServletRequest req, HttpServletResponse resp) {// 所有 /api/ 开头的请求进入此 Servlet}
}

6.5 ServletContext

ServletContext 是 Java Web 开发中一个非常核心的接口,属于 Servlet API 的一部分。每个 Web 应用在服务器启动时都会创建一个唯一的 ServletContext 实例。

Q:可以自己手动 new 一个 ServletContext 对象?

A:不能直接 new 创建 ServletContext

Q:why?

A:ServletContext 是 由 Web 容器(如 Tomcat、Jetty)在启动 Web 应用时自动创建的,它是整个 Web 应用的运行环境对象。

Q:那我们怎么获取它?

A:在 Servlet 、Listener、JSP中获取;

web容器在启动的时候,它会为每个web程序都创建一个对应的 ServletContext 对象,它代表了当前的web应用。

  1. 共享数据:在这个Servlet中保存的数据,可以在另一个Servlet中拿到

画板

1. 在 Servlet 中获取 `ServletContext`,将一个数据保存在了`ServletContext`中
public class HelloServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// this.getInitParameter(); 获取初始化参数(web.xml文件中的初始化参数)// this.getServletConfig(); 获取servlet的配置(web.xml文件中的配置)// this.getServletContext(); 获取servlet上下文ServletContext context = this.getServletContext();String username = "张三";context.setAttribute("username",username);// 将一个数据保存在了ServletContext中}
}
// 在 Listener 中获取getServletContext
public class MyListener implements HttpSessionListener {public void sessionCreated(HttpSessionEvent se) {ServletContext context = se.getSession().getServletContext();}
}
2. 将保存在context的数据响应出来。
public class GetServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext context = this.getServletContext();String username = (String) context.getAttribute("username");resp.setContentType("text/html");resp.setCharacterEncoding("utf-8");resp.getWriter().println("名字"+username);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req,resp);}
}
3. 配置URL地址映射
<!-- // web.xml文件 -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet><servlet-name>helloservlet1</servlet-name><servlet-class>com.sunyiwenlong.servlet.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>helloservlet1</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping><servlet><servlet-name>getservlet</servlet-name><servlet-class>com.sunyiwenlong.servlet.GetServlet</servlet-class></servlet><servlet-mapping><servlet-name>getservlet</servlet-name><url-pattern>/getc</url-pattern></servlet-mapping>
</web-app>
1. <font style="color:rgba(0, 0, 0, 0.85);">也可以通过注解配置地址映射 :: Servlet 3.0+ 规范 

  1. 获取初始化参数

  2. 配置web应用中的基本参数

<!-- web.xml文件 -->
<!--配置一些web应用一些初始化参数-->
<context-param><param-name>url</param-name><param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
  1. 实现ServletDemo03PostGet的逻辑
public class ServletDemo03 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String url = this.getInitParameter("url");resp.getWriter().println(url);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req, resp);}
}
  1. 请求转发

  2. 配置相关URL映射的地址

// web.xml文件
// 请求sd4
<servlet><servlet-name>gp</servlet-name><servlet-class>com.sunyiwenlong.servlet.ServletDemo03</servlet-class>
</servlet>
<servlet-mapping><servlet-name>gp</servlet-name><url-pattern>/gp</url-pattern>
</servlet-mapping><servlet><servlet-name>sd4</servlet-name><servlet-class>com.sunyiwenlong.servlet.ServletDemo04</servlet-class>
</servlet>
<servlet-mapping><servlet-name>sd4</servlet-name><url-pattern>/sd4</url-pattern>
</servlet-mapping>
  1. /sd4请求 找到ServletDemo04ServletDemo04逻辑块中进行请求 转发到/gp,到/gp的页面, /gp找到ServletDemo03
// 请求/sd4找到ServletDemo04,ServletDemo04进行请求转发到/gp,到/gp的页面
// (浏览器路径是sd4的路径,页面拿到的是/gp的数据)
public class ServletDemo04 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext context = this.getServletContext();System.out.println("进入了demo04");// RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");// 转发的路径// requestDispatcher.forward(req,resp);// 调用forward请求转发context.getRequestDispatcher("/gp").forward(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req, resp);}
}
  1. 读取资源文件 Properties
  • src/main/java 目录下新建 properties
  • src/main/resources 目录下新建 properties
  • 最后都被打包到了同一个路径下: target/classes ,我们俗称这个路径为classpath
  1. 实现一个文件流,读取properties
public class ServletDemo05 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {InputStream stream = this.getServletContext().getResourceAsStream("/WEB-INF/CLASSES/db.properties");Properties properties = new Properties();properties.load(stream);String username = properties.getProperty("username");String password = properties.getProperty("password");resp.getWriter().println(username+":"+password);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req, resp);}
}
  1. 用例图解析:

画板

在一个Web应用中,客户端通过请求与服务器上的多个Servlet交互,ServletContext在其中扮演的角色是提供一个全局的数据共享空间。

重点理解:ServletContext是所有Servlet都能访问的全局上下文,用于在应用范围内共享数据;而HttpSession则是针对单个用户的会话管理,用于保存用户相关的临时数据。

6.6 HttpServletResponse

web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的 HttpServletRequest 对象,代表响应的一个 HttpServletResponse

  • 如果要获取客户端请求过来的参数:找 HttpServletRequest
  • 如果要给客户端响应一些信息:找 HttpServletResponse
  1. 负责向浏览器发送数据的方法
public ServletOutputStream getOutputStream() throws IOException;
public PrintWriter getWriter() throws IOException;
  1. 响应的状态码
public static final int SC_CONTINUE = 100;
/*** Status code (200) indicating the request succeeded normally.*/
public static final int SC_OK = 200;/*** Status code (302) indicating that the resource has temporarily* moved to another location, but that future references should* still use the original URI to access the resource.** This definition is being retained for backwards compatibility.* SC_FOUND is now the preferred definition.*/
public static final int SC_MOVED_TEMPORARILY = 302;/*** Status code (302) indicating that the resource reside* temporarily under a different URI. Since the redirection might* be altered on occasion, the client should continue to use the* Request-URI for future requests.(HTTP/1.1) To represent the* status code (302), it is recommended to use this variable.*/
public static final int SC_FOUND = 302;/*** Status code (304) indicating that a conditional GET operation* found that the resource was available and not modified.*/
public static final int SC_NOT_MODIFIED = 304;/*** Status code (404) indicating that the requested resource is not* available.*/
public static final int SC_NOT_FOUND = 404;/*** Status code (500) indicating an error inside the HTTP server* which prevented it from fulfilling the request.*/
public static final int SC_INTERNAL_SERVER_ERROR = 500;/*** Status code (502) indicating that the HTTP server received an* invalid response from a server it consulted when acting as a* proxy or gateway.*/
public static final int SC_BAD_GATEWAY = 502;// ...

常见应用

  1. 向浏览器输出消息的
  2. 下载文件 实现方式
public class FileServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 1.要获取下载文件的路径 :\转义字符String realPath = "E:\\dev\\StudyProjects\\javaweb-servlet\\response\\src\\main\\resources\\大乔.jpg";// 2.下载的文件名是啥?String filename = realPath.substring(realPath.lastIndexOf("\\") + 1);// 3.设置想办法让浏览器能够支持下载我们需要的东西resp.setHeader("Content-disposition","attachment;filename="+ URLEncoder.encode(filename,"utf-8"));// 4.获取下载文件的输入流FileInputStream in = new FileInputStream(realPath);// 5.创建缓冲区int len = 0;byte[] buffer = new byte[1024]; // 每次读取的长度// 6.获取OutputStream对象ServletOutputStream out = resp.getOutputStream();// 7.将FileOutputStream流写入到bufer缓冲区while ((len = in.read(buffer))>0){// 每次读取的长度大于0的情况下,就写出去out.write(buffer,0,len);// 写出字节,从0写到len}// 8.使用OutputStream将缓冲区中的数据输出到客户端!in.close();out.close();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req, resp);}
}
  1. 验证码功能 实现方式
public class ImageServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 让浏览器3秒刷新一次resp.setHeader("refresh", "3");// 在内存中创建一个图片BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);// 宽、高、颜色// 得到图片Graphics2D g = (Graphics2D) image.getGraphics();// 得到一只2D的笔// 设置图片的背景颜色g.setColor(Color.white);g.fillRect(0, 0, 80, 20);// 填充颜色// 换个背景颜色g.setColor(Color.BLUE);// 设置字体样式:粗体,20g.setFont(new Font(null,Font.BOLD,20));// 画一个字符串(给图片写数据)g.drawString(makeNum(),0,20);// 告诉浏览器,这个请求用图片的方式打开resp.setContentType("image/jpeg");// 网站存在缓存,不让浏览器缓存resp.setDateHeader("expires",-1);resp.setHeader("Cache-Control","no-cache");resp.setHeader("Pragma","no-cache");// 把图片写给浏览器boolean write = ImageIO.write(image, "jpg",resp.getOutputStream());}// 生成随机数private String makeNum() {Random random = new Random();String num = random.nextInt(9999999) + "";// 随机数,最大七位,[0,9999999)StringBuffer sb = new StringBuffer();for (int i = 0; i < 7 - num.length(); i++) {// 不足七位,则添加0sb.append("0");}num = sb.toString()+num;// 不足七位,在随机数前面添加0return num;}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req, resp);}
}
  1. 实现 请求重定向
public class RedirectServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/*resp.setHeader("Location","/response_war/image");resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);*/resp.sendRedirect("/response_war/image");// 重定向相当于上面两行代码}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req, resp);}
}

6.7 HttpServletRequest

HttpServletRequest 代表客户端的请求,用户通过 Http 协议访问服务器,HTTP请求中的所有信息会被封装到 HttpServletRequest ,通过这个 HttpServletRequest 的方法,获得客户端的所有信息。

  1. 获取前端传递的参数
    1. getParameter(String name) | 获取指定名称的请求参数值(GET 或 POST 表单)
String username = request.getParameter("username");
String password = request.getParameter("password");
  1. getParameterValues(String name) | 用于获取多个值的参数(如多选框)
String[] hobbies = request.getParameterValues("hobby");
if (hobbies != null) {for (String hobby : hobbies) {System.out.println("兴趣爱好:" + hobby);}
}
  1. getParameterMap() | 返回所有参数的 Map 形式,键为参数名,值为字符串数组(适合处理复杂表单)
Map<String, String[]> parameterMap = request.getParameterMap();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) { //使用foreach语句循环使用System.out.println(entry.getKey() + " = " + Arrays.toString(entry.getValue()));
}
  1. getInputStream() / getReader() | 适用于接收 JSON、XML 等原始请求体内容(常用于前后端分离项目)
StringBuilder jsonBody = new StringBuilder();
BufferedReader reader = request.getReader();
String line;while ((line = reader.readLine()) != null) {jsonBody.append(line);
}System.out.println("接收到的JSON:" + jsonBody.toString());

注意:你需要使用 JSON 解析库(如 Jackson、Gson)来解析这个字符串。

  1. 获取路径参数(RESTful 风格) :request.getPathInfo()
@WebServlet("/user/*")
public class UserServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) {String pathInfo = request.getPathInfo(); // 如:/user/123 → 返回 "/123"if (pathInfo != null && pathInfo.length() > 1) {String userId = pathInfo.substring(1); // 去掉开头斜杠System.out.println("用户ID:" + userId);}}
}
  1. 完整代码如下:
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 获取普通参数String username = request.getParameter("username");String password = request.getParameter("password");// 获取多选参数String[] hobbies = request.getParameterValues("hobby");// 获取所有参数(Map)Map<String, String[]> parameterMap = request.getParameterMap();// 输出参数System.out.println("用户名:" + username);System.out.println("密码:" + password);if (hobbies != null) {for (String hobby : hobbies) {System.out.println("兴趣:" + hobby);}}// 处理 JSON 数据(如果需要)if ("application/json".equals(request.getContentType())) {BufferedReader reader = request.getReader();StringBuilder json = new StringBuilder();String line;while ((line = reader.readLine()) != null) {json.append(line);}System.out.println("JSON 内容:" + json);}
}
  1. 请求 转发

前端:

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>首页</title></head><body><form action="${pageContext.request.contextPath}/login" method="post">用户名:<input type="text" name="username"><br>密码:<input type="password" name="password"><br>爱好:<input type="checkbox" name="hobbys" value="代码"> 代码<input type="checkbox" name="hobbys" value="唱歌"> 唱歌<input type="checkbox" name="hobbys" value="女孩"> 女孩<input type="checkbox" name="hobbys" value="电影"> 电影<br><input type="submit" name="提交"></form></body></html>

后端:

public class LoginServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 处理请求中文乱码(后期可以使用过滤器来解决)req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");String username = req.getParameter("username");	 // 用户String password = req.getParameter("password");	 // 密码String[] hobbys = req.getParameterValues("hobbys"); // 爱好System.out.println(username);System.out.println(password);System.out.println(Arrays.toString(hobbys));// 这里的 / 代表当前的web应用,所以不需要再加上/request_war这个上下文路径了,否则会出现404错误 转发req.getRequestDispatcher("/success.jsp").forward(req,resp);}
}

web.xml

<servlet><servlet-name>login</servlet-name><servlet-class>com.sunyiwenlong.request.LoginServlet</servlet-class>
</servlet><servlet-mapping><servlet-name>login</servlet-name><url-pattern>/login</url-pattern>
</servlet-mapping>

这个 web.xml 配置定义了一个名为 login 的Servlet及其URL映射,对应的Java代码实现了基本的登录处理逻辑,包括获取请求参数和返回响应

面试题:请你聊聊 重定向 转发 的区别?
相同点:页面都会实现跳转
不同点:

请求转发的时候,URL地址栏不会产生变化。

  • 适合服务器内部跳转 。
  • 状态码: 307 (临时重定向)
  • RequestDispatcher.forward()
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 设置属性request.setAttribute("message", "这是转发时携带的消息");// 获取 RequestDispatcher 并转发RequestDispatcher dispatcher = request.getRequestDispatcher("/target.jsp");dispatcher.forward(request, response);
}

重定向时候,URL地址栏会发生变化。

  • 跳转到外部网站。
  • 状态码:302 ,301(永久重定向)
  • HttpServletResponse.sendRedirect()
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {// 重定向到另一个路径response.sendRedirect("http://example.com");  // 也可以是相对路径:"/myapp/target.jsp"
}

7. cookie/session

7.1 会话

无状态的会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话。
有状态的会话:一个用户打开一个浏览器,访问某些资源(网站),下次再来访问该资源(网站),我们会知道这个用户曾经来过,称之为有状态会话;

一个网站,怎么证明你来过?

  1. 服务端给客户端一个信件,客户端下次访问服务端带上信件就可以了;cookie(客户端)
  2. 服务器登记你来过了,下次你来的时候我来匹配你;session(服务端)

7.2 保存会话的两种技术

cookie:客户端技术,(响应、请求)
session:服务端技术,利用这个技术,可以保存用户的会话信息?我们可以把信息或者数据放在Session中。

用户登录↓
服务器创建 Session 并保存用户信息↓
服务器生成 JSESSIONID 并写入 Cookie↓
客户端保存 Cookie↓
后续请求携带 Cookie 到服务器↓
服务器根据 JSESSIONID 找到对应的 Session↓
继续处理用户逻辑

7.3 Cookie

  1. 从请求中拿到cookie
  2. 服务器响应给客户端cookie
Cookie[] cookies = req.getCookies();// 获得cookie
cookie.getName();// 获得cookie中的key
cookie.getValue();// 获得cookie中的value
new Cookie("lastLoginTime",System.currentTimeMills()+"");// 新建一个cookie
cookie.setMaxAge(24*60*60);// 设置cookie的有效期,单位:秒
resp.addCookie(cookie);// 响应给客户端一个cookie

Q: cookie:一般会保存在本地的用户目录下appdata,一个网站cookie是否存在上限!聊聊细节问题

  • 一个Cookie只能保存一个信息;
  • 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie;
  • Cookie大小有限制4kb
  • 300个cookie浏览器上限

删除cookie

  • 不设置有效期,关闭浏览器,自动失效
  • 设置有效期时间为 0

编码解码,怎么解决中文乱码问题

URLEncoder.encode("张三","UTF-8")
URLDecoder.decode("张三","UTF-8")

保存上一次登录时间** 实现方式 **

// 保存用户上一次访问的时间
public class CookieDemo01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 服务器告诉你,你来的时间,把这个时间封装成一个信息,你下次带来,我就知道你上次来的时间// 解决中文乱码req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");PrintWriter out = resp.getWriter();// Cookie,服务器端从客户端获取cookieCookie[] cookies = req.getCookies();// 数组,说明cookie可以有多个// 判断cookie是否if (cookies != null) {out.write("你上一次登录的时间是:");for (int i = 0; i < cookies.length; i++) {// 获取cookie的名字if (cookies[i].getName().equals("lastLoginTime")) {// 获取cookie的值long l = Long.parseLong(cookies[i].getValue());Date date = new Date(l);out.write(date.toLocaleString());}}} else {out.write("你是第一次登录!");}Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");cookie.setMaxAge(24*60*60);// 设置cookie的有效期为一天,单位是:秒resp.addCookie(cookie);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req, resp);}
}

7.4 Session (重点)

什么是Session:

  • 服务器会给每一个用户(浏览器)创建一个Seesion对象。
  • 一个session独占一个浏览器,只要浏览器没有关闭,这个session就存在。
  • 用户登录之后,整个网站它都可以访问。(保存用户的信息;也可以保存购物车的信息)

Session和cookie的区别

  • Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)保存在 客户端
  • Session把用户的数据写到用户独占 Session 中,服务器端 保存(保存重要的信息,减少服务器资源的浪费)
  • Session对象由服务( sevice )创建;

使用场景

  • 保存一个登录用户的信息;
  • 购物车信息;
  • 在整个网站中经常会使用的数据,我们将它保存在 Session 中;

会话自动过期

<!--设置session默认的失效时间-->
<session-config><!--15分钟后session自动失效,以分钟为单位--><session-timeout>15</session-timeout>
</session-config>

Session的使用

public class SessionDemo01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 解决中文乱码req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=utf-8");// 得到SessionHttpSession session = req.getSession();// 给session中存东西session.setAttribute("name", "张三");// 获取session的idString sessionId = session.getId();// 判断session是不是新创建if (session.isNew()) {resp.getWriter().write("session创建成功,ID:" + sessionId);} else {resp.getWriter().write("session已经存在了,ID:" + sessionId);}// session创建的时候做了什么事情/*Cookie cookie = new Cookie("JSESSIONID", sessionId);resp.addCookie(cookie);*///------------------// 从session中获取数据String name = (String) session.getAttribute("name");//------------------// 从session中删除指定name的数据session.removeAttribute("name");// 手动注销sessionsession.invalidate();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req, resp);}
}

http://www.hkcw.cn/article/OtiGJkTKiS.shtml

相关文章

【Day40】

DAY 40 训练和测试的规范写法 知识点回顾&#xff1a; 彩色和灰度图片测试和训练的规范写法&#xff1a;封装在函数中展平操作&#xff1a;除第一个维度batchsize外全部展平dropout操作&#xff1a;训练阶段随机丢弃神经元&#xff0c;测试阶段eval模式关闭dropout 作业&#x…

GEARS以及与基础模型结合

理解基因扰动的反应是众多生物医学应用的核心。然而&#xff0c;可能的多基因扰动组合数量呈指数级增长&#xff0c;严重限制了实验探究的范围。在此&#xff0c;图增强基因激活与抑制模拟器&#xff08;GEARS&#xff09;&#xff0c;将深度学习与基因-基因关系知识图谱相结合…

【C++】入门基础知识(1.5w字详解)

本篇博客给大家带来的是一些C基础知识&#xff01; &#x1f41f;&#x1f41f;文章专栏&#xff1a;C &#x1f680;&#x1f680;若有问题评论区下讨论&#xff0c;我会及时回答 ❤❤欢迎大家点赞、收藏、分享&#xff01; 今日思想&#xff1a;微事不通&#xff0c;粗事不能…

[SWPUCTF 2023 秋季新生赛]Classical Cipher203分古典密码Base家族栅栏密码

下载附件解压得到txt文件 得到信息 U2FsdGVkX19aQNEomnRqmmLlI9qJkzr0pFMeMBF99ZDKTF3CojpkTzHxLcu/ZNCYeeAV3/NEoHhpP5QUCK5AcHJlZBMGdKDYwko5sAATQ 用在线网站进行解密 解密得到 TGhmYlMlXXNwX2BTb3NoQWcye1VweSRfcXEGdmBheDx0I1BkMXdfXG0ldzdbGBy 栅栏密码用在线网站进行…

Unity 中实现首尾无限循环的 ListView

之前已经实现过&#xff1a; Unity 中实现可复用的 ListView-CSDN博客文章浏览阅读5.6k次&#xff0c;点赞2次&#xff0c;收藏27次。源码已放入我的 github&#xff0c;地址&#xff1a;Unity-ListView前言实现一个列表组件&#xff0c;表现方面最核心的部分就是重写布局&…

【提升工作效率的小工具】截图软件Snipaste

1.F1截图&#xff0c;F3钉在桌面上 2.小技巧 纯文本复制后&#xff0c;F3钉在桌面上&#xff0c;鼠标右键&#xff0c;点击复制纯文本&#xff0c;可以直接再次复制 shift鼠标双击截图&#xff0c;可以缩略显示不占位置&#xff0c;重复操作就是展开截图。 隐藏/显示所有贴图…

极刻云搜2.0-强大的蓝奏搜索引擎以及智能网址导航

【&#x1f389; 重磅发布】极刻云搜 2.0 正式上线&#xff01; &#x1f680; 核心升级&#xff1a; ✨ 界面全面焕新&#xff1a; 视觉更美观&#xff0c;操作更流畅&#xff0c;体验显著提升。 &#x1f50d; 搜索能力升级&#xff1a; 在原有站内搜索&#xff08;覆盖全站…

无人机视角海上漂浮物检测与人员救援检测数据集VOC+YOLO格式2903张6类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;2903 标注数量(xml文件个数)&#xff1a;2903 标注数量(txt文件个数)&#xff1a;2903 …

cocosCreator 1.8 升级到 2.4

现在负责的一个运营中的商业项目&#xff0c;使用的是 cocosCreator1.8&#xff0c;之前没有做好设计&#xff0c;所以东西都是直接加载在内存中的&#xff0c;到了现在性能问题逐渐暴露出来&#xff0c;讨论之后想进行引擎升级&#xff0c;升级到cocosCreator 2.4。 官方的升…

【递归、搜索与回溯算法】综合练习(二)

&#x1f4dd;前言说明&#xff1a; 本专栏主要记录本人递归&#xff0c;搜索与回溯算法的学习以及LeetCode刷题记录&#xff0c;按专题划分每题主要记录&#xff1a;&#xff08;1&#xff09;本人解法 本人屎山代码&#xff1b;&#xff08;2&#xff09;优质解法 优质代码…

65.AI流式回答后再次修改同一界面的消息不在同一对话中bug

问题背景 在实现AI对话应用的流式响应功能后&#xff0c;我发现一个关键问题&#xff1a;当用户对AI的回答进行修改或重新生成时&#xff0c;有时会导致新的回答不在原对话上下文中&#xff0c;而是创建了一个新的独立对话。这种bug会严重影响用户体验和对话的连贯性。 问题现…

YOLOv8目标检测实战-(TensorRT原生API搭建网络和使用Parser搭建网络)

文章目录 一、原理篇1&#xff09;Trt基础知识2&#xff09;Trt plugin3&#xff09;int8量化算法和原理4&#xff09;cuda编程5&#xff09;onnx基础知识6&#xff09;yolov8网络架构6.1 yolov5网络架构图6.2 yolov8s网络架构 二、TensorRT原生API搭建网络1&#xff09;window…

【IC】ASIC 设计流程:什么是 ASIC 设计?

什么是 ASIC&#xff1f; ASIC&#xff08;专用集成电路&#xff09;是一种经过精心设计的专用集成电路&#xff0c;用于在电子系统中执行特定功能或功能集。与微波炉或电视盒等日常电子设备中的通用微处理器不同&#xff0c;ASIC 是为特定应用量身定制的&#xff0c;可提供无…

TKdownloader V5.5 抖音批量下载工具

目前能找到的仅存的免费抖音批量下载软件&#xff0c;有win版和mac版。 但是软件的运行需要一点点电脑知识&#xff0c;不太复杂&#xff0c;按着说明一步一步走&#xff0c;也能正常安装使用。 项目功能 下载抖音无水印视频/图集 下载抖音无水印实况/动图 下载最高画质视频文件…

Rust 编程实现猜数字游戏

文章目录 编程实现猜数字游戏游戏规则创建新项目默认代码处理用户输入代码解析 生成随机数添加依赖生成逻辑 比较猜测值与目标值类型转换 循环与错误处理优化添加循环优雅处理非法输入​ 最终完整代码核心概念总结 编程实现猜数字游戏 我们使用cargo和rust实现一个经典编程练习…

苏州SAP代理公司排名:工业园区企业推荐的服务商

目录 一、SAP实施商选择标准体系 1、行业经验维度 2、实施方法论维度 3、资质认证维度 4、团队实力维度 二、SAP苏州实施商工博科技 1、SAP双重认证&#xff0c;高等院校支持 2、以SAP ERP为核心&#xff0c;助力企业数字化转型 三、苏州使用SAP的企业 苏州是中国工业…

2505软考高项第一、二批真题终极汇总

第一批2025.05综合题&#xff08;75道选择题&#xff09; 1、2025 年中央一号文件对进一步深化农村改革的各项任务作出全面部署。“推进农业科技力量协同攻关”的相关措施不包括()。 A.强化农业科研资源力量统筹&#xff0c;培育农业科技领军企业 B.发挥农业科研平台作用&…

微深节能 堆取料机动作综合检测系统 格雷母线

精准定位&#xff0c;高效运行——微深节能格雷母线堆取料机动作综合检测系统 在现代工业自动化领域&#xff0c;精准的位置检测是保障设备高效运行的关键。武汉市微深节能科技有限公司推出的格雷母线高精度位移测量系统&#xff0c;凭借其卓越的性能和可靠性&#xff0c;成为…

Android Native 之 adbd进程分析

目录 1、adbd守护进程 2、adbd权限降级 3、adbd命令解析 1&#xff09;adb shell 2&#xff09;adb root 3&#xff09;adb reboot 4、案例 1&#xff09;案例之实现不需要执行adb root命令自动具有root权限 2&#xff09;案例之实现不需要RSA认证直接能够使用adb she…

wireshark分析国标rtp ps流

1.将抓到的tcp或者udp视频流使用decode as 转为rtp包 2.电话->RTP->RTP播放器 选择Export 里面的Payload 就可以导出原始PS流