【Javaweb】实现Cookie关于当前时间显示的Demo

实现Cookie关于当前时间显示的Demo

使用Servlet实现。

java代码:

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;

public class CookieDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //解决乱码问题
        req.setCharacterEncoding("utf-16"); 
        resp.setCharacterEncoding("utf-16");

        PrintWriter out = resp.getWriter();

        //cookie,服务器从客户端获取
        Cookie[] cookies = req.getCookies();  //返回数组,说明cookie存在多个

        if(cookies!=null){
            out.write("你上一次访问的时间是:");
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                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() + "");// 通过+""将输出的当前时间转化为字符串
        resp.addCookie(cookie);

    }


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

对相关的Servlet进行注册

web.xml内容:



<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>CookieDemo01servlet-name>
    <servlet-class>com.donric.servlet.CookieDemo01servlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>CookieDemo01servlet-name>
    <url-pattern>/cookieurl-pattern>
  servlet-mapping>

web-app>

最后配置一下tomcat就好了。

你可能感兴趣的:(JAVAWEB)