前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Servlet中请给出一个Cookie的增删改查的例子。

Servlet中请给出一个Cookie的增删改查的例子。

作者头像
马克java社区
修改2021-07-09 10:51:16
2810
修改2021-07-09 10:51:16
举报
文章被收录于专栏:java大数据java大数据

2.Servlet与Cookie:

马克-to-win:Cookie有点像Session。Session是把键值对存在服务器端,一个Servlet存值 ,另外一个Servlet可以取值。Cookie也是以键值对的形式用于读取,不过是保存在客户端 浏览器的某个文本里面。取时,也要从这台机器的这个浏览器上去取。像Session一样,你 也可以设置过期时间,比如“一年”。和Session不同的是:用户可以把自己浏览器的Cookie工作系统关掉。这就是Cookie不如Session的重要的原因。不可靠,不保险。程序员编的程序都白费了。另外,对于Cookie来讲,servlet只能拿回属于自己整个Web应用的Cookie(别人的Web应用不行)。当然了,Session范围更小,只能拿回自己用户浏览器写过的东西。

马克-to-win:底下,我就给出一个Cookie的增删改查的例子。只需运行cookie.html。这个html带动四个增删改查Servlet。读者可以先增加Cookie,之后查询一下,再删除,再查询一下。反正自己研究研究这个例子。实验使用ie8做的,cookie的查找在例子当中。

注意此文件不能直接打开,只能拷贝到别的目录下,之后用记事本打开。我就这样打开后,给大家看一下。

mark-to-win0

yes0

localhost/ServletHello/

1024

2493888000

30731375

472266800

30657950

*

mark-to-win

yes

localhost/ServletHello/

1024

2172629504

30657951

472376800

30657950

*

执行了下面的delete之后,变成了:

mark-to-win0

yes0

localhost/ServletHello/

1024

414149632

30731383

2685375728

30657957

*

例:4.2.1:

cookie.html:

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Cookie Test</title>

</head>

<body>

<FORM ACTION="AddCookie" METHOD="POST">

add<INPUT TYPE="SUBMIT" VALUE="Submit">

</FORM><br>

<FORM ACTION="DeleteCookie" METHOD="POST">

delete<INPUT TYPE="SUBMIT" VALUE="Submit">

</FORM><br>

<FORM ACTION="QueryCookie" METHOD="POST">

Query<INPUT TYPE="SUBMIT" VALUE="Submit">

</FORM><br>

<FORM ACTION="ModifyCookie" METHOD="POST">

Modify<INPUT TYPE="SUBMIT" VALUE="Submit">

</FORM><br>

</body>

</html>

package com;

import java.io.IOException;

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 AddCookie extends HttpServlet {

protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

Cookie returnVisitorCookie0 = new Cookie("mark-to-win0", "yes0");

Cookie returnVisitorCookie = new Cookie("mark-to-win", "yes");

Cookie returnVisitorCookie1 = new Cookie("mark-to-win1", "yes1");

/* public void setMaxAge(int expiry) Sets the maximum age of the cookie

* in seconds. A positive value indicates that the cookie will expire

* after that many seconds have passed. A negative value means that the

* cookie is not stored persistently and it won't be recorded in the

* file. will be deleted when the Web browser exits. A zero value causes

* the cookie to be deleted. By default, the value is -1, -1 indicating

* the cookie will persist until browser shutdown. in the cookie file,

* if the value is negative, it won't be recorded in the file. As long

* as the value is positive, itself and its expiry time will be recorded

* in the file. After that moment, that cookie expires, but that cookie

* is not deleted from the file, any way, there is schedule to expire in

* the file.另外,当你设置cookie时长为0,删除它后,如文件中所有cookie都被你用这种方法删除后,文件也会被ie自动删掉。你如果想删cookie文件,正常的得通过ie8设置中的删除钮。如果想看cookie文件,得拷贝文件到其他地方,之后用记事本看。*/

returnVisitorCookie0.setMaxAge(60 * 60 * 24 * 365); // 1 yea

returnVisitorCookie.setMaxAge(10 * 60 * 1);

response.addCookie(returnVisitorCookie0);

response.addCookie(returnVisitorCookie);

response.addCookie(returnVisitorCookie1);

response.sendRedirect("cookie.html");

}

}

更多请看:https://blog.csdn.net/qq_44594371/article/details/103166189

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档