加注时间
当我们第一次访问的时候和下次访问时不知道上次访问时间是什么时候,我们可通过此代码清晰的查看到上次访问时间,若第一次访问则输出“您是第一次访问”
Admin
上才艺
LastAccessServlet
package Cookies; 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; @WebServlet("/LastDateServlet") public class LastAccessServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); String lasttime=null; //获取cookie Cookie cookies[]=request.getCookies(); int i;//全局变量 for (i=0;i<cookies.length;i++){ String name = cookies[i].getName(); String value =cookies[i].getValue(); out.print(name+":"+value+"<br>"); } //如果循环结束,没有找到名叫lasttime的cookie,意味着第一次访问 if (i==cookies.length){//此时遍历完后的cookies的数量=i,也就是当查找完最后一个cookie时还是没有找到与其对应的cookie,则输出“您是第一次访问” out.print("您是第一次访问"); } //创建cookie,保存当前访问时间 //当前时间 Date date = new Date(); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String strdate=format.format (date) ; Cookie cookie=new Cookie("lasttime",strdate); //cookie有效时间设置为1小时 cookie.setMaxAge(60*60); response.addCookie(cookie); } }
改进后
package Cookies; 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; @WebServlet("/LastDateServlet") public class LastAccessServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); String lasttime=null; //获取cookie Cookie cookies[]=request.getCookies(); int i; for (i=0;i<cookies.length;i++){ // String name = cookies[i].getName(); // String value =cookies[i].getValue(); // out.print(name+":"+value+"<br>"); if(cookies[i].getName().equals("lasttime")){ out.print("上次访问时间为:"+cookies[i].getValue()); break; } } //如果循环结束,没有找到名叫lasttime的cookie,意味着第一次访问 if (i==cookies.length){ out.print("您是第一次访问"); } //创建cookie,保存当前访问时间 //当前时间 Date date = new Date(); SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String strdate=format.format (date) ; Cookie cookie=new Cookie("lasttime",strdate); //cookie有效时间设置为1小时 cookie.setMaxAge(60*60); response.addCookie(cookie); } }
文章有(1)条网友点评
有错误地方欢迎指出,互相学习。
不懂可以评论区或私信问博主哦~