从请求中拿到cookie信息,服务器响应给客户端cookie
- package com.wyh.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;
- import java.io.PrintWriter;
- import java.util.Date;
-
- /**
- * @author WuYongheng
- * @date 2022/9/1
- * @description
- */
- public class CookieDemo1 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("gbk");
- resp.setCharacterEncoding("gbk");
-
- PrintWriter out = resp.getWriter();
-
- Cookie[] cookies = req.getCookies();
-
- if (cookies != null) {
- out.write("你上一次的访问时间是:");
- for (int i = 0; i < cookies.length; i++) {
- Cookie cookie = cookies[i];
- // 获取cookie的名字
- if (cookie.getName().equals("lastLoginTime")) {
- // 获取cookie的值
- Long lastLoginTime = Long.parseLong(cookie.getValue());
- Date date = new Date(lastLoginTime);
- out.write(date.toLocaleString());
- }
- }
- } else {
- out.write("这是第一次访问网站");
- }
- // 服务器给客户端响应一个cookie
- Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
- // 设置超时时间,为1天
- cookie.setMaxAge(24 * 60 * 60);
- resp.addCookie(cookie);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
- }
- }
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
- http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0"
- metadata-complete="true">
- <servlet>
- <servlet-name>CookieDemo1servlet-name>
- <servlet-class>com.wyh.servlet.CookieDemo1servlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>CookieDemo1servlet-name>
- <url-pattern>/cookieurl-pattern>
- servlet-mapping>
- web-app>
启动浏览器的一瞬间,session就生成了
- package com.wyh.servlet;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.io.IOException;
-
- /**
- * @author WuYongheng
- * @date 2022/9/1
- * @description
- */
- public class SessionDemo1 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- // 解决乱码问题
- req.setCharacterEncoding("gbk");
- resp.setCharacterEncoding("gbk");
- resp.setContentType("text/html;charset=gbk");
-
- // 得到Session
- HttpSession session = req.getSession();
- // 给Session里面存东西
- session.setAttribute("name", "巫永恒");
- System.out.println(session.getAttribute("name"));
- // 获取Session的id
- String sessionId = session.getId();
- //判断Session是不是新的
- if (session.isNew()) {
- resp.getWriter().write("session创建成功,ID+" + sessionId);
- } else {
- resp.getWriter().write("session已经在服务器存在了,id:" + sessionId);
- }
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doGet(req, resp);
- }
- }