JavaWeb -- 会话, Cookie 和 Session
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("上次访问时间: ");
//get Cookie 获得上次写入的cookie
Cookie[] cookies = request.getCookies(); //从request获得cookie数组
for(int i=0; cookies!=null && i<cookies.length; i++)
{
if( cookies[i].getName().equals("lastAccessTime") )
{
long cookieValue = Long.parseLong(cookies[i].getValue());
Date date = new Date(cookieValue);
out.print(date.toLocaleString());
}
}
//write cookie 写入相应名字的cookie
Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+""); //新建访问时间cookie
cookie.setMaxAge(1*30*24*3600);
cookie.setPath("/WebTest3");
response.addCookie(cookie); //将cookie添加到response,将会返回到浏览器
}
<!-- 10 minutes 10分钟-->
<session-config>
<session-timeout>10</session-timeout>
</session-config>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(); //获得Session
String sessionID = session.getId();
Cookie cookie = new Cookie("JSESSIONID", sessionID);
cookie.setPath("/WebTest3");
cookie.setMaxAge(30*60); //设置保存时间,不然关闭浏览器即销毁相应的Cookie
response.addCookie(cookie);
session.setAttribute("name", "bug a TV");
out.print("bug");
//String url1 = response.encodeURL("/WebTest2/Demo4"); //如果浏览器的Cookie功能被关闭,则需要重写URL
//String url2 = response.encodeURL("/WebTest2/Demo5");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
if(session!=null)
{
String product = (String) session.getAttribute("name");
out.print("name = " + product);
}
}
实现自动登录功能:
<body>
Welcome: ${user.username} <br/>
<a href="/WebTest3/1.html">Login</a> <br/>
<a href="/WebTest3/Demo7">Logout</a> <br/> </body>
<form action="/WebTest3/Demo6" method="post">
用户名:<input type="text" name="username"/> <br/>
密码: <input type="password" name="passwd"/><br/>
登录: <input type="submit" name="submit"/> <br/>
自动登录:<input type="checkbox" name="autoLogin" value="true"/> <br/>
</form>
@WebServlet("/Demo6")
public class Demo6 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Demo6() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String passwd = request.getParameter("passwd");
//System.out.println("log: " + username + " " + passwd);
ArrayList<User> list = DB.getAll(); //从数据库获取用户信息
for(User user : list)
{
if(user.getUsername().equals(username) && user.getPasswd().equals(passwd)) //用户名密码校验
{
request.getSession().setAttribute("user", user);
response.sendRedirect("/WebTest3/welcome.jsp"); //获得Session
return;
}
}
out.write("Username or passwd Error");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}
}
class DB
{
private static ArrayList list;
static
{
list = new ArrayList<User>();
list.add(new User("kevin", "123456"));
list.add(new User("xiang", "123456"));
}
public DB() {
super();
// TODO Auto-generated constructor stub
}
public static ArrayList getAll()
{
return list;
}
}
public class User implements Serializable
{
private String username;
private String passwd; public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String username, String passwd) {
super();
this.username = username;
this.passwd = passwd;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
登录注销Servlet Demo7
public class Demo7 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Demo7() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
if( session==null)
{
response.sendRedirect("/WebTest3/welcome.jsp");
return;
}
session.removeAttribute("user"); //删除Session中的User 注销
response.sendRedirect("/WebTest3/welcome.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doGet(request, response);
}
}
JavaWeb -- 会话, Cookie 和 Session的更多相关文章
- 会话Cookie及session的关系(Cookie & Session)
会话Cookie及session的关系(Cookie & Session) 在通常的使用中,我们只知道session信息是存放在服务器端,而cookie是存放在客户端.但服务器如何使用sess ...
- JavaWeb之Cookie和Session的区别
Cookie和Session的区别 一.cookie机制和session机制的区别 ********************************************************** ...
- java基础学习:JavaWeb之Cookie和Session
一.会话概述 1.1.什么是会话? 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话其中不管浏览器发送多少请求,都视为一次会话,直到 ...
- JavaWeb 补充(Cookie&JSP&Session)
1. 会话技术 1. Cookie 2. Session 2. JSP:入门学习 会话技术 1. 会话:一次会话中包含多次请求和响应. * 一次会话:浏览器第一次给服务器资源发 ...
- 【JavaWeb】 Cookie和Session
Session和Cookie出现的原因: 由于Http是无状态的协议,会话之间没有任何关联,也就是上一次会话和下一次会话没有任何关联,因此出现了会话技术Cookie和Session 下面分别从Cook ...
- 会话Cookie与session的关系
在通常的使用中,我们只知道session信息是存放在服务器端,而cookie是存放在客户端.但服务器如何使用session和客户端之间进行通信,以及jsessionId是怎么回事,这并没有一个完整和正 ...
- 【Javaweb】Cookie和Session
会话技术 什么是会话 从浏览器访问服务器开始,到访问服务器结束,浏览器关闭为止的这段时间内容产生的多次请求和响应,合起来叫做浏览器和服务器之间的一次会话 会话管理作用 共享数据用的,并且是在不同请求间 ...
- IT兄弟连 JavaWeb教程 Cookie和Session应用结合使用
一般对于不要求安全的非敏感数据,建议存储在Cookie中! 对于敏感的数据,占用空间较小的,建议存储在Session中! 对于敏感的,较大的数据,存数据库!
- Django 组件-cookie 与 session
会话跟踪技术 1 什么是会话跟踪技术 我们需要先了解一下什么是会话!可以把会话理解为客户端与服务器之间的一次会晤,在一次会晤中可能会包含多次请求和响应.例如你给10086打个电话,你就是客户端,而10 ...
- Django框架07 /cookie和session
Django框架07 /cookie和session 目录 Django框架07 /cookie和session 1. django请求生命周期 2. cookie 3. session 4. 总结 ...
随机推荐
- QT项目性能调优小记
最近的项目用到了QT 5.5,项目在运行过程中出现了一段时间CPU占用率持续25%,并频繁断网的情况,遂决定对项目性能进行优化. 优化工具也是VS2010自带的性能分析工具,具体的使用方法参见:htt ...
- vs 2015 update 3各版本下载地址
微软在06月27日发布了Visual Studio 2015 Update 3 .在MSDN中微软也提供下载,而且MSDN的Visual Studio 2015 Update 3与官方免费下载的文件是 ...
- 让Category支持添加属性与成员变量【转载】
Category是Objective-C中常用的语法特性,通过它可以很方便的为已有的类来添加函数.但是Category不允许为已有的类添加新的属性或者成员变量. 一种常见的办法是通过runti ...
- find - exec 命令
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是command ...
- IIS7设置默认页
一般用ASP.NET创建的网站默认页都是Default.aspx,不需要设置. 但是如果有网站的起始页不是Default.aspx,就需要在IIS里设置了. IIS7的设置方法和IIS6的不一样: 在 ...
- eclipse配置jp.gr.java_conf.ussiy.app.propedit_5.3.3
配置PropertiesEditor插件 jp.gr.java_conf.ussiy.app.propedit_5.3.3 1.下载PropertiesEditor插件 http://pan.ba ...
- 使用badboy录制脚本 结合Jmeter一起测试。
1.badboy介绍 Badboy是一款不错的Web自动化测试工具,如果你将它用于非商业用途,或者用于商业用途安装Badboy 的机器数量不超过5台,你是不需要为它支付任何费用的.Badboy提供了将 ...
- UFLDL深度学习笔记 (四)用于分类的深度网络
UFLDL深度学习笔记 (四)用于分类的深度网络 1. 主要思路 本文要讨论的"UFLDL 建立分类用深度网络"基本原理基于前2节的softmax回归和 无监督特征学习,区别在于使 ...
- Yaml 的python 应用
1.安装yaml的python包 2.准备yaml的数据 3.yaml.load 解析yaml 3.生产yaml
- 洛谷P2661 信息传递==coedevs4511 信息传递 NOIP2015 day1 T2
P2661 信息传递 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知 ...