Sessions resetting

Hi

Ok I googled for this and actually found the question somewhere but no-one had answered it.

I am running tomcat as localhost on XP windows profesional and util tuesday morning I was happily testing my software with session variables all working nicely. and then poof - all gone, every page is a new session. The software works fine when I bring it to school - sessions work fine.

I checked all my internet settings - others beside me use this computer. all my cookies work when I am on the net. Please help I need to be able to test & develop on this computer.

I am using the jakarta-tomcat-5.0.25 and Java 2 development kit standard edition version 5.0 (came with the Deitel book Java how to program 6thed)

Lena

[757 byte] By [LenaBa] at [2007-11-26 14:52:54]
# 1

Can you give us an example of your code, from a few JSP pages?

In the JSP page directive have you ever set the session to false? If so, you must set it to true.

From here:

http://java.sun.com/products/jsp/syntax/2.0/syntaxref2010.html#15653

session="true|false"

Whether the client must join an HTTP session in order to use the JSP page. If the value is true, the session object refers to the current or new session.

If the value is false, you cannot use the session object or a <jsp:useBean> element with scope=session in the JSP page. Either of these usages would cause a translation-time error.

The default value is true.

The latest stable release of Tomcat is version 5.5, in case you were interested in upgrading it.

appy77a at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

[nobr]Hi

I tried adding that and it didn't make any difference.

The thing is the sessions were working at 9pm monday and not working on tuesday morning. I joyfully closed the browser on monday night knowing I had the code with sessions working and made no other changes. There was some surfing (by someone else) in between and some restarts . I did a complete shutdown hoping it was some glitch.

This is now the opening page, (there are 12 other pages so far) it has a lot of crud in it as I tried to work around the problem.

I never went into any tomcat files I am still strictly at the "follow the steps in the book" level in Java(6 weeks). This is my first jsp code however I do have 25 years of programming in other languages.

Thanks for your help

Lena

<?xml version = "1.0"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">

<head>

<title>Acme Games Welcome page</title>

<LINK href="Styles.css" type="text/css" rel="stylesheet"/>

<jsp:useBean id="vKount" scope="page"

class = "com.cdi.PP4.jsp.beans.VisCounter" />

<jsp:useBean id = "cust" scope = "page"

class = "com.cdi.PP4.jsp.beans.custRecord" />

<jsp:useBean id = "custData" scope = "request"

class = "com.cdi.PP4.jsp.beans.custData" />

<!-- somewhere we have to check if they are already logged in -->

</head>

<body>

<%

session =request.getSession();

boolean x=session.isNew();

// check the session values to see if user already logged on

String cdN=(String)session.getAttribute("userNm");

Cookie updck;

updck= new Cookie("updKnt","0");

updck.setMaxAge(-1);

response.addCookie(updck);

cust.setUserName(cdN);

String fd=custData.findCust(cust);

String fPath=application.getRealPath("Project/visitorsCount.txt");

vKount.countIt(fPath);

int theCount=vKount.getCounter();

vKount.writeIt(fPath);

%>

<table>

<tr>

<td style="width: 160px; text-align:center">

<img src="games/acelogoG.gif" alt="ACME GamesLB" width="140" height="140"/>

</td>

<td>

<%-- banner --%>

<jsp:include page= "banner.html"

flush="true"/>

</td>

</tr>

<tr>

<td style="width: 160px">

<%-- links --%>

<jsp:include page="links.html"

flush="true"/><br />

<h4>Visitors : <%= theCount %></h4>

</td>

<td style="vertical-align: top">

<%-- main window--%>

<jsp:include page="mainView.html"

flush="true"/>

<br />

<!-- this is the begining of the login logic first time thru -->

<!-- there is nothing in the class cust, when they push submit then -->

<!-- it is a post back then there is and it is verified -->

<jsp:setProperty name="cust" property="userName" param ="userName"/>

<jsp:setProperty name="cust" property="userPass" param="userPass"/>

<% // start scriplet

if(cust.getUserName()==null ||cust.getUserPass()==null)

{

%>

<form name="logIn" method="post" action="Home.jsp">

<table bgcolor="#ff00ff" >

<tr>

<td>UserID:&nbsp <input type="text" name="userName" />

</td>

<td>Password:&nbsp <input type="password" name="userPass" />

</td>

<td><input type="submit" value="Submit" /></td>

<td>

</td>

</tr>

</table>

</form>

<% // contine

}

else

{

String nm="XY";

String cd=cust.getUserName();

nm=custData.checkPswd(cust);

if(nm.equals("XX"))

{

// error

out.println("<h4>Incorrect Login</h4>");

}

else

{

session =request.getSession();

boolean x1=session.isNew();

session.setAttribute("userNm", cd);

String cdN2=(String)session.getAttribute("userNm");

boolean x2=session.isNew();

int ii=session.getMaxInactiveInterval();

String cdxx=session.getId();

%>

<h4> Welcome to Acme games <%= nm %> </h4>

<a href="Parent.jsp?userName=<%= cd %>">Parent's page</a>

visitor count: <%= theCount %>

<%

}

} // end of login form verification

%>

</td>

</tr>

</table>

</body>

</html>

[/nobr]

LenaBa at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I don't know if you know this already, but a session gets timed-out after a certain amount of time.

So, if you leave the page idel for more than (say 30 minutes) , and come back to it later, the session will not be valid anymore.

The only way to make the session object appear again is to refresh the browser.

Also, you need to prevent the page from getting cached in the browser.

If the browser is trying to access a cached version of the page instead of the live version from the server, then also session becomes invalid.

There are a few things you can do to preven caching in some popular browsers:

response.setHeader( "Expires", "Sat, 6 May 1995 12:00:00 GMT" );

/* set standard HTTP/1.1 no-cache headers*/

response.setHeader( "Cache-Control", "no-store, no-cache, must-revalidate" );

/* set IE extended HTTP/1.1 no-cache headers*/

response.addHeader( "Cache-Control", "post-check=0, pre-check=0" );

/* set standard HTTP/1.0 no-cache header*/

response.setHeader( "Pragma", "no-cache" );

Also instead of session=request.getSession();

Change the code to either:

request.getSession(true); This gets the session if it already exists, otherwise it creates a new one.

or

request.getSession(false); This gets the session if it already exists but does not create a new one if it doesn't.

Off topic, but I noticed that your JSP code contains scriplets ( Java code between <% %> ), as your schedule permits you may want to consider using JSTL1.1, EL in your JSP code instead of Java scriptlets. and encapsulate most of the logic inside JavaBeans that can then be invoked from the JSP page. Using JSTL instead of scriplets will make your code much more easier to read and maintain, and it's the new way of writing JSPs.

Message was edited by:

appy77

appy77a at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

hi Apply77

Each page as it loads a new page is treated as a new session. I tried all the variations of getSession.First they all worked then they didn't.I will try the other suggestions.

I am using Firefox but the same thing happens in IE.

I think when I finish the project I will upgrade everything.

As far as the code is concerned it is practically a copy of the text book. (Java How to program 6Th ed by Deitel) Just have to get this thing running to pass the course.

Then I have to write some real stuff so I have to get things working again this month.

Can you recommend a good book that will get me the next level of Java programming? On line tutorials drive me crazy. I want a book I can page through.

Thank you for your help

Lena

LenaBa at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Let's try a couple simpe things to trouble shoot the problem.

First go into the cong/server.xml file for Tomcat and enable the access logging. Just search the file for "Access log" and uncomment the Valve XML element.

Second open a browser and navigate to you web application. Then type the command:

javascript:document.cookie

in the addrs bar. Verify that there is a cookie named JSESSIONID. Note the value of this cookie.

Go to the next page in your web app and repeat the javascript command. verify that the value of the JSESSIONID has changed or not.

tolmanka at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Hi

JSESSIONID doesn't show up with the "javascript:document.cookie" but if I go into firefox tools and show cookies it does.

My cookie that I tried to use doesn't change values. I tried in the next page to change its value but it doesn't seem to work

code used for cookies create:

Cookie updck;

updck= new Cookie("updKnt","999");

updck.setMaxAge(-1);

response.addCookie(updck);

code used in further page to modify:

Cookie[] updck=request.getCookies();

String updV="";

for (int i=0; i<updck.length; i++)

{

String ckN=updck[i].getName();

if (ckN.equals("updKnt"))

{

updV=updck[i].getValue();

updck[i].setValue("0");

}

}

Note that they don't teach cookies in the course and this is what I was able to figure out . Should there be some response.xxxx to get the changed cookie to record?

I really appreciate your help with this

Lena

(Live was so much easier with cards, octal dumps and when multitasking meant printing at the same time as processing)>

LenaBa at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
If you correctly entered the javascript:document.cookie command on the address bar and did not see the JSESSIONID cookei there is a problem on your system and not in the code. An easy test is to try it on this forum page and see if it displays the JSESSIONID cookie.
tolmanka at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
HiIt shows up on this. yes. I know it is something that changed on my system. The problem is only on localhost with tomcat .Lena
LenaBa at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

When then there is no point in posting code since the problem is not a Java issue.

Re-check the browser configurations. Most browsers have seperate security settings for internet and local addresses.

Check your firewall, anti-virus, anti-spyware applications to make sure that they are not blocking cookies from localhost.

Look on the Tomcat mailing list for any configration issues that could cause this issue.

If all else fails re-write you code to use url re-writing instead of cookie based sessions.

tolmanka at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

Under [Tomcat_home]/conf/web.xml , you can check to see if at least you have the default session settings:

<session-config>

<session-timeout>30</session-timeout>

</session-config>

> hi Apply77

> Then I have to write some real stuff so I have to get

> things working again this month.

> Can you recommend a good book that will get me the

> next level of Java programming?

I don't know any recent books, you can check people's reviews on books

to see which ones are good.

As far as JSTL is concerned, I discovered that it's a better way to program from people at work. Then I tried it with a few examples and realized that it is indeed much more easier to use than scriptlets.

Since you are new to JSP, you might as well learn from the book you have and understand the basic concepts before moving to JSTL.

But for professional code, I recommend using JSTL.

On line tutorials

> drive me crazy. I want a book I can page through.

>

> Thank you for your help

>

> Lena

appy77a at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

Actually I see a problem in your code.

You are using Session cookies instead of Persistant cookies.

With session cookies you set maxAge= -1

I suggest you change your code to Persistant cookies,

so change the cookie.setMaxAge(24*60*60*2);

Also change your browser settings to allow cookies, that is Medium/Default setting in IE6 and Firefox Privacy. and then re test it.

appy77a at 2007-7-8 8:41:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...