In my application, we use java applet to write cookie on browser and use jquery to read this cookie. I tried do like tutorial but I always don't see the cookie set on my browser.
http://docs.oracle.com/javase/tutorial/deployment/doingMoreWithRIA/accessingCookies.html And here is my code: The first I create a class to handle the 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();
}
}
Then I call it in main applet (I use jquery to run this 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();
}
But I've checked the browser cookies and there's no UserName What is missing in this code?
Many thanks!