实现获取用户上一次访问时间的功能,当用户访问服务器,如果是非第一次访问的话,需要将上次访问服务器的时间给用户显示出来

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <welcome-file-list>
        <welcome-file>login.html</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>com.exercise.login.Login</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servlet-mapping>
</web-app>

 核心代码 

package com.exercise.login;


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.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符编码方式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String lastAccessTime=null;
        Cookie[]cookies=request.getCookies();
        // 遍历Cookie数组,取出上次访问时间
        for(int i=0;cookies!=null&&i<cookies.length;i++){
            if("lastAccess".equals(cookies[i].getName())){
                lastAccessTime=URLDecoder.decode(cookies[i].getValue());
                break;
            }
        }
        // 用户第一次请求时,lastAccessTime为null
        // 非第一次请求时,lastAccessTime不为null
        if(lastAccessTime==null){
            response.getWriter().print("你是首次访问本站!");
        }else {
            response.getWriter().print("你上次的访问时间是:"+lastAccessTime);
        }
        // 每次进入都需要将当前时间更新进Cookie,覆盖原来记录的时间
        // 设置过期时间 10天
        /**
         * 关于设置Cookie的value时报错:Cookie值中存在无效字符 的问题
         * 此处应在设置时统一编码和解码方式:
         * Cookie cookie=new Cookie("lastAccess", URLEncoder.encode(currentTime));
         * lastAccessTime=URLDecoder.decode(cookies[i].getValue());
         */
        String currentTime=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        Cookie cookie=new Cookie("lastAccess", URLEncoder.encode(currentTime));
        cookie.setMaxAge(10*24*60*60);
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小馒头爱学Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值