在我的应用程序中,我们使用java applet在浏览器上编写cookie并使用jquery来读取这个cookie。我试过像教程一样,但我总是看不到我的浏览器设置的cookie。
http://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/accessingCookies.html 这是我的代码: 第一个我创建一个类来处理cookie
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.util.List;
public class CookieAccessor implements CookieStore {
public CookieStore store;
public CookieManager manager;
public CookieAccessor() {
try {
manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
CookieHandler.setDefault(manager);
store = manager.getCookieStore();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void add(URI uri, HttpCookie cookie) {
// TODO Auto-generated method stub
store.add(uri, cookie);
}
@Override
public List<HttpCookie> get(URI uri) {
// TODO Auto-generated method stub
return store.get(uri);
}
@Override
public List<HttpCookie> getCookies() {
// TODO Auto-generated method stub
return store.getCookies();
}
@Override
public List<URI> getURIs() {
// TODO Auto-generated method stub
return store.getURIs();
}
@Override
public boolean remove(URI uri, HttpCookie cookie) {
// TODO Auto-generated method stub
return store.remove(uri, cookie);
}
@Override
public boolean removeAll() {
// TODO Auto-generated method stub
return store.removeAll();
}
}
然后我在主applet中调用它(我使用jquery来运行这个applet):
try {
cookie = new CookieAccessor();
HttpCookie cookiedata = new HttpCookie("UserName", "Jonh");
URL url = new URL(getCodeBase().toString());
cookie.add(url.toURI(), cookiedata);
} catch(Exception e) {
e.printStackTrace();
}
但是我已经检查了浏览器cookie并且没有UserName此代码中缺少什么?
非常感谢!