Cookie
Cookie: 客户端会话技术, 将数据保存在客户端, 以后每次请求都携带Cookie进行访问
Cookie的基本使用之发送
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//发送cookie
//1.创建cookie对象
Cookie cookie = new Cookie("username", "zs");
//2.发送cookie
response.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
Cookie的基本使用之获取
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/bServlet")
public class BServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从浏览器获取cookie
//1.获取cookie数组
Cookie[] cookies = request.getCookies();
//遍历数组
for (Cookie cookie : cookies) {
//获取数据
String name = cookie.getName();
if ("username".equals(name)) {
String value = cookie.getValue();
System.out.println(name + ":" + value);
break;
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
Cookie细节:存活时间
//1.创建cookie对象
Cookie cookie = new Cookie("username", "zs");
//cookie存活时间
cookie.setMaxAge(7 * 24 * 60 *60);//设置为一个星期
//2.发送cookie
response.addCookie(cookie);
//setMaxAge()里单位是秒 负数代表存在内存里, 正数代表存在硬盘里, 0代表销毁
Cookie细节:存储中文
在发送Cookie时通过 URLEncoder 将字符串编码
接收Cookie时再通过 URLDecoder 解码
//发送Cookie时编码
String value = "张三";
//URL编码
value = URLEncoder.encode(value, "UTF-8");
//接收Cookie时解码
String value = cookie.getValue();
value = URLDecoder.decode(value, "UTF-8");
Session
基本使用
//存数据
package com.itheima.web.session;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/demo1")
public class ASessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//存储到session中
//1.获取session对象
HttpSession session = request.getSession();
//2.存储数据
session.setAttribute("username", "zs");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
//取数据
package com.itheima.web.session;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/demo2")
public class BSessionServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//从session中读取数据
//得到session
HttpSession session = request.getSession();
//从session1中取数据
Object username = session.getAttribute("username");
System.out.println(username);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
Session使用细节
Session钝化, 活化:
服务器正常重启后Session中的数据还在
钝化: 正常关闭服务器后Session数据写入硬盘
活化: 再次启动服务器后从文件加载数据到Session中
Session销毁:
默认情况下无操作30分钟销毁
<session-config>
<session-timeout>30</session-timeout>
</session-config>
在web.xml里配置这三行代码修改Session销毁时间
或者调用Session对象的invalidate()方法手动删除