13-Session
13-Session
什么是Session?
Session即会话,打开浏览器到关闭浏览器之间的所有的操作就是一次会话
服务器会给每一个用户(浏览器)创建一个Session
一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在
用户登录之后,整个网站它都可以访问
Session vs Cookie
- session中存的数据格式是(String,Object),Cookie是存的(String,String)
- SessionID是以一条Cookie存在的
- Session中可以存attribute
实践
父pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>JavaWeb-Study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>01-HelloServlet</module>
<module>02-ServletContext</module>
<module>03-Download</module>
<module>04-VerificationCode</module>
<module>05-Login</module>
<module>06-Cookie</module>
<module>07-Session</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
</project>
子pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.example</groupId>
<artifactId>JavaWeb-Study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>07-Session</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>07-Session Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
</project>
package com.kuang.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 功能描述
*
* @since 2022-08-19
*/
public class ClearSession extends HttpServlet {
private static final long serialVersionUID = 226104837611295423L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
package com.kuang.servlet;
import com.kuang.pojo.Person;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 功能描述
*
* @since 2022-08-19
*/
public class ReadSessionAttr extends HttpServlet {
private static final long serialVersionUID = -8108269474781201057L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
Person kuang = (Person) session.getAttribute("name");
System.out.println(kuang);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
package com.kuang.servlet;
import com.kuang.pojo.Person;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 功能描述
*
* @since 2022-08-19
*/
public class SessionDemo1 extends HttpServlet {
private static final long serialVersionUID = 2958609145047320295L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
HttpSession session = req.getSession();
String sessionId = session.getId();
session.setAttribute("name", new Person("kuang", 18));
PrintWriter writer = resp.getWriter();
writer.write("你的sessionId为:" + sessionId);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0"
metadata-complete="true">
<servlet>
<servlet-name>demo1</servlet-name>
<servlet-class>com.kuang.servlet.SessionDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo1</servlet-name>
<url-pattern>/demo1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>demo2</servlet-name>
<servlet-class>com.kuang.servlet.ReadSessionAttr</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo2</servlet-name>
<url-pattern>/demo2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>demo3</servlet-name>
<servlet-class>com.kuang.servlet.ClearSession</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo3</servlet-name>
<url-pattern>/demo3</url-pattern>
</servlet-mapping>
</web-app>