javaweb--cookie

package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public abstract class BaseServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决post请求中文乱码问题
        // 一定要在获取请求参数之前调用才有效
        req.setCharacterEncoding("UTF-8");
        // 解决响应中文乱码问题
        resp.setContentType("text/html; charset=UTF-8");

        String action = req.getParameter("action");
        try {
            // 获取action业务鉴别字符串,获取相应的业务 方法反射对象
            Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
//            System.out.println(method);
            // 调用目标业务 方法
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
package servlet;

import util.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author nkc
 * @create 2022-05-01 12:20
 */
public class CookieServlet extends BaseServlet {

    protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("path1", "path1");
        cookie.setPath(req.getContextPath()+"/abc");
        resp.addCookie(cookie);
        resp.getWriter().write("创建了一个带有Path路径的Cookie");
    }
    protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("life3600", "life3600");
        cookie.setMaxAge(60*60);
        resp.addCookie(cookie);
        resp.getWriter().write("已经创建了存活一小时的Cookie");
    }
    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先找到要删除的
        Cookie cookie = CookieUtils.findCookie("key2", req.getCookies());
        if(cookie!=null){
            cookie.setMaxAge(0);
            resp.addCookie(cookie);
            resp.getWriter().write("key2又被删除了!!!");
        }
    }
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defaultLife", "defaultLift");
        cookie.setMaxAge(-1);
        resp.addCookie(cookie);
    }
    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //方案一:
        //先创建一个要修改的同名的Cookie对象
        //在构造器,同时赋予新的Cookie值
        //Cookie cookie = new Cookie("key1", "newValue1");
        ////通知客户端
        //resp.addCookie(cookie);
        //resp.getWriter().write("key1的cookie已经修改好");
        
        
        //方案二:
        Cookie key2 = CookieUtils.findCookie("key2", req.getCookies());
        if(key2!=null){
            //调用setValue方法赋予新的cookie值
            key2.setValue("newNewValue2");
            resp.addCookie(key2);
            resp.getWriter().write("key2的值又又又更新啦");
        }
    }
    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            //getName方法返回cookie的key(名)
            //getValue方法返回cookie的value(值)
            resp.getWriter().write("Cookie["+ cookie.getName()+"="+cookie.getValue()+"]<br/>");
        }

        Cookie iWantCookie = CookieUtils.findCookie("key1", cookies);
        
        if(iWantCookie!=null){
            resp.getWriter().write("找到了指定的Cookie");
        }
        

    }

    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建cookie对象
        Cookie cookie = new Cookie("key1", "value1");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie);
        resp.getWriter().write("Cookie创建成功");

        //1.创建cookie对象
        Cookie cookie2 = new Cookie("key2", "value2");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie2);
        resp.getWriter().write("Cookie2创建成功");
    }
}
package servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 
 * 
 * @create 2022-05-01 14:48
 */
public class LoginServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        
        if("hahaha".equals(username) && "123456".equals(password)){
            //登录成功
            Cookie cookie = new Cookie("username", username);
            cookie.setMaxAge(60*60*24*7);
            resp.addCookie(cookie);
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
    }
}
<%--
  Created by IntelliJ IDEA.
  User: 86156
  Date: 2022/5/1
  Time: 14:45
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="http://localhost:8080/13_cookie_session/loginServlet" method="get">
        用户名:<input type="text" name="username" value="${cookie.username.value}"> <br>
        密码:<input type="password" name="password" > <br>
        <input type="submit" value="登录">
    </form>
</body>
</html>
posted @ 2022-05-01 14:59  ice--cream  阅读(19)  评论(0编辑  收藏  举报