`
java-_-ted
  • 浏览: 12145 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Cookie学习笔记《一》

阅读更多
一、什么是Cookie

     Cookie是一小段的文本信息,客户端请求服务器,如果服务器需要记录该用户状态,就使用response向客户端浏览器颁发一个Cookie.
客户端浏览器会把Cookie保存起来.当浏览器再请求该网站时,浏览器把请求的网址连同该Cookie一同提交给服务器.
     服务器检查该Cookie,以此来辨认用户状态.服务器还可以根据需要修改Cookie的内容.

     查看某个网站颁发的Cookie很简单:
     javascript:alert(document.cookie)


二、练习

Cookie.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" errorPage="login.jsp" %>
<%@ page import="javax.servlet.http.*"%> 

<%
	
	request.setCharacterEncoding("UTF-8"); //设置request编码
	String username = "";	//用户名
	int visitTimes = 0;		//访问次数
	
	Cookie[] cookies = request.getCookies();	//所有的Cookie
	for(int i=0; cookies!=null&&i<cookies.length;i++){		//遍历Cookie寻找账号与登录次数
		Cookie cookie = cookies[i];		//第i个Cookie
		if("username".equals(cookie.getName())){	//如果Cookie名为Username
			System.out.println(cookie.getValue());
			username = cookie.getValue();	//则记录该Cookie的内容
		}else if("visitTimes".equals(cookie.getName())){	//如果Cookie名为visitTimes
			visitTimes = Integer.parseInt(cookie.getValue());	//则记录Cookie的内容
		}
	}
	if(username == null || username.trim().equals("")){		//如果没有找到用户名,则转到登录界面 
		throw new Exception("您还没有登录,请先登录.");
	}
	//修改Cookie,更新用户的访问次数
	Cookie visitTimesCookie = new Cookie("visitTimes",Integer.toString(++visitTimes));
	response.addCookie(visitTimesCookie);
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>cookie.jsp</title>
</head>
<body>
	<div align="center" style="margin:10px;">
		<fieldset>
			<legend>登录信息</legend>
			<form action="login.jsp" method="post">
				<table>
					<tr>
						<td>您的账号:</td>
						<td><%= username%></td>
					</tr>
					<tr>
						<td>登陆次数:</td>
						<td><%= visitTimes%></td>
					</tr>
					<tr>
						<td></td>					
						<td>
						<input type="button" value="刷新" onclick="location='<%= request.getRequestURI() %>?ts=' + new Date().getTime();" class="button">
						</td>
					</tr>
				</table>
			</form>
		</fieldset>
	</div>
</body>
</html>

login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");
	
	if("POST".equals(request.getMethod())){
		
		Cookie usernameCookie = new Cookie("username",request.getParameter("username"));
		//Cookie visittimesCookie = new Cookie("visitTimes","0");
		
		response.addCookie(usernameCookie);
		//response.addCookie(visittimesCookie);
		
		//System.out.println(visittimesCookie.getValue());
		
		response.sendRedirect(request.getContextPath() + "/cookie.jsp");
		return;
	}
%>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login.jsp</title>
</head>
<body>
	<div align="center" style="margin:10px;">
		<fieldset>
			<legend>登录</legend>
			<form action="login.jsp" method="POST">
				<table>
					<tr>
						<td></td>
						<td>
							<span style="color:red; ">
								<%= exception.getMessage() %>
							</span>
						</td>					
					</tr>
					<tr>
						<td>账号:</td>
						<td>
							<input type="text" name="username" style="width:200px; ">
						</td>
					</tr>
					<tr>
						<td>密码:</td>
						<td>
							<input type="password" name="password" style="width:200px; ">
						</td>
					</tr>
					<tr>
						<td></td>
						<td>
							<input type="submit" value="登录" class="button">
						</td>
					</tr>
				</table>
			</form>
		</fieldset>
	</div>
</body>
</html>

三、Cookie的不可跨域名性

       Cookie具有不可跨域名性.根据Cookie规范,浏览器访问某个网站只会携带该网站的Cookie,而不会携带别的网站的Cookie.
       需要注意的是:虽然网站images.google.com和www.google.com同属于Google,但是域名不一样,二者同样不能互相操作彼此的Cookie.

四、Unicode编码:保存中文

        中文与英文字符不同,中文属于Unicode字符,在内存中占4个字符,而英文属于ASCII字符,内存中占2个字节.
        Cookie中使用Unicode字符时需要对Unicode字符进行编码,否则会乱码.
        编码可以使用java.net.URLEncoder类的 encode(String str,String encoding)方法.
        解码使用java.net.URLDecoder类的decode(String str,String encoding)方法.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>

<%
	//使用中文的Cookie. name与value都使用UTF-8编码
	Cookie cookie = new Cookie(URLEncoder.encode("姓名","UTF-8"),
							   URLEncoder.encode("刘京华","UTF-8"));
	response.addCookie(cookie);	//发送到客户端	
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cookie Encoding</title>
</head>
<body>
<%
	Cookie[] cookie1 = request.getCookies();
	if(cookie1 != null){
		for(Cookie cc : cookie1){
			String cookieName = URLDecoder.decode(cc.getName(),"UTF-8");
			String cookieValue = URLDecoder.decode(cc.getValue(),"UTF-8");			
			out.println(cookieName + "=" + cookieValue + "; <br/>");
		}
	}else{
		out.println("Cookie 已经写入客户端.请刷新页面.");
	}
%>
</body>
</html>
分享到:
评论

相关推荐

    学习cookie的笔记和总结

    学习,cookie,笔记和总结,cookie和session的区别

    PHP Cookie学习笔记

    主要为大家分享了PHP Cookie学习笔记,告诉大家什么是Cookie,Cookie的功能有哪些? 如何创建、读取、删除Cookie,感兴趣的小伙伴们可以参考一下

    js 逆向学习笔记之阿里系cookie.docx

    js 逆向学习笔记之阿里系cookie.docx

    js 逆向学习笔记之加速乐cookie加密.docx

    js 逆向学习笔记之加速乐cookie加密.docx

    Cookie、Session学习笔记

    NULL 博文链接:https://wjt276.iteye.com/blog/690332

    php学习笔记

    一.双引号与单引号 36 二.运算符 36 三.字符串拼接 36 四.类型运算符 36 五.switch语句 36 六.全局变量 37 七.预定义变量 37 函数 39 数组 40 一.用字符串做下标 40 二.使用小数作为key将,自动截断小数...

    shiro学习笔记.rar

    这是shiro的学习笔记,包括学习时候自己写的代码,主要内容就是使用springboot整合shiro,实现对用户的认证和授权,以及图片验证码的实现

    net学习笔记及其他代码应用

    6.如果在一个B/S结构的系统中需要传递变量值,但是又不能使用Session、Cookie、Application,您有几种方法进行处理? 答 : this.Server.Transfer 7.请编程遍历页面上所有TextBox控件并给它赋值为string.Empty? ...

    HttpClient学习笔记

    HttpClient学习笔记 整理 post get cookie

    安全测试学习笔记一(Cookie&Session)

    一,Session:含义:有始有终的一系列动作\消息1,隐含了“面向连接”和“保持状态”两种含义2,一种用来在客户端与服务器之间保持状态的解决方案3,也指这种解决方案的存储结构“把××保存在session里”二,http...

    Web安全学习笔记-Web-Sec Documentation

    Web安全学习知识库 Web安全学习笔记 Web-Sec Documentation

    php cookie使用方法学习笔记分享

    在php中cookie与其它程序中没什么两样,cookie都是用来存储信息到客户端,常用用于安全性要求不高的一些应用中,如用户登录记住密码之类的,下面我来给大家介绍一下php cookie学习笔记

    js学习笔记-恶意代码识别

    恶意js代码学习笔记,恶意代码识别.IE在处理Javascript执行的过程中存在漏洞,攻击者可以利用此漏洞在用户机器上执行恶意代码。因为当前正在执行的Javascript存在一个机会窗口,以旧页面的权限对新加载页面的内容执行...

    JavaScript学习笔记之Cookie对象

     Cookie是一种以文件的形式保存在客户端硬盘的Cookies文件夹中的用户数据信息(Cookie数据)。  Cookie文件由所访问的Web站点建立,以长久的保存客户端与Web站点间的会话数据,并且该Cookie数据只允许被所访问的Web...

    web前端开发AJAX学习笔记

    web前端开发AJAX学习笔记

    javascript学习笔记(七)利用javascript来创建和存储cookie

    首先看一下基础知识: 1、什么是cookie cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值 2、有关cookie...

    javascript学习笔记3

    javascript 中cookie与ajax 的基础知识总结

    JavaWeb入门学习笔记.chm

    2.此笔记含金量很高,无论你是初学者,自学者,还是有数年编程经验的大神,此套教程都将是你不可或缺的学习宝典。 3.笔记内容包括:HTML、CSS、JavaScript、XML、Java基础加强、Servlet、Request和Response、Cookie...

    MVC使用Memcache+Cookie解决分布式系统共享登录状态学习笔记6

    主要介绍了MVC使用Memcache+Cookie解决分布式系统共享登录状态学习笔记,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Global site tag (gtag.js) - Google Analytics