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 ...
随机推荐
- EF异常捕捉
try{// 写数据库}catch (DbEntityValidationException dbEx){ }
- Linux bash shell环境变量以及语法规范
摘自: http://blog.csdn.net/abc_ii/article/details/8762739
- BZOJ 1651 [Usaco2006 Feb]Stall Reservations 专用牛棚:优先队列【线段最大重叠层数】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1651 题意: 给你n个线段[a,b],问你这些线段重叠最多的地方有几层. 题解: 先将线段 ...
- JSP 使用<%@include%>报Duplicate local variable path 错误 解决方法
错误提示:Multiple annotations found at this line:- Duplicate local variable path- Duplicate local variab ...
- MySQL学习_计算用户支付方式占比_20161104
计算用户支付方式占比 SELECT b.*#根据城市ID 年月排序 FROM ( SELECT a.* FROM ( #纵向合并 SELECT b1.ID,a1.城市,a1.收款方式,DATE_FOR ...
- XAML 编码规范 (思考)
1.尽量和Blend统一 2.兄弟元素之间需要空行 4.父子元素之间不需要空格 3.每行尽量单个属性 5.Grid的Row和Column定义不需要空行 6.Style里的Setter中不需要单行一个属 ...
- 杂项随记:gcc/objdump/section等
gcc -g 如果不打开-g或者-ggdb(GDB专用)调试开关,GCC编译时不会加入调试信息,因为这会增大生成代码的体积.GCC采用了分级调试,通过在-g选项后附加数字1.2或3来指定在代码中加入调 ...
- bash 实现菜单
#!/bin/bash a=`ls /data1/chenggang5/kepler/cases` cat <<EOF `j=0;for i in $a;do let j=$j+1;if ...
- BI 底座——数据仓库技术(Data Warehouse)
在开始喷这个主题之前,让我们先看看数据仓库的官方定义: 数据仓库(Data Warehouse)是一个面向主题的(Subject Oriented).集成的(Integrate).相对稳定的(Non- ...
- Oracle 11gr2的完全卸载
Oracle 11gr2的完全卸载方式与前些版本有了改变,运行D:\app\Administrator\product\11.2.0\dbhome_1\deinstall的deinstall.bat批 ...