JavaWeb会话管理之用户上次访问时间
来源:互联网 发布:淘宝 充话费 退款 编辑:程序博客网 时间:2024/06/02 03:34
package iz.cast;import java.io.IOException;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;/** * 案例-用户上次访问时间 * @author APPle * */public class Firstcookie extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");//获取当前时间SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String curTime = format.format(new Date());//取得cookieCookie[] cookies = request.getCookies();String lastTime = null;if(cookies!=null){for (Cookie cookie : cookies) {if(cookie.getName().equals("lastTime")){//有lastTime的cookie,已经是第n次访问lastTime = cookie.getValue();//上次访问的时间//第n次访问//1.把上次显示时间显示到浏览器response.getWriter().write("欢迎回来,你上次访问的时间为:"+lastTime+",当前时间为:"+curTime);//2.更新cookiecookie.setValue(curTime);cookie.setMaxAge(1*30*24*60*60);//3.把更新后的cookie发送到浏览器response.addCookie(cookie);break;}}}/** * 第一次访问(没有cookie 或 有cookie,但没有名为lastTime的cookie) */if(cookies==null || lastTime==null){//1.显示当前时间到浏览器response.getWriter().write("你是首次访问本网站,当前时间为:"+curTime);//2.创建Cookie对象Cookie cookie = new Cookie("lastTime",curTime);cookie.setMaxAge(1*30*24*60*60);//保存一个月//3.把cookie发送到浏览器保存response.addCookie(cookie);}}}
阅读全文
0 0
- JavaWeb会话管理之用户上次访问时间
- 会话技术-显示用户的上次访问时间(十七)
- 会话管理之Cookie案例一:获取网站上次访问时间
- Cookie应用之用户上次访问时间、用户浏览记录
- 案例-用户上次访问时间
- javaWeb-02-cookie案例-显示用户上次访问网站的时间
- 【Javaweb】Cookie应用——显示用户上次访问网站的时间
- 显示用户上次的访问时间
- 利用cookie实现用户上次访问时间
- servlet显示上次用户访问的时间
- cookie--显示用户上次访问时间
- cookie案例,显示用户上次访问时间
- cookie的介绍,实现显示用户上次访问时间
- Cookie的应用—显示用户上次访问时间
- 利用Cookie显示用户上次访问网站的时间
- cookie案例-显示用户上次访问网站的时间
- cookie案例-显示用户上次访问网站的时间
- 记录用户上次访问时间(cookie和session)
- 从文件中读取数据
- 『ORACLE』 DBLINK(11g)
- 数组指针和指针数组的区别
- 新闻客户端——SmartImageView——AsyncHttpClient
- Intent和Intent过滤器
- JavaWeb会话管理之用户上次访问时间
- 搜索算法总结
- POJ
- 离散题目10
- 新人作者快速入门的最好方法都在这了
- JQuery实现导航菜单栏随滚动条上下滚动
- B
- java基础--for循环嵌套打印输出直角三角形
- Django_分页设计和Ueditor配置、图文发布