4-cookie 简介
1.eclipse中tomcate镜像位置:
D:\javaTools\eclipse\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tem1
2.cookie对象
作用:是能将客户的信息保存在客户端,是以文本的形式保存,生命周期由程序决定,可以很长
使用步骤:
a.创建一个cookie对象 :Cookie cook = new Cookie("key","value");
b.可以设置cookie的有效期:cook.setMaxAge();
c.将该cook对象添加到response对象中,发送(转发和重定向都可以)到前台页面
d.页面发送请求到服务端会带上cookie对象,服务端可以获取里面的信息来做相应的处理
课堂练习:
1.用cookie保存用户名,在成功页面获取显示
2.登录时课选择保存密码状态,下次访问该页面直接登录成功
login.jsp
<%@page import="com.pojo.UserInfo"%>
<%@page import="com.service.UserInfoService"%>
<%@page import="com.service.IUserInfoService"%>
<%@page import="java.net.CookieStore"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
Cookie[] cookies = request.getCookies();
IUserInfoService userSer = new UserInfoService();
if(cookies!=null)
for(int i=0;i<cookies.length;i++){
if("username".equals(cookies[i].getName())){
if(cookies[i].getValue()!=null&&!"".equals(cookies[i].getValue())){
//根据用户名查询用户信息
UserInfo user = userSer.getUserByName(cookies[i].getValue());
session.setAttribute("user", user);
response.sendRedirect("success.jsp");
}
}
}
%>
<body>
<form action="cookie/control.jsp" method="post">
用户名:<input name="username"><br>
密码:<input type="password" name="password"><br>
是否记住<input type="checkbox" name="isremder" value="1"><input type="submit" value="登录">
</form>
</body>
</html>
control.jsp
<%@page import="com.pojo.UserInfo"%>
<%@page import="com.service.UserInfoService"%>
<%@page import="com.service.IUserInfoService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String isrember = request.getParameter("isremder");
IUserInfoService userSer = new UserInfoService();
UserInfo user = userSer.login(username, password);
if(user!=null){
session.setAttribute("user",user);
//登录成功,将用户名和密码保存到cookie中
if("1".equals(isrember)){//记住用户名
Cookie cookie = new Cookie("username",user.getUser_name());
cookie.setMaxAge(1000*3600*24*7);//设置cookie的有效期
response.addCookie(cookie);//将cookie添加到响应中发送给客户端
}
response.sendRedirect("success.jsp");
}else{
response.sendRedirect("login.jsp?error=true");
}
%>
</body>
</html>
success.jsp
<%@page import="com.pojo.UserInfo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//获取cookie中的值
// Cookie[] cookies = request.getCookies();
// String username = "";
// String password = "";
// if(cookies!=null)
// for(int i=0;i<cookies.length;i++){
// if("username".equals(cookies[i].getName())){
// username = cookies[i].getValue();
// }
// if("password".equals(cookies[i].getName())){
// password = cookies[i].getValue();
// }
// }
//session中获取用户信息
UserInfo user = (UserInfo)session.getAttribute("user"); %>
<h1>欢迎<%=user.getUser_name() %>登录 <a href="">退出登录</a> </h1>
</body>
</html>
exit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
//获取username的cookie
Cookie[] cookies = request.getCookies();
if(cookies!=null)
for(int i=0;i<cookies.length;i++){
if("username".equals(cookies[i].getName())){
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);//覆盖掉客户端的有效cookie
}
}
response.sendRedirect("login.jsp");
%>
<body> </body>
</html>
4-cookie 简介的更多相关文章
- Session & Cookie 简介
(一)简介 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通 ...
- cookie简介
上例子 1.首先要用php创建cookie发送给客户端,利用setcookie()方法即可 <?php /* * * @Authors peng--jun * @Email 1098325951 ...
- session cookie简介
会话机制:Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身 ...
- Session&Cookie 简介及使用
Cookie cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 或其它语言来创建和取回 cookie ...
- cookie技术简介
Cookie简介 众所周知,HTTP协议是一个无状态的协议.一个Web应用由很多个Web页面组成,每个页面都有唯一的URL来定义.用户在浏览器的地址栏输入页面的URL,浏览器就会向Web Server ...
- C#中Cookie的概述及应用
1.Cookie简介 Cookie 提供了一种在 Web 应用程序中存储用户特定信息的方法.例如,当用户访问您的站点时,您可以使用 Cookie 存储用户首选项或其他信息.当该用户再次访问您的网站时, ...
- Asp.net操作cookie大全
实例代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...
- [转载]JavaEE学习篇之——Session&&Cookie
原文链接: http://blog.csdn.net/jiangwei0910410003/article/details/23337043 今天继续来看看JavaWeb的相关知识,这篇文章主要来讲一 ...
- session与cookie的区别---
session和cookie的最大区别在于session是保存在服务端的内存中, 而cookie保存与浏览器或客户端文件里面: session是基于访问的进程,记录了一个访问的开始到结束,当浏览器或进 ...
- Servlet和JSP读书笔记(三)之Cookie
一. 浏览器和服务器之间通信的简单介绍引出Cookie和Session(只是简单的简介,不包含协议方面的知识) 1.当我们在浏览器中输入一个地址后,回车后就可以看到浏览器给我们展示的漂亮页面.在这个过 ...
随机推荐
- 2018年,JavaScript都经历了什么?
摘要: 对JSer来说,这是很有意思的1年. 本文灵感来自JavaScript Weekly周报,欢迎大家订阅. The State of JavaScript 2018 The State of J ...
- 为啥JQuery被淘汰了?
摘要: 技术进步永不止步. 原文:jQuery的没落和技术发展的一般规律 作者:凌霄光 Fundebug经授权转载,版权归原作者所有. jQuery的成就 jQuery是一个伟大的库, 它解决了dom ...
- 为什么用bower 安装bootstrap而不用npm来安装?
NPM(node package manager),通常称为node包管理器.顾名思义,它的主要功能就是管理node包,包括:安装.卸载.更新.查看.搜索.发布等. npm的背后,是基于couchdb ...
- 包含min函数的栈 ,二叉树的镜像
包含min函数的栈 问题 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 代码 # -*- coding:utf-8 -*- class Sol ...
- nginx中有关 root 和 alias的主要区别
举个例子给伙伴们区别就明显看出来了,例子如下: location /img/ { alias /var/www/image/; }注意:如果按照上述配置的话,则访问/img/目录里面的文件时,ning ...
- SpringBoot-学习笔记
启动方式 运行main方法 @SpringBootApplication public class BootApplication { public static void main(String[] ...
- (后台)SQL Server 数据库定时自动备份(转)
转自博客园: SQL Server 数据库定时自动备份[转] 在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以 ...
- C# 如何使用 Elasticsearch (ES)
Elasticsearch简介 Elasticsearch (ES)是一个基于Apache Lucene(TM)的开源搜索引擎,无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好 ...
- CsQuery中文编码乱码问题
一.问题描述 InnerHTML 中文显示为Модель 二.解决方法 在初始化CQ对象前,先设置执行以下语句: CsQuery.Config.HtmlEncoder = CsQuery.HtmlEn ...
- redis搭建主从(1主2从)
一,先附上配置文件 1,master(6379.conf)上面的配置文件 daemonize yes pidfile /usr/local/redis/logs/redis_6379.pid port ...