Popular Posts
abap naming rule 命名規則 報表程式(以列表格式輸出資料分析):Yaxxxxxx或Zaxxxxxx。用應用程式區的分類字母替換a。 任何有效字元替換x。注意SAP報表程式遵守相似的命名約定:Raxxxxxx。 任何其他ABAP/4程式(培訓程式或事務程式):SAPMYxxx或SAPMZxxx... IDES 4.7 Installation 電腦名稱不能使用特殊名稱(bin/etc/var ...) 網路卡-> File and Printer Sharing for Microsoft Networks ->網路應用程式的資料輸送量最大化 安裝jdk1.4 (不升級) 設置JAVA_HOME ... Data type 資料類型 預設大小 大小 初始值 輸出長度 輸出定位 說明 C 1 1-65535 SPACE 字串長度 LEFT-JUSTIFIED 字...
Blog Archive
Stats
set/remove cookie using applet
jdk/jre 1.4 later, the library is included in plugin.jar file.
import java.applet.Applet;
import java.util.ArrayList;
import java.util.Date;

import netscape.javascript.JSObject;
import sgb.util.Strings;

public class CookieManager {
    private Applet applet;

    public CookieManager(Applet applet) {
        this.applet = applet;
    }

    private JSObject getDocument() {
        JSObject win = (JSObject) JSObject.getWindow(applet);
        return (JSObject) win.getMember("document");
    }

    public void setCookie(String name, String value) {
        if (Strings.isNullOrEmpty(name))
            throw new IllegalArgumentException();
        Cookie cookie = new Cookie();
        cookie.setName(name);
        cookie.setValue(value);
        setCookie(cookie);
    }

    public void setCookie(Cookie cookie) {
        if (cookie == null)
            throw new IllegalArgumentException();
        JSObject doc = getDocument();
        doc.setMember("cookie", cookie.toString());
    }

    public String getCookieValue(String name) {
        return getCookie(name).getValue();
    }

    public Cookie getCookie(String name) {
        if (Strings.isNullOrEmpty(name))
            throw new IllegalArgumentException();
        Cookie[] cookies = getCookies();
        for (Cookie cookie : cookies)
            if (cookie.getName().equals(name))
                return cookie;
        return null;
    }

    public Cookie[] getCookies() {
        JSObject doc = getDocument();
        String[] token = ((String) doc.getMember("cookie")).trim().split(";");
        ArrayList<Cookie> cks = new ArrayList<Cookie>();
        for (String tk : token) {
            String[] tks = tk.trim().split("=");
            String name = tks[0];
            String value = tks.length > 1 ? tks[1] : null;
            Cookie cookie = new Cookie();
            cookie.setName(name);
            cookie.setValue(value);
            cks.add(cookie);
        }
        return cks.toArray(new Cookie[0]);
    }

    public void removeCookie(String name) {
        if (Strings.isNullOrEmpty(name))
            throw new IllegalArgumentException();
        Cookie cookie = new Cookie();
        cookie.setName(name);
        cookie.setExpireDate(new Date(0));
        setCookie(cookie);
    }

}