package com.hqyj.gwr.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.net.URLDecoder; import java.net.URLEncoder; import java.util.Date; @WebServlet("/zy01") public class zy01 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); System.out.println(username); String password = request.getParameter("password"); System.out.println(password); Cookie[] cookies=request.getCookies(); // for (Cookie cookie:cookies){ // String name=cookie.getName(); // String value=cookie.getValue(); // String decode = URLDecoder.decode(value, "utf-8"); // System.out.println(name+":"+decode); // } if ("zhangsan".equals(username)&&"123456".equals(password)&&cookies.length==3){ response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write("这是你的第一次登录"); Date date = new Date(); String da=date.toString(); String encode = URLEncoder.encode(da,"utf-8"); Cookie cookie = new Cookie("username", encode); cookie.setMaxAge(30); response.addCookie(cookie); String map=da; response.setContentType("text/html;charset=utf-8"); PrintWriter writer1 = response.getWriter(); writer1.write("登录时间:"+map); }else { response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); String value=cookies[3].getValue(); String decode = URLDecoder.decode(value, "utf-8"); writer.write("上次登录时间:"+decode); Date date = new Date(); String da=date.toString(); String encode = URLEncoder.encode(da,"utf-8"); Cookie cookie = new Cookie("username", encode); cookie.setMaxAge(30); response.addCookie(cookie); } } }
cookie进行登录时间返回
最新推荐文章于 2023-07-11 20:22:31 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)