从Applet中读取Cookie:AccessCookiesfromaJavaApplet
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了从Applet中读取Cookie:AccessCookiesfromaJavaApplet,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4021字,纯文字阅读大概需要6分钟。
内容图文
来源: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 "";
}
}
内容总结
以上是互联网集市为您收集整理的从Applet中读取Cookie:AccessCookiesfromaJavaApplet全部内容,希望文章能够帮你解决从Applet中读取Cookie:AccessCookiesfromaJavaApplet所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。