news 2026/7/24 20:12:12

IDEA(2020版)sevlet+cookie实现显示上次访问时间

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
IDEA(2020版)sevlet+cookie实现显示上次访问时间

查看全文请点击:https://www.longkui.site/program/java/idea2020sevletcookie/7197/


【任务目标】

当用户访问某些Web应用时,经常会显示出该用户上一次访问时间。例如,QQ登录成功后,会显示上次的登录时间。本案例要求使用Cookie技术显示用户上次访问时间。

1.创建Servlet
右击src,选择New—>Create New Servlet,名字为LastAccessServlet


参考代码如下:

importjava.io.IOException;importjava.net.URLDecoder;importjava.net.URLEncoder;importjava.text.SimpleDateFormat;importjava.util.Date;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.*;@WebServlet(name="LastAccessServlet",urlPatterns="/LastAccessServlet")public class LastAccessServlet extends HttpServlet{private static final long serialVersionUID=1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{//指定服务器输出内容的编码方式UTF-8,防止发生乱码 response.setContentType("text/html;charset=utf-8");//获取所有cookie Cookie[]cookies=request.getCookies();//定义flag的boolean变量,用于判断cookies是否为空 booleanflag=false;//遍历cookie数组 if(cookies.length>0&&cookies!=null){for(Cookie cookie:cookies){//获取cookie的名称 Stringname=cookie.getName();//判断名称是否是lastTime if("lastTime".equals(name)){//有该cookie不是第一次访问flag=true;//响应数据 //获取cookie的value时间 Stringvalue=cookie.getValue();System.out.println("解码前:"+value);//URL解码value=URLDecoder.decode(value,"utf-8");System.out.println("解码后:"+value);response.getWriter().write("欢迎回来,您上次访问时间为:"+value);//设置cookie的value //获取当前时间的字符串,重新设置cookie的值,重新发送cookie Datedate=new Date();SimpleDateFormattimesdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Stringstr_time=timesdf.format(date);System.out.println("编码前:"+str_time);//URL编码str_time=URLEncoder.encode(str_time,"utf-8");System.out.println("编码后:"+str_time);cookie.setValue(str_time);//设置cookie存活时间 cookie.setMaxAge(60*60*24*30);//一个月 //加入当前cookie请求时间 response.addCookie(cookie);break;}}//如果cookies中没有时间,也就是没有访问过 if(cookies==null||cookies.length==0||flag==false){//设置cookie的value //获取当前时间的字符串,重新设置cookie的值,重新发送cookie Datedate=new Date();SimpleDateFormatsdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Stringstr_date=sdf.format(date);System.out.println("编码前:"+str_date);//URL编码str_date=URLEncoder.encode(str_date,"utf-8");System.out.println("编码后:"+str_date);Cookiecookie=new Cookie("lastTime",str_date);//设置cookie存活时间 cookie.setMaxAge(60*60*24*30);//一个月 response.addCookie(cookie);response.getWriter().write("您好,欢迎您首次访问");}}}public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{this.doPost(request,response);}}

2.实现效果如下
启动tomcat,在浏览器里输入

http://localhost:8080/Servlet01_war_exploded/LastAccessServlet

效果如下:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/19 4:07:10

Git stash临时保存未提交更改的使用场景

Git stash临时保存未提交更改的使用场景 在日常开发中,你是否遇到过这样的情况:正忙着重构一段模型训练代码,突然测试团队发来消息说生产环境有个严重 Bug 需要立刻修复?或者你在 Jupyter Notebook 里调了一上午超参数&#xff0c…

作者头像 李华
网站建设 2026/7/16 16:28:15

c/c++ 常见输入输出函数

参考文章: C标准输入cin详解-CSDN博客 https://blog.csdn.net/bravedence/article/details/77282039 c里面的函数区分cin,cin.get,cin.getline,getline() cin:从标准输入中读取数据 cin会跳过空白字符,以空格,换行符,tab键作为分割…

作者头像 李华
网站建设 2026/7/16 7:21:28

WSL2中无法注册分发?直接使用云端PyTorch镜像

WSL2中无法注册分发?直接使用云端PyTorch镜像 在深度学习开发的日常中,你是否曾遇到这样的场景:满怀信心地打开 WSL2,准备跑一个 PyTorch 模型,结果 torch.cuda.is_available() 返回了令人绝望的 False?或者…

作者头像 李华
网站建设 2026/7/21 15:12:25

手把手教你构建多代理AI系统:MCP+A2A+LangGraph实战!

引言 在 AI Agent 开发领域,MCP(Model Context Protocol,模型上下文协议)用于标准化工具和资源的访问,让 LLM 能无缝调用外部数据源;A2A(Agent2Agent,代理间协议)则实现代…

作者头像 李华