I'm currently working on setting cookies using Java as demonstrated here.
My goal is to utilize this cookie in JavaScript (it's necessary to do it this way due to certain limitations). However, I'm unable to detect any set cookies (using the web developer addon for Firefox).
Is there a solution for this? Can cookies be used in this manner?
Here is the Java code snippet:
try {
// instantiate CookieManager
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
CookieStore cookieJar = manager.getCookieStore();
// create cookie
HttpCookie cookie = new HttpCookie("UserName", str);
// add cookie to CookieStore for a
// specific URL
URL url = new URL("http://host.example.com");
cookieJar.add(url.toURI(), cookie);
System.out.println("Added cookie using cookie handler");
} catch(Exception e) {
System.out.println("Unable to set cookie using CookieHandler");
e.printStackTrace();
}
And here is the JavaScript snippet:
function Cookie(cname){
alert("in getcookie function");
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0)
document.getElementById("result").innerHTML=c.substring(name.length,c.length);
}
console.log(document.cookie);
//document.getElementById("result").innerHTML="somewhere something went wrong!";
}
</script>
<div id="result">
<p onclick="Cookie('JSESSIONID')">Click me</p>
</div>
The console.log did not show any output.