建站系统排行榜,做证明图片的网站,动漫网站怎么做,企业网站后台管理模板本篇是以JSP为背景介绍#xff0c;但是在web开发中也是相同的原理。 什么是cookie 由于http是一种无状态的协议#xff0c;因此服务器收到请求后#xff0c;只会当做一次新的请求。即便你重复发送了1000次同样的请求#xff0c;这1000次都属于独立的请求。 这样显然效率很低… 本篇是以JSP为背景介绍但是在web开发中也是相同的原理。 什么是cookie 由于http是一种无状态的协议因此服务器收到请求后只会当做一次新的请求。即便你重复发送了1000次同样的请求这1000次都属于独立的请求。 这样显然效率很低如果要登录某个网站后期的操作都与用户身份有关难道还得没操作一个页面都得登录一次 于是cookie和session就诞生了。 cookie和session都是用于帮助http进行状态管理的一种手段。 cookie与session的区别 cookie与session的区别可以通过下面几点区分 1 保存位置cookie保存在客户端浏览器中session保存在服务器端。 2 生命周期cookie由用户指定或者使用默认的过期时间在这段期限内cookie都保存在客户端本地session属于一次会话如果会话关闭浏览器关闭服务器启动都会导致session的清除。 3 数据类型cookie其实就是一堆字符串session是某种Object对象。 4 安全性cookie一般只保存一些用户的行为习惯等等像用户名密码肯定都需要经过加密的即使泄露了也无关紧要session则保存用户相关的重要内容。 cookie的使用过程 如果要保存cookie 首先需要创建一个Cookie对象然后通过把它添加到response对象中返回给客户端即可。 Cookie对象中的数据就自动保存在客户端了。 如果要使用cookie 可以通过request对象直接查询cookie信息并且比对是否含有自己使用的数据。 Cookie中常用的方法 1 创建Cookie对象 Cookie usernameCookie new Cookie(username,username); 2 设置过期时间以秒为单位 usernameCookie.setMaxAge(864000); 3 保存cookie response.addCookie(usernameCookie); 4 获取cookie数据 Cookie[] cookies request.getCookies(); 5 提取关键数据 Cookie[] cookies request.getCookies();
if(cookies!null cookies.length0){for(Cookie c:cookies){if(c.getName().equals(username)){response.addCookie(c);}}
} JSP中cookie使用样例 业务场景 1 login.jsp登录用户名密码可以设置是否记录cookie如果之前登陆过则自动填写cookie中的信息。 2 跳转到doLogin.jsp界面进行cookie的保存于清除。如果前一页设置保存则保存cookie信息如果前一页设置不保存则清除信息。 3 通过URL跳转到users.jsp页面可以提取cookie中的相关信息。 login.jsp % page languagejava contentTypetext/html; charsetutf-8importjava.net.*pageEncodingutf-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetutf-8
title用户登录/title
/head
bodyh1用户登录/h1hr%request.setCharacterEncoding(utf-8);String username ;String password ;Cookie[] cookies request.getCookies();if(cookies!null cookies.length0){for(Cookie c:cookies){if(c.getName().equals(username)){username URLDecoder.decode(c.getValue(),utf-8);}if(c.getName().equals(password)){password URLDecoder.decode(c.getValue(),utf-8);}}}%form nameloginForm actiondoLogin.jsp methodposttabletrtdusername/tdtdinput typetext nameusername value%username%/input/td/trtrtdpassword/tdtdinput typepassword namepassword value%password%/input/td/trtrtdinput typecheckbox nameisUseCookie checkedtrue/记住登录状态/td/trtrtd colspan2 aligncenterinput typesubmit valuesubmit//td/tr/table/form
/body
/html View Code doLogin.jsp % page languagejava contentTypetext/html; charsetutf-8importjava.net.*pageEncodingutf-8%!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetutf-8
title用户登录/title
/head
bodyh1javaBeans/h1hr%//保证request以及response的编码 request.setCharacterEncoding(utf-8);response.setContentType(text/html;charsetutf-8);String[] isUseCookies request.getParameterValues(isUseCookie);if(isUseCookies!null isUseCookies.length0 ){//使用URLEncoder解决cookie中中文问题String username URLEncoder.encode(request.getParameter(username),utf-8);String password URLEncoder.encode(request.getParameter(password),utf-8);Cookie usernameCookie new Cookie(username,username);Cookie passwordCookie new Cookie(password,password);usernameCookie.setMaxAge(864000);passwordCookie.setMaxAge(864000);response.addCookie(usernameCookie);response.addCookie(passwordCookie);}else{Cookie[] cookies request.getCookies();if(cookies!null cookies.length0){for(Cookie c:cookies){if(c.getName().equals(username)||c.getName().equals(password)){c.setMaxAge(0);response.addCookie(c);}}}}%a hrefusers.jsp target_blankcheck user info/a
/body
/html View Code users.jsp % page languagejava importjava.util.*,java.io.*,java.net.* contentTypetext/html; charsetutf-8%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd
html
head
meta http-equivContent-Type contenttext/html; charsetutf-8
/head
bodyh1cookie/h1%request.setCharacterEncoding(utf-8);String username ;String password ;Cookie[] cookies request.getCookies();if(cookies!null cookies.length0){for(Cookie c:cookies){if(c.getName().equals(username)){username URLDecoder.decode(c.getValue(),utf-8);}if(c.getName().equals(password)){password URLDecoder.decode(c.getValue(),utf-8);}}}%用戶名%username %密碼%password %
/body
/html View Code 其中关于编码问题可以参考中文乱码问题转载于:https://www.cnblogs.com/xing901022/p/4359732.html