cookie应用显示当前与之前时间
2016-11-28 16:56
295 查看
package com.neuedu.sevrlet; 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.text.SimpleDateFormat; import java.util.Date; /** * Created by ttc on 16-11-28. */ @WebServlet(name = "LastTimeServlet",urlPatterns = "/lasttime") public class LastTimeServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean bIsFirstVisit = true;//假定当前是第一次访问 String strLastTime = ""; Cookie[] cookies = request.getCookies(); if(cookies!=null) { for(Cookie c : cookies) { //看cookie中是否含有名称是lasttime的cookie,如果有,不是第一次访问(之前设置过这个cookie了) //如果循环一圈都没有发现名称是lasttime的cookie,说明是第一次访问 if("lasttime".equals(c.getName())) { bIsFirstVisit = false; strLastTime = c.getValue(); } } } response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //取得当前时间字符串 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String curDate = sdf.format(new Date()); PrintWriter pw = response.getWriter(); //将当前时间保存到cookie中 Cookie c = new Cookie("lasttime", curDate); response.addCookie(c); if(!bIsFirstVisit) { pw.print("当前时间为:"+ curDate); pw.print("上次的时间为:"+ strLastTime); } else { pw.print("当前时间为:"+ curDate); } } }
相关文章推荐
- cookie应用之显示当前时间,上次时间
- Cookie的应用—显示用户上次访问时间
- 使用Cookie来显示当前时间
- WINCE应用层设计经验总结-关于当前时间显示和当前时间获取
- AJAX应用案例--不刷新整个WEB页面显示服务器响应的当前时间
- [置顶] 【Javaweb】Cookie应用——显示用户上次访问网站的时间
- Cookie应用:显示上次访问页面时间
- 显示当前日期时间代码
- 在您的页面中显示当前时间的农历时间
- JSP下取当前时间并显示的代码,jsp年月日显示时间
- js动态显示当前日期,时间和星期代码
- 如何在TextBox控件中显示系统当前时间?包括:年、月、日、时、分
- 显示当前时间
- JavaScript动态显示当前时间和倒计时的设计(附全码)_AX
- 用js实现页面显示当前日期和时间的代码
- 用js实现页面显示当前日期和时间的代码
- javascript 应用技巧 显示当前日期
- 完整显示当前日期和时间的JS代码
- 利用BIOS中断或DOS中断实现一个显示当前时间的程序
- JSP下取当前时间并显示的代码,jsp年月日显示时间