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 ...
随机推荐
- WCF异常处理
[读书笔记] 在进行分布式应用的异常处理时需要解决和考虑的基本要素: 异常的封装:服务端抛出的异常如何序列化传递到客户端 敏感信息的屏蔽:抛出的异常往往包含一些敏感的信息,直接将服务操作执行过程抛出的 ...
- System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration sect
An error has occurred creating the configuration section handler for userSettings/Microsoft.SqlServe ...
- Compiling: main.cpp /bin/sh: g++: not found
Kbuntu用codeblocks编写C程序的时候,编译报错如下: Compiling: main.cpp/bin/sh: g++: not found 解决方法: sudo apt-get inst ...
- matlab之flipud()函数
此函数实现矩阵的上下翻转.fliplw()实现左右旋转. 举例: a =[1 2;3 4;5 6] flipud(a)的结果: 5 6 3 4 1 2 fliplr(a)的结果: 2 1 4 3 6 ...
- tkinter之button
Button按钮,直接上代码: from tkinter import * def gs(): global read s=Label(read,text='昨夜西风凋敝树,堵上高楼,望尽天涯路!', ...
- HihoCoder1653 : 公平分队([Offer收割]编程练习赛39)(贪心)
描述 小Hi和小Ho在玩一个战争游戏.游戏中2N个战斗单位,其中第i个单位的战斗力是Ai. 现在小Hi和小Ho要各选N个单位组成队伍,当然他们都希望自己队伍的总战斗力越大越好. 为了使分队更加公平,经 ...
- 1053 Path of Equal Weight (30)(30 分)
Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. The weight ...
- poj3017 Cut the Sequence[平衡树+单调队列优化]
这里已经讲得很清楚了. 本質上是決策點與區間最大值有一定關係,於是用单调队列来维护决策集合(而不是常规的),然后在决策集合中选取最小值. 然后觉得这题方法还是很重要的.没写平衡树,用优先队列(堆)来维 ...
- 「NOIP2017」「LuoguP3952」 时间复杂度(模拟,栈
题目描述 小明正在学习一种新的编程语言 A++,刚学会循环语句的他激动地写了好多程序并 给出了他自己算出的时间复杂度,可他的编程老师实在不想一个一个检查小明的程序, 于是你的机会来啦!下面请你编写程序 ...
- 基于Html5的移动端APP开发框架
快速增长的APP应用软件市场,以及智能手机的普及,手机应用:Native(原生)APP快速占领了APP市场,成为了APP开发的主流,但其平台的不通用性,开发成本高,多版本开发等问题,一直困扰着专业AP ...