一个网站,怎么证明你来过?
客户端 --- 服务端
-
服务端给客户端一个 信件,客户端下次访问服务端带上信件就可以了; cookie
-
服务器登记你来过了,下次你来的时候我来匹配你; seesion
CookieDemo类代码:
package com;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class CookieDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求和响应的编码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//请求一个Cookie,返回数组类型,说明Cookie可以有多个
Cookie[] cookies = req.getCookies();
//一个Cookie对象
Cookie cookie = new Cookie("name", URLEncoder.encode("管",
StandardCharsets.UTF_8));//对Value值编码
//设置Cookie存活周期,不设置有效期,关闭浏览器,自动失效; 设置有效期时间为 0 响应给浏览器后立马消失;
cookie.setMaxAge(60*2);
if (cookies != null) {
resp.getWriter().write("Cookie");
resp.getWriter().write("key:"+cookie.getName()+",");//获取Cookie的Key值
resp.getWriter().write("value:"+URLDecoder.decode(cookie.getValue(),
StandardCharsets.UTF_8));//对获取到的Value值解码、输出
} else {
resp.getWriter().write("这是你第一次访问");
}
resp.addCookie(cookie);//响应一个Cookie
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
碰到的问题案例
顺序应该是:接受到Cookie请求---创建一个Cookie对象---对Cookie进行赋key、值---将Cookie响应回服务器
web.xml配置文件代码
<?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet>
<servlet-name>CookieDemo</servlet-name>
<servlet-class>com.CookieDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookieDemo</servlet-name>
<url-pattern>/cookiedemo</url-pattern>
</servlet-mapping>
</web-app>
遇见的问题:在设置了编码解码的情况下页面依旧显示乱码,idea后台输出不乱码
说明浏览器编码问题,edga浏览器为例;
解决:修改浏览器的编码
Session
什么是Session:
-
服务器会给每一个用户(浏览器),创建一个Session对象;
-
一个Session独占一个浏览器,只要浏览器页面没有关闭,这个Session就存在;
-
用户登录之后,整个网站他都可以访问的!→保存用户的信息,保存购物车的信息。。。
Session跟Cookie的区别:
-
Cookie是把用户的数据写给用户的浏览器,浏览器保存
-
session 把用户的数据写到用户独占Session 中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
-
Session对象由服务器对象创建;
使用场景:
-
保存一个登录用户的信息;
-
购物车信息;
-
在整个网站中经常会使用的数据,我们将他保存在Session中;
sessiondemo代码:
package com;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo extends HttpServlet {
@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");
//获取session
HttpSession reqSession = req.getSession();
//获取session的 id
String sessionId = reqSession.getId();
//往session存数据
reqSession.setAttribute("name","管");
//判断session是否为新建的
if(reqSession.isNew()){
//输出session的ID、Value
resp.getWriter().write("session创建成功,ID:"
+sessionId
+reqSession.getAttribute("name").toString()
);
}
else {
resp.getWriter().write("session已存在,ID:"
+sessionId
+reqSession.getAttribute("name").toString()
);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
会话自动过期demo:
package com;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
public class demo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取session
HttpSession reqSession = req.getSession();
//移除session属性
reqSession.removeAttribute("name");
//使session失效
reqSession.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml设置
更详细的见解可以借鉴标签:jakarta,resp,req,Session,Cookie,import,servlet From: https://www.cnblogs.com/mo-de/p/16730748.html
https://www.cnblogs.com/l199616j/p/11195667.html#_label1_2