从Applet中读取Cookie

发表于:2008-7-18 10:21

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:fenglibing    来源:CSDNblog

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

javac testcookie.java

        注:包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)]

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 "";
        }
}






《2023软件测试行业现状调查报告》独家发布~

关注51Testing

相关阅读

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号