Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JSP page not reading javascript cookie correctly

Status
Not open for further replies.

Ito2233

MIS
Sep 2, 2003
77
0
0
US
I have a JSP page with some embedded javascript. When the page loads, it tries to detect the presence of a cookie by means of the "if (document.cookie.length > 0)" javascript statement. The strange thing is that, without a cookie present, the "document.cookie.length" statement returns either the number 57, or 55, or 56. If no cookie is present, how is this possible? Furthermore, the statement "document.cookie" returns the following value even when no cookie is present:
"sapj2ee_*=4001;JSESSIONID=ID4001DB0.02315627049120228End".
What is going on?
THe full code is displayed below. The javascript and accompanying body have been tested on a regular html file. I don't understand why it's not working on this jsp page. Any input would be highly appreciated. Thanks

Code:
<%@ page import="com.sapmarkets.isa.core.util.WebUtil" %>

<%@ taglib uri="/isa" prefix="isa" %>
<isa:contentType/>
<html>
<head>
  <title><isa:translate key="b2b.login.jsp.title"/></title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
if (document.cookie.length > 0)  {
    alert(document.cookie.length);
    var address;
    cookie_data = document.cookie;
    alert(cookie_data);
    cookie_pieces = cookie_data.split("=");
    alert(cookie_pieces);
    window.location = cookie_pieces[1];
}

function SetCookie() {  
var box = document.form1.region;
var chosen_value = box.options[box.selectedIndex].value;

if (document.form1.remember.checked == true) {
    var expiration = new Date("December 1, 2004");
    expiration = expiration.toGMTString();
    document.cookie = "name=" + chosen_value + ";expires=" + expiration;
}
}
//  End -->
</script>
</head>
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
<form method="post" name="form1" action="javascript:window.location=document.form1.region.options[document.form1.region.selectedIndex].value;" onSubmit="SetCookie();">
						
<p>Select a region:</p>
<select name="region">
<option value="[URL unfurl="true"]http://www.google.com">Google</option>[/URL]
<option value="[URL unfurl="true"]http://www.yahoo.com">Yahoo</option>[/URL]
<option value="[URL unfurl="true"]http://www.lycos.com">Lycos</option>[/URL]
</select> 
<input type="submit" name="Submit" value="Go"> 
<input type="checkbox" name="remember" value="yes">Remember my selection
</form>
</body>
</html>
 

document.cookie.length returns the whole length of the cookie string. i.e. length of "sapj2ee_*=4001;JSESSIONID=ID4001DB0.02315627049120228End"

The cookie string "JSESSIONID=....." is the session cookie set by the application server. So even your JSP page doesn't set any cookie, the application server adds a session cookie for session tracking.
 
How would I go about resolving this situation then? From what you're saying it seems that setting cookies with Javascript won't work, because JSP does what it wants. I know nothing about JSP, and I'm merely trying to add my HTML and Javascript code to a kind of template used here at the company.
I'd be grateful for any suggestions.
Thanks
 
Well if you are using a servlet container like Tomcat or something, you will always have a cookie present, and its name will always be JSESSIONID - there is no real way around this unless the browser diables cookies (at which point the container will use URL rewriting to track the session). You will just have to work around this I guess in your Javascript.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top