cookie and session
- 一、cookie
- 1. 会话技术
- 2. 什么是cookie
- 3. cookie的属性
- 4. cookie方法
- 5. cookie添加和获取
- 6. 需求案例
- 7. cookie的细节
- 二、session
- 1. HttpSession介绍
- 2. HttpSession常用方法
- 3. HttpSession获取
- 4. HttpSession使用
- 5. HttpSession使用细节
一、cookie
cookie:客户端会话管理技术
1. 会话技术
2. 什么是cookie
一个普通的java类
3. cookie的属性
4. cookie方法
5. cookie添加和获取
6. 需求案例
需求:
通过cookie记录最后访问时间,并在浏览器上显示出来
目的:
掌握cookie的基本使用,从创建到添加客户端,再到从服务端获取。
实现步骤:
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.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* cookie 的使用
*/
@WebServlet("/test")
public class Test extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 通过响应对象写出提示信息
resp.setContentType("text/html;charset=UTF-8");
PrintWriter pw = resp.getWriter();
pw.write("欢迎访问本网站,您最后的访问时间为:<br>");
//2.创建cookie对象,用于记录最后时间
Cookie cookie=new Cookie("time",System.currentTimeMillis()+"");
//3.设置最大存活时间 1h
cookie.setMaxAge(3600);
//4. 将cookie对象添加到客户端
resp.addCookie(cookie);
//5. 获取cookie
Cookie[] arr = req.getCookies();
for (Cookie c:arr){
if ("time".equals(c.getName())){
//6. 获取cookie对象中的value
String value = c.getValue();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
pw.write(sdf.format(new Date(Long.parseLong(value))));
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
7. cookie的细节
cookie路径限制:创建cookie的路径 获取要一致 ,/servlet开始创建的话,获取也要以此开头
二、session
1. HttpSession介绍
HttpSession服务器会话管理技术,是一个接口
2. HttpSession常用方法
3. HttpSession获取
4. HttpSession使用
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* seesion基本使用
**/
@WebServlet("/demo01")
public class ServletDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取请求的用户名
String username = req.getParameter("username");
//2. 获取HttpSession的对象
HttpSession session = req.getSession();
System.out.println(session);
System.out.println(session.getId());
//3.将用户名信息添加到共享数据中
session.setAttribute("username",username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* seesion基本使用
**/
@WebServlet("/demo02")
public class ServletDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 获取HttpSession的对象
HttpSession session = req.getSession();
System.out.println(session);
System.out.println(session.getId());
//2.获取共享数据
Object username = session.getAttribute("username");
//3.将数据响应给浏览器
resp.getWriter().write(username+"");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
是同一个session对象
成功:
5. HttpSession使用细节