package cn.itcast.access;

 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.text.SimpleDateFormat;
import java.util.Date; @WebServlet("/lastTime")
public class LastTimeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
案例需求:
1. 访问一个Servlet,如果是第一次访问,则提示:您好,欢迎您首次访问。
2. 如果不是第一次访问,则提示:欢迎回来,您上次访问时间为:显示时间字符串 1.用户访问Servlet资源
2.从记忆中获取访问时间
Servlet中getCookies()
判断是否为null
判断是否存在lastTime
存在,则获取用户时间,并将当前时间存入,显示上次访问 用户是第1次访问,显式首次访问 如果没有,说明第1次访问,则提示:您好,欢迎您首次访问。
如果有,则提示:欢迎回来,您上次访问时间为:显示时间字符串
*/ //编码 response.setContentType("text/html;charset=utf-8");
//从记忆中获取访问时间
Cookie[] cookies = request.getCookies();
if(cookies != null){
//判断是否存在lastTime
for (Cookie cookie : cookies)
if (cookie.getName().equals("lastTime")) {
String lastTimeMillisStr = cookie.getValue();
//存入当前时间
Cookie currentTime = new Cookie("lastTime", String.valueOf(System.currentTimeMillis()));
//保存1天
// currentTime.setMaxAge(60 * 60 * 24);
response.addCookie(currentTime); //用户已经访问,输出上次访问
Date lastTimeDate = new Date();
lastTimeDate.setTime(Long.parseLong(lastTimeMillisStr)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
String lastTimeStr = sdf.format(lastTimeDate);
response.getWriter().write("欢迎回来,您上次访问时间为:" + lastTimeStr); return;
}
} //首次访问
//存入当前时间
Cookie currentTime = new Cookie("lastTime", String.valueOf(System.currentTimeMillis()));
//保存1天
// currentTime.setMaxAge(60 * 60 * 24);
response.addCookie(currentTime); response.getWriter().write("您好,欢迎您首次访问。");
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

版本1 直接就写

 package cn.itcast.access;

 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.text.SimpleDateFormat;
import java.util.Date; @WebServlet("/lastTime2")
public class LastTimeServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
案例需求:
1. 访问一个Servlet,如果是第一次访问,则提示:您好,欢迎您首次访问。
2. 如果不是第一次访问,则提示:欢迎回来,您上次访问时间为:显示时间字符串 1.用户访问Servlet资源
2.从记忆中获取访问时间
Servlet中getCookies()
判断是否为null
判断是否存在lastTime
存在,则获取用户时间,并将当前时间存入,显示上次访问 用户是第1次访问,显式首次访问 如果没有,说明第1次访问,则提示:您好,欢迎您首次访问。
如果有,则提示:欢迎回来,您上次访问时间为:显示时间字符串
*/ //编码
response.setContentType("text/html;charset=utf-8"); //1.存入访问时间(毫秒值形式)
Cookie currentTime = new Cookie("lastTime", String.valueOf(System.currentTimeMillis()));
// currentTime.setMaxAge(60 * 60 * 24);
response.addCookie(currentTime); //2.从记忆中获取上次访问时间
Cookie lastTimeCookie = null;
Cookie[] cookies = request.getCookies();
if(cookies != null){
//判断是否存在lastTime
for (Cookie cookie : cookies)
if (cookie.getName().equals("lastTime")) {
lastTimeCookie = cookie;
break;
}
} //3.判断lastTimeCookie是否存在
if(lastTimeCookie != null){
//是,上次访问
Date lastTimeDate = new Date();
lastTimeDate.setTime(Long.parseLong(lastTimeCookie.getValue())); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
String lastTimeStr = sdf.format(lastTimeDate);
response.getWriter().write("欢迎回来,您上次访问时间为:" + lastTimeStr);
}else{
//否,首次访问
response.getWriter().write("您好,欢迎您首次访问。");
} } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

版本2 使用毫秒值

 package cn.itcast.access;

 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.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date; @WebServlet("/lastTime3")
public class LastTimeServlet3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
案例需求:
1. 访问一个Servlet,如果是第一次访问,则提示:您好,欢迎您首次访问。
2. 如果不是第一次访问,则提示:欢迎回来,您上次访问时间为:显示时间字符串 1.用户访问Servlet资源
2.从记忆中获取访问时间
Servlet中getCookies()
判断是否为null
判断是否存在lastTime
存在,则获取用户时间,并将当前时间存入,显示上次访问 用户是第1次访问,显式首次访问 如果没有,说明第1次访问,则提示:您好,欢迎您首次访问。
如果有,则提示:欢迎回来,您上次访问时间为:显示时间字符串
*/ //编码
response.setContentType("text/html;charset=utf-8"); //1.存入访问时间(日期字符串形式) //获取当前日期字符串
Date currentDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss"); //将当前日期字符串,存入cookie
Cookie currentTime = new Cookie("lastTime", URLEncoder.encode(sdf.format(currentDate), "UTF-8"));
// currentTime.setMaxAge(60 * 60 * 24);
response.addCookie(currentTime); //2.从记忆中获取上次访问时间
Cookie lastTimeCookie = null;
Cookie[] cookies = request.getCookies();
if(cookies != null){
//判断是否存在lastTime
for (Cookie cookie : cookies)
if (cookie.getName().equals("lastTime")) {
lastTimeCookie = cookie;
break;
}
} //3.判断lastTimeCookie是否存在
if(lastTimeCookie != null){
//是,上次访问
response.getWriter().write("欢迎回来,您上次访问时间为:" + URLDecoder.decode(lastTimeCookie.getValue(),"UTF-8"));
}else{
//否,首次访问
response.getWriter().write("您好,欢迎您首次访问。");
} } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

版本3 使用日期字符串

030_会话技术

1)什么是会话?
名词解释:会话描述的是通信双方之间一次交流过程。
特点:某个时刻对之前的会话内容都有记忆,可以基于这些记忆进行交流。

2)什么是web会话?
web会话:浏览器和服务器之间的交互过程,包含多次请求和响应。

3)会话技术:Cookie和Session
为会话存储数据的技术。Cookie把数据存储在客户端,Sessionba把数据存储在服务器。

核心:
即对数据跨请求的记忆和基于这些记忆的操作。
对数据跨请求存储和基于数据的操作。

JavaWeb案例:上次访问时间 Cookie技术的更多相关文章

  1. 【会话技术】Cookie技术 案例:访问时间

    创建时间:6.30 代码: package cookie; import java.io.IOException; import java.text.SimpleDateFormat; import ...

  2. Servlet—Cookie(显示用户上次访问时间、显示商品浏览历史)

    1 . 什么是会话? 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 1.1 会话过程中要解决的一些问题? 每个用户在使用浏览器 ...

  3. Servlet案例6:显示用户的上次访问时间

    这里是cookie的简单应用 告诉用户您的上次访问时间是:xxxx-xx-xx xx:xx:xx 思路: 第一次访问该网站时候,记录当前访问时间(new Date()) 把当前时间以cookie的形式 ...

  4. 使用Cookie实现显示用户上次访问时间

    一. 常用Cookie API介绍 1. 获取cookie request.getCookies();  // 返回Cookie[] 2. 创建cookie Cookie(String key, St ...

  5. Cookie实现--用户上次访问时间

    用户上次访问时间  

  6. cookie ? 利用cookie实现 显示上次访问时间?

    二. <%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.D ...

  7. IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie技术简介

    Cookie的英文原意是“点心”,它是在客户端访问Web服务器时,服务器在客户端硬盘上存放的信息,好像是服务器送给客户的“点心”.服务器可以根据Cookie来跟踪客户状态,这对于需要区别客户的场合(如 ...

  8. IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie技术原理

    Cookie使用HTTPHeader传递数据.Cookie机制定义了两种报头,Set-Cookie报头和Cookie报头.Set-Cookie报头包含于Web服务器的响应头(ResponseHeade ...

  9. javaWeb 使用cookie显示上次访问网站时间

    package de.bvb.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Date ...

随机推荐

  1. Android系统Gps分析(一)【转】

    本文转载自:http://blog.csdn.net/xnwyd/article/details/7198728 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   1 G ...

  2. hello vue不显示

    本身是做java后端开发的,但对任何技术都感兴趣.于是尝试了下最近国内比较火的vue框架. 在使用官网的例的时候子就卡壳了,写了个html,第一个Hello VUE!就是出不来,只显示{{messag ...

  3. 解决ini-parser解析ini文件中文乱码问题

    rickyah/ini-parser 是一个.net 平台解析ini文件的库,当ini文件中含有中文字符时会乱码. 解决:将文件通过Editplus 等文本编辑工具保存为 utf-8 + bom 格式 ...

  4. animate旋转动画练习,css3形变练习

    <!DOCTYPE html> <!-- saved from url=(0048)http://yinjiazeng.github.io/test/dial/index.html ...

  5. UML中的6大关系详细说明

    UML中的6大关系详细说明: 1.关联关系: 含义:类与类之间的连结,关联关系使一个类知道另外一个类的属性和方法:通常含有“知道”,“了解”的含义 体现:在C#中,关联关系是通过成员变量来实现的: 方 ...

  6. 使用js获取当前页面的url网址信息。

    1.设置或获取整个 URL 为字符串: window.location.href 2.设置或获取与 URL 关联的端口号码: window.location.port 3.设置或获取 URL 的协议部 ...

  7. AtCoder Beginner Contest 106 2018/08/18

    A - Garden Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement There is a ...

  8. visualVM远程监控JVM

    对于完全没用使用过visualVM的初学者 环境:Windows PC上使用visualVM监控远端JVM @@@@jstatd方式连接@@@@ 1.Windows安装jdk,$JAVA_HOME/b ...

  9. [转载]Windows网络编程系列教程之四:Select模型

    原文:http://www.51see.com/asp/bbs/public/bp_show.asp?t_id=200308131152297103 讲一下套接字模式和套接字I/O模型的区别.先说明一 ...

  10. Mesos以及Marathon安装总结

    安装了将近一周的环境了,终于把Mesos以及Marathon给安装上了,我指的离线安装. 策略1: 严格的按照官网的流程: http://mesos.apache.org/gettingstarted ...