- 浏览: 14983 次
- 性别:
- 来自: 天津
-
最新评论
1. 发送Cookie对象
java.util.Date date=new java.util.Date(); Cookie c=new Cookie("lastVisited",date.toString()); c.setMaxAge(60*60*24);//设置有效时间. c.setPath(); //设置路径 response.addCookie(c);//存储Cookie文件
2.读取Cookie
Cookie[] cookies=request.getCookies(); Cookie c=null; if(cookies!=null){ for(int i=0;i<cookies.length;i++){ c=cookies[i]; out.println("Cookie name:"+c.getName()+"<br>"); out.println("Cookie value:"+c.getValue()+"<br>"); } }
3.修改Cookie
Cookie[] cookies=request.getCookies(); Cookie c=null; for(int i=0;i<cookies.length;i++){ c=cookies[i]; if(c.getName().equal("lastvisited")){ c.setValue("2010-11-11"); c.setMaxValue(60*60*24); response.addCookie(c); } }
4.删除Cookie
Cookie[] cookies=request.getCookies(); Cookie c=null; for(int i=0;i<cookies.length;i++){ c=cookies[i]; if(c.getName().equal("lastvisited")){ c.setMaxValue(0); response.addCookie(c); } }
使用Cookie的注意事项:
1.Cookie的大小和数量是有限制的.只能是英文字符和数字,不能是汉字.
2.Cookie的个人硬盘上所保存的文本信息以明文格式进行保存.没有任何加密措施.
3.浏览器用户可以设定不适用Cookie
例子:
web project:Cookie
编写username.html
编写SetCookieServlet.java(Servlet),映射为/servlet/setServlet
编写GetCookieServlet.java(Servlet)映射为/servlet/getServlet
编写StringUtil.java 用来过滤一些恶意代码.
username.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <form id="form1" name="form1" method="post" action="/Cookie/servlet/setCookies"> <table width="361" border="1"> <tr> <th colspan="2" scope="col"><div align="left">请输入用户名:</div></th> </tr> <tr> <th scope="row">用户名:</th> <td><input type="text" name="username" id="username" /></td> </tr> <tr> <th scope="row"><input type="reset" name="reset" id="reset" value="重置" /></th> <td><input type="submit" name="submit" id="submit" value="提交" /></td> </tr> </table> </form> </body> </html>
SetCookieServlet.java:
import java.io.IOException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.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; import com.v512.guestbook.StringUtil; public class SetCookiesServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); String outputContent=null; if (!StringUtil.validateNull(username)) { Cookie cookie1=new Cookie ("username",StringUtil.filterHtml(username)); cookie1.setMaxAge(24*60*60*30); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Cookie cookie2=new Cookie("lastVisited", sdf.format(new Date())); cookie2.setMaxAge(24*60*60*30); response.addCookie(cookie1); response.addCookie(cookie2); outputContent="本次登录的用户名与时间已经写到Cookie当中.<br><a href='/Cookie/servlet/getCookies'>读取Cookie</a><br>"; }else { outputContent="本次记录没有写到Cookie当中.<br><a href='/Cookie/username.html'>重新输入</a><br>"; } response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>set cookie</TITLE></HEAD>"); out.println(" <BODY>"); out.println("<h2>"+outputContent+"</h2>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
3.GetCookieServlet.java
import java.io.IOException; import java.io.PrintWriter; import java.net.CookiePolicy; 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 GetCookiesServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println("<h2>从Cookie读取的上次登录日期与用户名</h2>"); Cookie[] cookies=request.getCookies(); Cookie cookie=null; if (cookies!=null) { for (int i = 0; i < cookies.length; i++) { cookie=cookies[i]; if (cookie.getName().equals("username")) { out.println("用户名:"+cookie.getValue()+"<br>"); } if (cookie.getName().equals("lastVisited")) { out.println("上次登录的日期:"+cookie.getValue()+"<br>"); } } } out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
StringUtil.java
public class StringUtil { public static boolean validateNull(String args) { if (args == null || args.length() == 0) { return true; } else { return false; } } public static String ChangeNull(String source, String target) { if (source == null || source.length() == 0 || source.equalsIgnoreCase("null")) { return target; } else { return source; } } // 过滤一些恶意的代码 public static String filterHtml(String input) { if (input == null) { return null; } if (input.length() == 0) { return input; } input = input.replaceAll("&", "&"); input = input.replaceAll("<", "<"); input = input.replaceAll(">", ">"); input = input.replaceAll(" ", " "); input = input.replaceAll("'", "'"); input = input.replaceAll("\"", """); input = input.replaceAll("\n", "<br>");// 空格过滤一定要放在最后面! return input; } }
发表评论
-
成为Java高手的25个学习要点
2011-03-16 13:48 803成为Java高手的25个学习 ... -
对数字进行四舍五入的方法
2010-12-28 16:35 1146使用Math中的round方法 import java.u ... -
关于数据类型
2010-12-28 15:22 588本文章是从网上摘下来了.http://www.360doc.c ... -
测试对象的等价性和等值性
2010-12-28 14:42 824public class qwe{ public sta ... -
java数组
2010-12-22 15:55 708java数组不同于c++和c,因为java数组可以被确保实例化 ... -
对象存储在什么地方
2010-12-22 15:32 1602程序运行时,对象放到哪里.,内存是怎么样分配. 有一共六个不 ... -
java从基础开始学起!
2010-12-01 10:53 683写博客就当是学习笔记. 1.线程和进程的概念.以为一直很 ...
相关推荐
在Web开发中,Cookie是一种非常重要的技术,用于在客户端和服务器之间传递状态信息。Cookie的主要功能包括用户会话管理、个性化设置保存、跟踪用户行为等。在这个“cookie读写,子cookie”主题中,我们将深入探讨...
### 关于Java应用Cookie知识点详解 #### 一、Cookie简介 Cookie是一种常用的技术,用于存储用户的访问信息。在Web开发中,服务器可以将少量的信息发送到客户端浏览器,并存储在客户端,这种信息就是Cookie。当...
在IT领域,尤其是在Web开发中,Cookie的管理与安全至关重要。C#作为一种广泛使用的编程语言,在处理Cookie方面提供了丰富的功能,包括对Cookie的加密与解密。本文将深入探讨如何在C#中操作Cookie,以及如何实现...
《EditThisCookie.crx:强大的Cookie管理工具及与curl的集成》 在互联网浏览中,Cookie扮演着重要的角色,它们存储用户信息、保持登录状态、个性化网页内容等。EditThisCookie.crx是一款专为Chrome浏览器设计的强大...
FlashCookie,也称为Local Shared Objects (LSO),是Adobe Flash Player为存储用户特定数据而引入的一种机制。不同于传统的HTTP Cookie,这些数据存储在用户的计算机上,不受浏览器的隐私设置或清除浏览器缓存操作的...
var UserValue = unescape(allCookies.substring(cookie_User, cookie_UserEnd)); // 提取PwdCookie的值 cookie_Pwd += "PwdCookie".length + 1; var cookie_PwdEnd = allCookies.indexOf(";", cookie_Pwd); ...
### browser-cookie3:获取浏览器cookie #### 一、概述 `browser-cookie3` 是一个用于Python 3的库,能够帮助用户将当前浏览器中的cookies提取出来,并存储到一个`cookiejar`对象中,从而实现无需登录即可通过...
本文将详细讲解如何使用微信小程序中的cookie维护插件,实现自动设置和获取Cookie的功能,并特别关注增加的Cookie超时处理机制。 首先,理解Cookie的基本概念。Cookie是由服务器端生成,发送到客户端(浏览器)的一...
Cookies的处理 作用 ...如果在请求的过程中产生了cookie,如果该请求使用session发起的,则cookie会被自动存储到session中. 案例 爬取雪球网中的新闻资讯数据:https://xueqiu.com/ #获取一个sessi
标题中的“获取cookie非常好用的google插件”指的是在Google Chrome浏览器上的一种扩展程序,它允许用户方便地管理和操作浏览器中的Cookie数据。Cookie是网站在用户计算机上存储的小型文本文件,用于跟踪用户偏好、...
在易语言中,“取设cookie”是网络编程中的一个重要概念,主要用于处理网页浏览过程中的数据存储与交互。 Cookie是在HTTP协议下,服务器发送到用户浏览器并保存在本地的一小块数据,它用于记录用户的浏览历史、登录...
### Android Cookie 获取与设置以及 WebView 的 Cookie 同步 在 Android 开发中,Cookie 的管理和同步对于维护用户会话状态至关重要。特别是在使用 HttpClient 发起网络请求时,保持客户端与服务器端 Session 的...
$.cookie('the_cookie'); // 获得cookie $.cookie('the_cookie', 'the_value'); // 设置cookie $.cookie('the_cookie', 'the_value', { expires: 7 }); //设置带时间的cookie $.cookie('the_cookie', '', { expires:...
当用户访问某个网站时,该网站可能会将 Cookie 信息写入到用户的浏览器中,以便下次用户访问该网站时能够快速识别用户的身份。 为什么需要模拟 Cookie 登陆? 在实际开发中,我们可能需要模拟用户的登陆行为,以便...
jdcookie.js下载 JD cookie一键获取脚本,基于JavaScript jdcookie.js下载 JD cookie一键获取脚本,基于JavaScript jdcookie.js下载 JD cookie一键获取脚本,基于JavaScript jdcookie.js下载 JD cookie一键获取脚本...
标题中的“20210820cookie万能获取助手.rar”暗示这是一款在2021年8月20日发布的工具,主要用于获取Web应用程序的Cookie信息。Cookie是Web服务器存储在用户浏览器上的一小段数据,用于跟踪用户状态、个性化设置以及...
在QT中访问网页并获取Cookie是网络编程的一个重要部分,尤其对于需要实现自动化登录、跟踪用户状态或者进行Web服务交互的应用来说。 Cookie是一种在客户端和服务器之间传递信息的小型文本文件。服务器在响应HTTP...
它的关键方法包括`getName()`(获取Cookie的名称)、`getValue()`(获取Cookie的值)、`setComment()`(设置Cookie的注释)、`setMaxAge()`(设置Cookie的有效期)以及`setPath()`和`setDomain()`(分别用于指定...
桂林老兵Cookie欺骗工具是一款在IT安全领域中被广泛讨论的工具,主要用于网络安全研究和测试。这个工具因其在Cookie管理及欺骗技术方面的应用而知名,尤其对于理解Web安全和防御策略的专家来说,它是一个重要的学习...
### Extjs 中关于 Cookie 的操作 #### 一、引言 在 Web 开发中,Cookie 是一种常用的数据存储方式,用于保存用户的一些基本信息或者状态,从而实现网站的个性化设置或登录状态保持等功能。Extjs 作为一种强大的 ...