使用Cookie来显示当前时间
来源:互联网 发布:java求圆锥圆柱体积 编辑:程序博客网 时间:2024/06/07 01:10
package com.ask.cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//显示用户上次访问的时间;
public class CookieTime extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置字体格式
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//存入当前时间;
Cookie cookie=new Cookie("lastAccessTime", System.currentTimeMillis()+"");
//将cookie交给response对象响应到浏览器中
resp.addCookie(cookie);
PrintWriter out=resp.getWriter();
String time="";
Cookie[] cookies=req.getCookies();
for (Cookie cookie2 : cookies) {
if("lastAccessTime".equals(cookie2.getName())){
time=cookie2.getValue();
long times=Long.parseLong(time);
Date date=new Date(times);
out.print("你当前的访问时间是: "+date);
}
}
}
}
2.web.xml配置文件如下:
<!-- 获得时间 -->
<servlet>
<servlet-name>CookieTime</servlet-name>
<servlet-class>com.ask.cookie.CookieTime</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookieTime</servlet-name>
<url-pattern>/CookieTime</url-pattern>
</servlet-mapping>
- 使用Cookie来显示当前时间
- cookie应用显示当前与之前时间
- 用js来显示当前时间
- cookie应用之显示当前时间,上次时间
- 使用DataPicker及TimePicker显示当前时间
- 使用js显示当前系统时间
- Struts2--使用JSP显示系统当前时间
- 使用JavaScript显示当前时间-UTC时间-PDT时间
- java 中 使用Calendar类来获取当前时间
- 使用Calendar来获取当前日期和时间
- 当前时间显示
- 显示当前时间
- JavaScript显示当前时间
- 显示当前系统时间
- 完整显示当前时间
- 显示当前时间
- 网页显示当前时间
- 显示当前时间
- 线性表--顺序表及链表
- iOS开发 - 屏幕适配之纯代码
- mysql插入多条语句
- UBIFS的编译
- 二分法查找
- 使用Cookie来显示当前时间
- API函数之SetParent
- 从入门到放弃C语言-瞎倒腾(2)
- C++中虚析构函数的作用
- javascript基本数据类型
- MYSQL root密码修改
- hdu1712 分组背包 *
- CZMQ使用举例
- 手机站限制PC端访问