Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Strange session/cookie issues - tomcat/JSP

843836Nov 26 2003 — edited Dec 1 2003
I'm using Tomcat 4.1 on Win2K. It uses JDK 1.4. The client browser is IE 6.

I've been having some strange issues attempting to manage state in an internal JSP application. Since it will only be used by internal employees (thus the assurance of IE 6 clients), I have some flexibility in how I manage state.

My first attempt was using the session object. I would set values for the user allowing the page to function properly. However, when more than one user was accessing the tool, Tomcat would get the sessions confused and would send the wrong data to the wrong user request. It was weird seeing the page jump from your selections to some other user's selections.

Okay, so I thought I'd use cookies stored on the user's hard drive. (Again, this is an internal tool and we have set IE 6 as our browsers and cookies "privacy" must be no higher than medium.) On the development machine, my local, everything works fine. I set the cookie and it is returned and displayed as required. But on any other machine, only the JSESSIONID cookie is displayed. The custom cookie I've set with an ID never gets set. It never get's written to the hard drive. I've even set the browser to accept all cookies with no luck.

Here's my cookie code.

//look for cookie
Cookie[] oreos = request.getCookies();
boolean foundCookie = false;
int cookieage = 8*60*60;

if (oreos!=null)
{
out.println("we have some cookies<br>");
for(int loopIndex = 0; loopIndex < oreos.length; loopIndex++)
{
out.println("<br>Cookie: "+oreos[loopIndex].getName()+" Value: "+oreos[loopIndex].getValue());
out.println("<br>Domain is "+oreos[loopIndex].getDomain());
out.println("<br>path is "+oreos[loopIndex].getPath());
out.println("<br>MaxAge is "+oreos[loopIndex].getMaxAge());
Cookie cookie1 = oreos[loopIndex];
if (cookie1.getName().equals("tid")) {
intTrackID = cookie1.getValue();
//cookie1.setMaxAge(cookieage);
//response.addCookie(cookie1);
foundCookie = true;
}
}
}

Later, if !foundCookie, I run some code to generate a record and return an id value for my cookie like this.

if (!foundCookie)
{
out.println("we didn't find the tid cookie<br>");
try {
sql = "use panel_master exec usp_newTracker";
ResultSet rs1 = DBGetRecSet(cnn, sql, false);
//ResultSet rs = stmt.execute(sql);
sql = "";
boolean rs1_isEmpty = !rs1.next();
if (!rs1_isEmpty)
{intTrackID = rs1.getString("newtid");}
rs1.close();
out.println("new tid value: " + intTrackID + "<br>");
setTrackerValue(dbTrackerServ, intTrackID, "current_tool_id", "24");
Cookie cookie1 = new Cookie("tid", intTrackID);
cookie1.setMaxAge(cookieage); //expires in 8 hours
cookie1.setPath("/EmailManagement");
response.addCookie(cookie1);
out.println("added new cookie to response<br>");
//rsEMTracker.updateString("current_tool_id", "24"); // updates current_tool_id value
//rsEMTracker.updateRow(); // updates the row in the data source
}
catch (SQLException se) {
System.out.println("Could not create a new session tracker record.<br>");
System.out.println(se);
}

} else {
//out.println("trying to get tracking record<br>");
try {
sql = "use panel_master select * from emsesstrack where emtrack_id = " + intTrackID;
//out.println("sql: " + sql + "<br>");
rsEMTracker = DBGetRecSet(cnn, sql, false);
//rsEMTracker = stmt.executeQuery(sql);
rsEMTracker_hasData = rsEMTracker.next();
//out.println("rsEMTracker_isEmpty: " + rsEMTracker_isEmpty + "<br>");
sql = "";
if (!rsEMTracker_hasData)
{
try {
sql = "use panel_master exec usp_newTracker";
ResultSet rs2 = DBGetRecSet(cnn, sql, false);
//ResultSet rs = stmt.execute(sql);
sql = "";
boolean rs2_isEmpty = !rs2.next();
if (!rs2_isEmpty)
{intTrackID = rs2.getString("newtid");}
rs2.close();
setTrackerValue(dbTrackerServ, intTrackID, "current_tool_id", "24");
Cookie cookie2 = new Cookie("tid", intTrackID);
cookie2.setMaxAge(cookieage); //expires in 8 hours
cookie2.setPath("/EmailManagement");
response.addCookie(cookie2);
//sql = "use panel_master select * from emsesstrack where emtrack_id = " + intTrackID;
//rsEMTracker = DBGetRecSet(cnn, sql, false);
//rsEMTracker = stmt.executeQuery(sql);
//rsEMTracker_isEmpty = rsEMTracker.next();
//rsEMTracker_hasData = !rsEMTracker_isEmpty;
//if (rsEMTracker_hasData)
//{
// rsEMTracker.updateString("current_tool_id", "24"); // updates current_tool_id value
// rsEMTracker.updateRow(); // updates the row in the data source
//}
//sql = "";
} catch (SQLException se) {
System.out.println("Could not create a new session tracker record.<br>");
System.out.println(se);
}
} else {
setTrackerValue(dbTrackerServ, intTrackID, "current_tool_id", "24");
Cookie cookie3 = new Cookie("tid", intTrackID);
cookie3.setMaxAge(cookieage); //expires in 8 hours
cookie3.setPath("/EmailManagement");
response.addCookie(cookie3);
//rsEMTracker.updateString("current_tool_id", "24"); // updates current_tool_id value
//rsEMTracker.updateRow(); // updates the row in the data source
}
rsEMTracker.close();
} catch (SQLException se) {
System.out.println("Could not get the session tracker record.<br>");
System.out.println(se);
}
}

I would appreciate any help. I'm at my wit's end and ready to switch over to ASP.NET. I've only ever programmed ASP. I took some Java classes and I thought working in JSP would be similar. It is, but I think my main problem here may actually be Tomcat. I have all the default settings for the site context.

-sotech_j
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 29 2003
Added on Nov 26 2003
9 comments
803 views