怎么用java获取cookie

发布网友 发布时间:2022-04-21 05:35

我来回答

3个回答

懂视网 时间:2022-05-04 03:14

来源:http://www.rgagnon.com/javadetails/java-0180.html Access Cookies from a Java Applet This Applet uses the package netscape.javascript.JSObject . To compile such program, you have to include in the CLASSPATH the file java40.jar if you

来源:http://www.rgagnon.com/javadetails/java-0180.html

Access Cookies from a Java Applet

This Applet uses the package netscape.javascript.JSObject. To compile such program, you have to include in the CLASSPATH the file java40.jar if you have the Netscape Communicator v4 installed or classes.zip for the Microsoft Internet Explorer. Compile with something like

Applet使用包netscape.javascript.JSObject。因为程序的需要,你首先把jre/lib/pulgin.jar加入到classpath路径中去,编译如下:

javac testcookie.java

NOTE: The netscape.javascript.* package is now included in %JRE_HOME%/lib/jaws.jar file.

注:包netscape.javascript.* package现在已经包含在%JRE_HOME%/lib/jaws.jar 文件中。

[HTML file (testCookie.html)]

网页文件:







[Java applet (TestCookie.java)]

Applet文件,TestCookie.java

import netscape.javascript.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class TestCookie extends Applet
implements ActionListener {
TextField tf1, tf2;
Button b1, b2, b3;


public void init() {
tf1 = new TextField(20);
tf2 = new TextField(20);

b1 = new Button("Write Cookie");
b2 = new Button("Read Cookie");
b3 = new Button("Delete Coookie");

setLayout(new FlowLayout());
add(tf1);
add(tf2);
add(b1);
add(b2);
add(b3);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
/*
** write a cookie
** computes the expiration date, good for 1 month
*/
java.util.Calendar c = java.util.Calendar.getInstance();
c.add(java.util.Calendar.MONTH, 1);
String expires = "; expires=" + c.getTime().toString();

String s1 = tf1.getText() + expires;
System.out.println(s1);

JSObject myBrowser = JSObject.getWindow(this);
JSObject myDocument = (JSObject) myBrowser.getMember("document");

myDocument.setMember("cookie", s1);
}

if (ae.getSource() == b2) {
/*
** read a cookie
*/
tf2.setText(getCookie());
}

if (ae.getSource() == b3) {
/*
** delete a cookie, set the expiration in the past
*/
java.util.Calendar c = java.util.Calendar.getInstance();
c.add(java.util.Calendar.MONTH, -1);
String expires = "; expires=" + c.getTime().toString();

String s1 = tf1.getText() + expires;
JSObject myBrowser = JSObject.getWindow(this);
JSObject myDocument = (JSObject) myBrowser.getMember("document");
myDocument.setMember("cookie", s1);
}
}

public String getCookie() {
/*
** get all cookies for a document
*/
try {
JSObject myBrowser = (JSObject) JSObject.getWindow(this);
JSObject myDocument = (JSObject) myBrowser.getMember("document");
String myCookie = (String)myDocument.getMember("cookie");
if (myCookie.length() > 0)
return myCookie;
}
catch (Exception e){
e.printStackTrace();
}
return "?";
}

public String getCookie(String name) {
/*
** get a specific cookie by its name, parse the cookie.
** not used in this Applet but can be useful
*/
String myCookie = getCookie();
String search = name + "=";
if (myCookie.length() > 0) {
int offset = myCookie.indexOf(search);
if (offset != -1) {
offset += search.length();
int end = myCookie.indexOf(";", offset);
if (end == -1) end = myCookie.length();
return myCookie.substring(offset,end);
}
else
System.out.println("Did not find cookie: "+name);
}
return "";
}
}

热心网友 时间:2022-05-04 00:22

ookie实际上是一个存在你硬盘里的数据,但是这些数据很特殊,只能由web应用提交给浏览器帮助存储,并且我们还能读取浏览器存在本地的cookie
web应用一般只在cookie中存储一些用户信息等少量且暂时的数据,数据量大则不适合存储在cookies
ava通过httpServletRequest接口来访问浏览器请求中的cookies数据(这里先了解一下cookies来龙去脉,代码待会一并给出)
每个cookie有两个属性:键 ,值(无特定格式字符串,所以可以diy存数据,不过要注意URL编码问题,编码问题待会和代码一同讲)
如果我们需要存储新的cookie我们可以new一个cookie实例 并通过httpservletRsponse提交到浏览器,进而存储到本地

热心网友 时间:2022-05-04 01:40

1.获取Cookie的值:获取Cookie值的标准代码格为:myCookie.Values["auth"];
上句代码可以获取名为myCookie的Cookie对象键名为auth的键值。如果不存在,则返回null。
DateTime now=new DateTime.Now;
Response.Cookies["Info"].Expires = DateTime.Now.AddDays(1);
//设定Cookie过期时间下面的代码示例演示删除应用程序中所有可用 Cookie 的一种方法
2.代码如下:
HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for (int i = 0; i < limit; i++)
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(aCookie);
}
请采纳,谢谢
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com