从Applet中读取Cookie:Access Cookies from a Java Applet
来源:互联网 发布:仿真软件实体对比 编辑:程序博客网 时间:2024/06/05 09:02
来源: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)]
网页文件:
<HTML>
<HEAD></HEAD>
<BODY>
<APPLET CODE=TestCookie.class MAYSCRIPT HEIGHT=150 WIDTH=200>
</APPLET>
</BODY>
</HTML>
[Java applet (TestCookie.java)]
Applet文件,TestCookie.java
import netscape.javascript.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TestCookie extends Applet
implements ActionListener {
TextField tf1, tf2;
Button b1, b2, b3;
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);
}
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());
}
/*
** 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();
/*
** 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);
}
}
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 "?";
}
/*
** 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 "";
}
}
/*
** 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:Access Cookies from a Java Applet
- 从Applet中读取Cookie:Access Cookies from a Java Applet
- java Applet从网页读取数据
- Java applet 写客户端 cookie
- java.applet.Applet类
- [java] a simple Applet program
- Java Applet draw a snowman
- JAVA中Applet的生命周期
- java applet
- java applet
- Java Applet
- JAVA Applet
- java Applet
- java applet
- Java applet
- Java Applet
- java applet
- java--Applet
- 如何组织代码使得代码的效率高
- SoftICE出现Error Code 31无法启动的解决办法
- WinForm:DockPanel实现窗体的停靠
- ShellCode汇编内存自动提取
- 游戏数值初解
- 从Applet中读取Cookie:Access Cookies from a Java Applet
- CSS定义表格边框大全(细线/虚线/点线)(转贴)
- 龙芯首席设计师胡伟武:坚持毛泽东思想搞研发
- 迪拜(二)初印象
- 如何使你自己更具魅力
- 一个高效的数据分页的存储过程
- 从数据库中读取图片文件和将图片和写入到数据库中
- java高级应用之框架篇
- SYMBIAN中的字符串操作