Cookie
在服务器中写一个
Cookie
键值对,然后把它扔进响应值中,于是在浏览器访问当前web项目时会得到响应值,并且把这个响应值中的Cookie数据
存在浏览器中,下次,在服务器中就可以先获得刚才的Cookie数据
,然后就可以以这个Cookie数据
为标记,共享数据
简单的代码实现:
- 创建Cookie数据,浏览器保存,控制台输出刚才创建的Cookie数据
即先访问co1
,再访问co2
- 服务器将Cookie数据放进响应头中,再给浏览器
浏览器将Cookie数据放入请求头中,再给服务器
- 创建Cookie数据:
package com.itjh.servlet;
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;
import java.io.IOException;
@WebServlet("/co1")
public class Servletcook1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie=new Cookie("name","zhangsan");
resp.addCookie(cookie);
}
}
- 输出Cookie数据:
package com.itjh.servlet;
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;
import java.io.IOException;
@WebServlet("/co2")
public class Servletcook2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie[] cookies =req.getCookies();
for (Cookie c:cookies
) {
if (c.getName().equals("name")){
System.out.println(c.getName()+":"+c.getValue());
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
结果:
[INFO] Running war on http://localhost:8080/filterday01
[INFO] Using existing Tomcat server configuration at C:\Users\李家院刘俊辉\new start\filterday01\target\tomcat
[INFO] create webapp with contextPath: /filterday01
四月 10, 2023 11:37:57 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
四月 10, 2023 11:37:57 上午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Tomcat
四月 10, 2023 11:37:57 上午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.47
四月 10, 2023 11:37:58 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
name:zhangsan
存储中文时,需要进行转码:
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;
import java.io.IOException;
import java.net.URLEncoder;
@WebServlet("/co1")
public class Servletcook1 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String value="王五";
//将中文数据转换成utf-8格式,网页才能识别,
//就是指网页能正常打开,
//于是进行编码
value=URLEncoder.encode(value,"UTF-8");
Cookie cookie=new Cookie("name",value);
resp.addCookie(cookie);
}
}
package com.itjh.servlet;
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;
import java.io.IOException;
import java.net.URLDecoder;
@WebServlet("/co2")
public class Servletcook2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie[] cookies =req.getCookies();
for (Cookie c:cookies
) {
if (c.getName().equals("name")){
String cc=c.getValue();
//未进行转码,所以得到的乱码
System.out.println(cc);
//将得到的数据进行转码,否则得到时乱码
cc=URLDecoder.decode(cc,"UTF-8");
System.out.println(c.getName()+":"+cc);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
想要定义时间:
定义Cookie数据的时候就可以进行
Cookie数据
再浏览器上存在时间的设置
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String value="王五";
value=URLEncoder.encode(value,"UTF-8");
Cookie cookie=new Cookie("name",value);
//一天的时间
cookie.setMaxAge(60*60*24);
resp.addCookie(cookie);
}
Session
- 先获得Session域,然后再进行设置或者获得
- 怎么保证每一次获得的Session对象都是同一个呢?
因为Session是基于Cookie来实现的,TomCat在拿到Session数据之后就会将当时前的数据打包成Cookie数据,然后再放在响应头中交给浏览器,然后浏览器访问服务器时,就会将这个Cookie数据放入请求头中,服务器看Session域中有没有这个数据,没有的话就自己创建一个,综上所述,就能是同一个Session对象了
代码实现:
package com.itjh.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/servletsession1")
public class servletsession1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得Session对象
HttpSession httpSession=request.getSession();
//设置键值对
httpSession.setAttribute("age","28");
System.out.println("doget。。1。。。");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
package com.itjh.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/servletsession2")
public class servletsession2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得Session对象
HttpSession httpSession=request.getSession();
//通过键值对得到Session域中的内容
Object age=httpSession.getAttribute("age");
System.out.println(age);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}