JavaWeb案例:上次访问时间 Cookie技术
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技术的更多相关文章
- 【会话技术】Cookie技术 案例:访问时间
创建时间:6.30 代码: package cookie; import java.io.IOException; import java.text.SimpleDateFormat; import ...
- Servlet—Cookie(显示用户上次访问时间、显示商品浏览历史)
1 . 什么是会话? 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 1.1 会话过程中要解决的一些问题? 每个用户在使用浏览器 ...
- Servlet案例6:显示用户的上次访问时间
这里是cookie的简单应用 告诉用户您的上次访问时间是:xxxx-xx-xx xx:xx:xx 思路: 第一次访问该网站时候,记录当前访问时间(new Date()) 把当前时间以cookie的形式 ...
- 使用Cookie实现显示用户上次访问时间
一. 常用Cookie API介绍 1. 获取cookie request.getCookies(); // 返回Cookie[] 2. 创建cookie Cookie(String key, St ...
- Cookie实现--用户上次访问时间
用户上次访问时间
- cookie ? 利用cookie实现 显示上次访问时间?
二. <%@page import="java.text.SimpleDateFormat"%> <%@page import="java.util.D ...
- IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie技术简介
Cookie的英文原意是“点心”,它是在客户端访问Web服务器时,服务器在客户端硬盘上存放的信息,好像是服务器送给客户的“点心”.服务器可以根据Cookie来跟踪客户状态,这对于需要区别客户的场合(如 ...
- IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie技术原理
Cookie使用HTTPHeader传递数据.Cookie机制定义了两种报头,Set-Cookie报头和Cookie报头.Set-Cookie报头包含于Web服务器的响应头(ResponseHeade ...
- javaWeb 使用cookie显示上次访问网站时间
package de.bvb.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.Date ...
随机推荐
- 【LeetCode】求众数
给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. class Solution(object): ...
- 通过阿里云域名动态解析 IP 地址
这两天在家里用树莓派折腾了一个家用服务器,主要用来做 mac 的 Time Machine ,还有就是当做下载机和 nas ,想着平时上班时间家里没人用网络,空着也是空着,就可以利用空闲带宽下个美剧啥 ...
- javascript macrotask & microtask
先看一个 实例 案例 console.log('script start'); setTimeout(function() { console.log('setTimeout'); }, 0); Pr ...
- CodeForces-329C(div1):Graph Reconstruction(随机&构造)
I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two in ...
- 每天一个linux命令(3):cd命令
版权声明 更新:2017-04-27博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下命令cd. 2 开 ...
- Spring 配置 详细
一.连接池概述 数据库连接池概述: 数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指 ...
- vue文件名规范
之前有看过一些命名规范,也看到说vue文件命名要么全是小写要么就是用小写 + '-':其实看到的时候有点不以意,因为本地能跑起项目:发布能正常访问也就OK了. 但是今天在做自动化部署的时候碰到一个问题 ...
- ambari快速安装hadoop
资源下载http://www.cnblogs.com/bfmq/p/6027202.html 大家都知道hadoop包含很多的组件,虽然很多都是下载后解压简单配置下就可以用的,但是还是耐不住我是一个懒 ...
- matlab形态学图像处理之strel函数
转自:http://blog.sina.com.cn/s/blog_b1cd5d330101pmwi.html strel--structuring element 运用各种形状和大小构造元素,基本语 ...
- 三台主机搭建LAMP(apache、mariadb、php)
实验环境:均是CentOS7 httpd:172.16.254.88 2.4.6 PHP:172.16.250.140 5.4.16 mariadb:172.16.250.94 5.5.52 第三 ...