Cookie是浏览器在本地持久化存储数据的一种机制
1.Cookie的数据从哪来?
服务器返回给浏览器
2.Cookie的数据是什么样的?
Cookie中是键值对结构的数据 并且这里的键值对都是程序员自定义的
3.Cookie的作用?
Cookie可以在浏览器存储一些"临时性的数据"
其中典型的一种使用方式就是用来存储"身份标识"
( 涉及到Cookie和Session之间的联动 Cookie浏览器存储数据 Session服务器存储数据 存储用户详细信息同时给这个用户分配一个sessionId(唯一值) 后续再访问该网站的其他页面的时候请求中就会自动带上刚才sessionId进一步的服务器就可以知道当前是那个用户在操作了)
4.Cookie到哪去?
Cookie的内容会在下次访问该网站的时候 自动的被带到HTTP请求中
5.Cookie怎么存储?
浏览器按照不同的"域名"分别存储Cookie域名和域名之间Cookie是不能干扰的 Cookie存储在硬盘上会有一个超时时间
实例:
Cookie操作:
HttpServletRequest:(请求中所有Cookie内容)
方法 | 描述 |
---|---|
String getName() | 该方法返回cookie的名称 名称在创建后不能改变 |
String getValue() | 该方法获取与cookie关联的值 |
void setValue(String newValue) | 该方法设置与cookie |
void addCookie(Cookie cookie) | 把指定的cookie添加到响应 |
期望通过这个doGet方法 把一个自定义的cookie数据返回浏览器这边
@WebServlet("/setCookie")
public class SetCookieServlet extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//期望通过这个doGet方法 把一个自定义的cookie数据返回浏览器这边
Cookie cookie = new Cookie("date","2024-2-20");
resp.addCookie(cookie);
Cookie cookie1 = new Cookie("time","10:00");
resp.addCookie(cookie1);
resp.getWriter().write("setCookie ok");
}
}
获取到这次请求中的Cookie
@WebServlet("/getCookie")
public class GetCookieServlet extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取到这次请求中的Cookie
Cookie[] cookies = req.getCookies();
if(cookies != null){
for (Cookie cookie : cookies){
System.out.println(cookie.getName()+":"+cookie.getValue());
}
}
resp.getWriter().write("ok");
}
}
访问setCookie和getCookie
然后我们就可以发现服务器能拿到cookie的内容了
Servlet也提供了Session相关的支持
方法 | 描述 |
---|---|
HttpSession getSession | 在服务器中获取会话 如果有则能查询到session 如果没有就会创建出session |
Object getAttribute(String name) | 该方法返回在该session会话中具有指定名称的对像 |
void setAttribute(String name,Object value) | 该方法使用指定的名称绑定一个对象到该session会话 |
boolean isNew() | 判定当前是否新创建出的会话 |
通过一个登录页面使用上述api
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录</title>
</head>
<body>
<form action="login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="登录">
</form>
</body>
</html>
当前是一个from表单 当用户点击 登录按钮 就会发起一个http请求 例如:
POST login Content-Type: application/x-www-form-urlencoded
username=张三&password=111
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取到用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
if(username == null || password == null || username.equals("") || password.equals("")){
resp.setContentType("text/html; charset=utf8");
resp.getWriter().write("请求的参数不完整");
return;
}
//2.验证用户名密码是否正确了
//正常验证 是从数据库读取数据
//此处约定合法用户名是zhangsan 密码是111
if(!username.equals("zhangsan")){
resp.setContentType("text/html; charset=utf8");
resp.getWriter().write("用户名错误!");
return;
}
if(!password.equals("123")){
resp.setContentType("text/html; charset=utf8");
resp.getWriter().write("密码错误!");
return;
}
//3.登陆成功
HttpSession session = req.getSession(true);
//在会话中 可以随便保存点自定义的数据 比如保存在一个登录时间戳
//setAttribute 后面的value是个Object
session.setAttribute("username",username);
session.setAttribute("time",System.currentTimeMillis());
//4.让页面自动跳到网页主站
resp.sendRedirect("index");
}
}
@WebServlet("/index")
public class IndexServlet extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(false);
if(session == null){
//用户未登陆
resp.setContentType("text/html; charset=utf8");
resp.getWriter().write("请先登录,在访问主页!");
return;
}
//已经登录成功 取出之前的attribute
String username = (String) session.getAttribute("username");
Long time = (Long) session.getAttribute("time");
System.out.println("username=" + username + ", time=" + time);
//根据这样的内容构造页面
resp.setContentType("text/html; charset=utf8");
resp.getWriter().write("登陆成功"+username+"上次登录"+time);
}
}
第一次访问直接进入index页面
然后我们访问login.html 输入数据
然后就会跳转
当前程序涉及三个部分
1.登录页面(静态的html 使用form构造http请求)
2.LoginServlet(doPost处理登录的逻辑流程)
3.IndexServlet(doGet处理主页的生成)