4、利用Cookie显示用户上次登录的时间
index.jsp:
<a href="LoginServlet">利用Cookie显示用户上次登录的时间</a>• 1
web.xml:
<servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>cn.hncu.servlets.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping>
LoginServlet.java:
package cn.hncu.servlets; import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>演示利用Cookie显示用户上次登录的时间</TITLE></HEAD>"); out.println(" <BODY>"); //读取客户端的cookie Cookie cs[] = request.getCookies(); boolean boo = false; if(cs!=null){ for(Cookie c:cs){ //遍历 if("loginTime".equals(c.getName())){ String val =c.getValue(); long dt = Long.parseLong(val); Date d = new Date(dt); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); out.print("您上次登录时间是:"+sdf.format(d)); boo=true; break; } } } if(boo==false){//表示之前1年没有访问记录!因为下面我们保存的过期时间是一年 out.print("您最近一年是第一次访问。。。"); } //无论是新旧用户,都会以最近的时间俩创建一个Cookie,写到客户端。原来有了的,就是更新时间 Date d = new Date(); Cookie c = new Cookie("loginTime", ""+d.getTime() ); c.setPath(request.getContextPath()); c.setMaxAge(60*60*24*30*12); response.addCookie(c); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
演示结果:
第一次访问;
再次访问:
5、利用Cookie技术显示用户最近浏览的若干个图片
index.jsp:
<a href="jsps/show.jsp">看美女--利用Cookie技术显示用户最近浏览的若干个图片</a>• 1
web.xml:
<servlet> <servlet-name>ShowServlet</servlet-name> <servlet-class>cn.hncu.servlets.ShowServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/showImg</url-pattern> </servlet-mapping>
show.jsp:
<%@page import="java.io.File"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <style type="text/css"> .span{ border:0px solid #000; width:100px; height:100px; overflow:hidden; } .span img{ max-width:100px; _width:expression(this.width > 100 ? "100px" : this.width); } .spans{ border:0px solid #000; width:50px; height:50px; overflow:hidden; } .spans img{ max-width:50px; _width:expression(this.width > 50 ? "50px" : this.width); } </style> </head> <body> <h1>看美女--利用Cookie技术显示用户最近浏览的若干个图片</h1> <a href="/myCookieWeb/jsps/show.jsp">看美女--利用Cookie技术显示用户最近浏览的若干个图片</a> <h3>最近浏览的图片:</h3> <!-- 添加最近3次浏览的图片 --> <% String str =null; Cookie cs[] = request.getCookies(); if(cs!=null){ for(Cookie c:cs){ if("images".equals(c.getName())){ str=c.getValue();// ***.jpg break; } } } if(str!=null){ String strs[] = str.split(","); for(String s:strs){ %> <span class="spans"> <img src="<%=request.getContextPath()%>/imgs/<%=s%>" /> </span> <% } } %> <br/><hr/><br/> <% //利用file遍历所有的图片,显示出来。 String path = getServletContext().getRealPath("/imgs"); //System.out.printf(path);//D:\apache-tomcat-7.0.30\webapps\myCookieWeb\jsps File file = new java.io.File(path); File[] files = file.listFiles(); if(files!=null){ %> <% for(File f:files){ String imgName = f.getName(); %> <span class="span"> <a href="<%=request.getContextPath() %>/showImg?img=<%=imgName %>"> <img src="<%=request.getContextPath()%>/imgs/<%=imgName%>" /> </a> </span> <% } %> <% } %> </body> </html>
ShowServlet.java:
package cn.hncu.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ShowServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); String img = request.getParameter("img"); String imgStr = "<img src='"+request.getContextPath()+"/imgs/"+img+"'/>"; out.print(imgStr); //用Cookie记录用户访问过的图片信息 Cookie cs[] = request.getCookies(); boolean boo = false; if(cs!=null){ for(Cookie c:cs){ if("images".equals(c.getName())){//已经有了images这个cookie String imgs = c.getValue(); String imgStrs[] = imgs.split(","); boolean booStr = false; //防范点击重复的图片 for(int i=0;i<imgStrs.length;i++){ if(imgStrs[i].equals(img)){ if(i==1&&imgStrs.length==2){ imgs=imgStrs[i]+","+imgStrs[0]; }else if(i==2&&imgStrs.length==3){ imgs=imgStrs[i]+","+imgStrs[0]+","+imgStrs[1]; }else if(i==2&&imgStrs.length==3){ imgs=imgStrs[i]+","+imgStrs[0]+","+imgStrs[1]; } booStr=true; break; } } if(!booStr){ imgs = img+","+imgs;//采用如下方式会麻烦一点:imgs+","+img if(imgs.split(",").length>3){//如果访问的图片超过3次了 imgs = imgs.substring(0, imgs.lastIndexOf(","));//左包含,右不包含 } /*//如果这样写了,最好把上面的防范重复的图片,那个添加顺序倒过来 //imgs+","+img 方式: imgs = imgs+","+img; if(imgs.split(",").length>3){//如果访问的图片超过3次了 imgs = imgs.substring(imgs.indexOf(",")+1, imgs.length()); } */ } c.setValue(imgs);//更新 c.setMaxAge(60*60*24*30); c.setPath("/");//相当于把访问权限完全放开,即所有的项目都能访问 response.addCookie(c); boo=true; break; } } } if(boo==false){//表示首次访问,即浏览器中没有图片浏览的cookie Cookie c = new Cookie("images", img); c.setMaxAge(60*60*24*30); c.setPath("/"); response.addCookie(c); } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
演示结果:
6、测试火狐浏览器到底支持多少个Cookie和一个Cookie最大为多大
index.jsp:
<a href="servlet/HowManyCookieServlet">测试火狐浏览器到底支持多少个Cookie和一个Cookie最大为多大 </a><br/>
web.xml:
<servlet> <servlet-name>HowManyCookie</servlet-name> <servlet-class>cn.hncu.servlets.HowManyCookie</servlet-class> </servlet> <servlet-mapping> <servlet-name>HowManyCookie</servlet-name> <url-pattern>/servlet/HowManyCookieServlet</url-pattern> </servlet-mapping>
HowManyCookie.java:
package cn.hncu.servlets; import java.io.IOException; import java.io.PrintWriter; import java.net.URLDecoder; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HowManyCookie extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); /* //测试个数--火狐47.0.1 最大支持110个 for(int i=1;i<=110;i++){ Cookie c = new Cookie("textNum"+i, ""+i); c.setMaxAge(60*15); c.setPath("/"); response.addCookie(c); } */ //测试大小 ---4092字节为最大支持的单个cookie存储 String s =""; for(int i=0;i<4092;i++){ s+="1"; } Cookie c = new Cookie("test", s); c.setMaxAge(60*15); c.setPath("/"); response.addCookie(c); Cookie cs[] = request.getCookies();//读取cookie if(cs!=null){//防范一下 for(Cookie cc:cs){ String key = cc.getName(); String val = cc.getValue(); out.print(key+"="+val+" "); } } } }