用Cookie实现显示上一次访问的时间和删除cookie

2018-01-10网页设计
174

public class Cookie1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         response.setContentType("text/html;charset=utf-8");
         PrintWriter out = response.getWriter();
         //得到客户端的所有的Cookie 
         Cookie[] cookies=request.getCookies();
         //从Cookie中找出存储最后登录时间的Cookie
         //Cookie是一个键值对,并且键和值都是String的
         if(cookies!=null){
             for(int i=0;i<cookies.length;i++){
                 String name = cookies[i].getName();//得到Cookie的name
                 if("lastAccessTime".equals(name))
                 {
                     String time = cookies[i].getValue();//得到最后登录时间,时间是字符串类型的
                     long  t = Long.parseLong(time);
                     out.println("上次登录时间为:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(t)));
                 }
             }
         }
         //在页面上写一个删除的链接连接到DelCookie上删除cookie
         out.println("<a href='"+request.getContextPath()+"/servlet/delcookie'>删除Cookie</a>");

         //把这次的登录时间以Cookie的形式发送给客户端
         //创建Cookie
         Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+"");
         cookie.setMaxAge(60*15);//Cookie 被存储到磁盘了
         cookie.setPath(request.getContextPath());// /day35_cookie 只要是路径以 /day35_cookie开头的都可以接收到Cookie
         //cookie.setPath("/");//  /day35_cookie
         //把cookie发送给客户端
         response.addCookie(cookie);
         /*
          * Cookie默认存储在浏览器的内存,浏览器一旦关闭,Cookie全都没了
          * setMaxAge(time)设置Cookie的最大存活时间,默认单位是秒
          * setMaxAge(-1);默认是-1,浏览器一旦关闭,Cookie全都没了
          * setMaxAge(0):浏览器不关闭,Cookie也没了
          * setMaxAge(正数)
          * 
          * 满足什么条件客户端会把Cookie发给服务器端?
          * 被访问资源的路径前边要匹配  
          * http://localhost:8080/day35_cookie/servlet/cookie1
          * http://localhost:8080/day35_cookie/servlet/cookie2
          */
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}


删除cookie:

public class DelCookie extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        //删除Cookie
        Cookie cook = new Cookie("lastAccessTime", "");
        cook.setMaxAge(0);
        //设置path,否则可能会错删
        cook.setPath(request.getContextPath());
        response.addCookie(cook);       
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         doGet(request,response);
    }

}

The End

相关推荐

移动端,手机端 响应式布局头部代码
将如下代码加到你网页的头部之间: meta name=viewport content=width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no meta name=apple-mobile-web-app-capable content=yes / meta name=apple-mobile-web-app-statu...
2021-06-10 网页设计
262

javascript实现禁止鼠标右键的功能
遇到网页上有精美图片或者精彩文字想保存时,通常大家都是选中目标后按鼠标右键,在弹出菜单中选择图片另存为或复制来达到我们的目的。但是,目前有许多网页都屏蔽了鼠标右键,那么用js如何实现禁止鼠标右键的功能呢? 1、与禁止鼠标右键相关的JS说明 script...
2021-04-27 网页设计
164

js实现点击按钮复制文本功能
最近遇到一个需求,需要点击按钮,复制 p 标签中的文本到剪切板 之前做过复制输入框的内容,原以为差不多,结果发现根本行不通 尝试了各种办法,最后使了个障眼法,实现了下面的效果 一、原理分析 浏览器提供了 copy 命令 ,可以复制选中的内容 document.exe...
2020-03-30 网页设计
233

如何去掉html中input的边框
这里不能用css样式选择器选择input组件,设置 border:none;或border: 0px;outline:none;cursor: pointer; 亲测没有用处,只有写这种格式时生效,如下: input type=text style=border:none;...
2020-03-30 网页设计
214

js 判断字符串中是否包含某个字符串
String对象的方法 方法一: indexOf() (推荐) var str = 123;console.log(str.indexOf(3) != -1 ); // trueindexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回 -1。 方法二: search() var str = 1...
2019-05-30 网页设计
262

video标签设置autoplay在手机浏览器无法自动播放视频的解决方案
在video标签上加个autoplay属性,在PC端浏览器里面运行流畅,但是在手机浏览器里面打开无法播放。原来现在的手机浏览器是不允许网页中视频自动播放的,只有与用户进行了一次交互动作,才可以播放视频。(音频同理) 方案一: 在页面上加一个弹框,用户点击了...
2019-05-27 网页设计
645