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 ...
随机推荐
- wampserver发布详解
以下为wampserver发布php并绑定域名的操作 1 在“httpd.conf”文件中查找:Include conf/extra/httpd-vhosts.conf,去掉前面的注释# 2 打开ap ...
- CDH版本Hbase二级索引方案Solr key value index
概述 在Hbase中,表的RowKey 按照字典排序, Region按照RowKey设置split point进行shard,通过这种方式实现的全局.分布式索引. 成为了其成功的最大的砝码. 然而单一 ...
- hdu 2015校赛1002 Dual horsetail (思维题 )
Dual horsetail Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 算法(Algorithms)第4版 练习 1.3.27 1.3.28
代码实现: //1.3.27 /** * return the value of the maximum key in the list * * @param list the linked list ...
- SGU 495 Kids and Prizes:期望dp / 概率dp / 推公式
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495 题意: 有n个礼物盒,m个人. 最开始每个礼物盒中都有一个礼物. m个人依次随 ...
- js中使用对象变量的两种方式
function Person(){ this.a=function(){ window.alert("a"); } this.b=function(){ window.alert ...
- IntelliJ IDEA 中详细图解记录如何连接MySQL数据库
- Maven-将jar包安装到本地仓库
因为项目需要,使用的是sqlserver数据库,但是却找不到其对应的pom依赖,所以需要将本地jar包安装到本地仓库,定义pom依赖.以此为例,其他jar包均可参考该方式 cmd命令语句: mvn i ...
- PS滤镜— —波浪效果
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- python爬虫知识点总结(五)正则表达式
在线正则表达式匹配:http://tool.oschina.net/regex 正则表达式学习:https://c.runoob.com/front-end/854 一.什么是正则表达式? 常见匹配模 ...