Java后端Cookie實(shí)現(xiàn)(時(shí)間戳)代碼實(shí)例
我們來(lái)簡(jiǎn)單實(shí)現(xiàn)一個(gè)cookie。
一、簡(jiǎn)單介紹
Cookie 是一些數(shù)據(jù), 存儲(chǔ)于你電腦上的文本文件中。
當(dāng) web 服務(wù)器向?yàn)g覽器發(fā)送 web 頁(yè)面時(shí),在連接關(guān)閉后,服務(wù)端不會(huì)記錄用戶的信息。
Cookie 的作用就是用于解決 '如何記錄客戶端的用戶信息':
當(dāng)用戶訪問(wèn) web 頁(yè)面時(shí),他的名字可以記錄在 cookie 中。 在用戶下一次訪問(wèn)該頁(yè)面時(shí),可以在 cookie 中讀取用戶訪問(wèn)記錄(博客園cookie界面)
二、簡(jiǎn)單實(shí)現(xiàn)
0.maven引入依賴
servlet和jsp的依賴
1.java代碼編寫
package com.lei;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[] cookies=req.getCookies(); if(cookies!=null) { out.write('您上一次訪問(wèn)時(shí)間為:'); for(int i=0;i< cookies.length;i++) {Cookie cookie=cookies[i];if(cookie.getName().equals('lastLoginTime')){ long lastLoginTime=Long.parseLong(cookie.getValue()); Date date=new Date(lastLoginTime); out.write(date.toString());} } } else{ out.write('first time come to this website!'); } Cookie cookie=new Cookie('lastLoginTime',System.currentTimeMillis()+''); resp.addCookie(cookie); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}
2.設(shè)置web-xml里面加入 servlet注冊(cè)和映射
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd' ><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>cookie</servlet-name> <servlet-class>com.lei.CookieDemo01</servlet-class></servlet><servlet-mapping> <servlet-name>cookie</servlet-name> <url-pattern>/cookie</url-pattern></servlet-mapping></web-app>
三、運(yùn)行效果
第一次cookie數(shù)組為空 不顯示登陸時(shí)間
按理說(shuō)應(yīng)該會(huì)顯示else里面的內(nèi)容first time come to this website!
但是顯示的是
只是因?yàn)橄旅娴牡诙垐D 是因?yàn)闉g覽器(我的是edge瀏覽器)默認(rèn)還有一個(gè)cookie
也就是說(shuō)我們第一次在執(zhí)行頁(yè)面(如果是從8080頁(yè)面輸入url跳轉(zhuǎn)的)時(shí) 有別的cookie存在
第二次才會(huì)顯示
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼2. 如何在PHP中讀寫文件3. java加載屬性配置properties文件的方法4. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析5. 什么是Python變量作用域6. 《Java程序員修煉之道》作者Ben Evans:保守的設(shè)計(jì)思想是Java的最大優(yōu)勢(shì)7. CSS3中Transition屬性詳解以及示例分享8. php redis setnx分布式鎖簡(jiǎn)單原理解析9. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼10. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
