首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

上回访问时间

2013-08-09 
上次访问时间package cn.lp.testimport java.io.IOExceptionimport java.io.PrintWriterimport java.ut

上次访问时间

package cn.lp.test;import java.io.IOException;import java.io.PrintWriter;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 VisitTime extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setHeader("Content-Type", "text/html;charset=UTF-8");//response.setContentType("text/html;charset=UTF-8");Cookie[] cookies=request.getCookies();Cookie cookie=findcookie(cookies,"lastvisit");if (cookie==null) {//response.getOutputStream().write("欢迎访问本网站".getBytes());response.getWriter().println("欢迎第一次登录本网站!");}else{long time=Long.parseLong(cookie.getValue());Date date = new Date(time);//response.getOutputStream().write(("您上次的访问时间是:"+date.toLocaleString()).getBytes());response.getWriter().println("您上次的访问时间是:"+date.toLocaleString());}Cookie c=new Cookie("lastvisit", System.currentTimeMillis()+"");c.setMaxAge(60 * 60 * 24);// long 毫秒      int  秒//设置有效路径c.setPath("/");response.addCookie(c);}private Cookie findcookie(Cookie[] cookies, String name) {if (cookies==null) {return null;}else{for (Cookie cookie : cookies) {if (cookie.getName().equals(name)) {return cookie;}}return null;}}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}}

热点排行