You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by go...@netscape.net on 2001/04/24 10:36:56 UTC

Problem with session tracking

Hi there,

I'm having trouble with session tracking in a servlet in that the session always appears to be new. When exercising the following code fragment:

HttpSession session = request.getSession();
if (session.isNew())
{
      // Some stuff here
}

the isNew() call always evaluates to true. I also have noticed that no cookies are being written since if I try:

Cookie[] cookies = request.getCookies();
System.out.print("Number of cookies is ");
System.out.println(cookies.length);

it is always zero. However, if I write cookies myself by calling response.addCookie(), I get a cookie next time round.

I'm finding this confusing since my understanding is that the session tracking is by default implemented using cookies. I'm guessing that the lack of cookies and the always-new-session are not unconnected but I've failed so far to find a solution or a reference to this problem in the Tomcat FAQ.

btw, I do have cookies turned on in my browser and the "noCookies" parameter in server.xml is set to false.

I would appreciate any ideas you might have.

Regards,
Alan Goulding.
__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/

Re: Problem with session tracking

Posted by go...@netscape.net.
I managed to get it working. Basically, what was happening is that Tomcat didn't think that the servlet and the JSP page were part of the same application. I believe that what happens is that you get different sessions for different apps running under Tomcat. The problem was that I'd misconfigured the app in the web.xml file. I redid it like the following extract:

<web-app>

    <display-name>Hello, World Application</display-name>
    <description>
        Simple test servlet.
    </description>

    <servlet>
        <servlet-name>InterestToJsp</servlet-name>
        <servlet-class>com.cwb.interest.InterestToJsp</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>InterestToJsp</servlet-name>
        <url-pattern>/InterestToJsp</url-pattern>
    </servlet-mapping>

</web-app>

This seemed to fix the problem, though why my previous misconfiguration caused it to think my servlet was part of a different app I'm not quite sure.

Hope this helps. If you need any more detail, get back to me, though I'm not going to be around for long today...

Cheers,
Alan.


tomcat-user@jakarta.apache.org wrote:
>
> I´m having the same problem.
> I use a servlet to control my application and Jsp´s just to apresentation..
> 
> How did you resolve that?
> 
> []´s
> 
> Daniel A.
> 
> gouldina@netscape.net wrote:
> 
> > My understanding is that sendRedirect() goes back to the client to do the redirection rather than doing it on the server which is obviously more efficient.
> > Have made some more progress and can now get a redirect to a servlet in the same package and keep the same session so my guess is now that it's something to do with the way that Tomcat thinks the web apps are partitioned - i.e. I don't think it thinks that it can/should reuse the same session for the jsp page I was redirecting it to for some reason.
> >
> > tomcat-user@jakarta.apache.org wrote:
> > >
> > > Hmm.....
> > > I guess its because your forwarding the context wholesale. I'd guess the
> > > sesion is new within that given context, and because your effectively
> > > recreating the context when you forward in that manner, it still counts as
> > > new. I use sendRedirect  instead and this works fine - what do you loose my
> > > using sendRedirect rather than forwarding?
> > >
> > > sam
> > > ----- Original Message -----
> > > From: <go...@netscape.net>
> > > To: <to...@jakarta.apache.org>
> > > Sent: Tuesday, April 24, 2001 11:21 AM
> > > Subject: Re: Problem with session tracking
> > >
> > >
> > > > Sam wrote:
> > > >
> > > > >
> > > > > There is a difference between the way HttpSessions arew created and
> > > handled,
> > > > > and the Cookie objetc. The cookie object creates a persistant cookie on
> > > the
> > > > > clients machine. The HttpSession is just a memory cookie, and as such is
> > > > > non-persistant and doesn't sit on disk.
> > > >
> > > > Interesting. Would you expect to see the session cookie when you call the
> > > getCookies() method then or not?
> > > >
> > > > Since my last mail, I've made some progress and can get isNew() to return
> > > false if I comment out the last few lines of the servlet method which
> > > forward to a jsp page:
> > > >
> > > > ServletContext servletContext = getServletConfig().getServletContext();
> > > > RequestDispatcher requestDispatcher =
> > > servletContext.getRequestDispatcher("/myapp/jsp/hello.jsp");
> > > > requestDispatcher.forward(request, response);
> > > >
> > > > Why would this screw up my session?
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > __________________________________________________________________
> > > > Get your own FREE, personal Netscape Webmail account today at
> > > http://webmail.netscape.com/
> > > >
> > >
> > >
> > __________________________________________________________________
> > Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/
> 
> 
__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/

Re: Problem with session tracking

Posted by "Daniel A. Melo" <da...@codata.com.br>.
How can i use sendRedirect?

can you show some source example?



[]´s

Daniel A.



"Daniel A. Melo" wrote:

> I´m having the same problem.
> I use a servlet to control my application and Jsp´s just to apresentation..
>
> How did you resolve that?
>
> []´s
>
> Daniel A.
>
> gouldina@netscape.net wrote:
>
> > My understanding is that sendRedirect() goes back to the client to do the redirection rather than doing it on the server which is obviously more efficient.
> > Have made some more progress and can now get a redirect to a servlet in the same package and keep the same session so my guess is now that it's something to do with the way that Tomcat thinks the web apps are partitioned - i.e. I don't think it thinks that it can/should reuse the same session for the jsp page I was redirecting it to for some reason.
> >
> > tomcat-user@jakarta.apache.org wrote:
> > >
> > > Hmm.....
> > > I guess its because your forwarding the context wholesale. I'd guess the
> > > sesion is new within that given context, and because your effectively
> > > recreating the context when you forward in that manner, it still counts as
> > > new. I use sendRedirect  instead and this works fine - what do you loose my
> > > using sendRedirect rather than forwarding?
> > >
> > > sam
> > > ----- Original Message -----
> > > From: <go...@netscape.net>
> > > To: <to...@jakarta.apache.org>
> > > Sent: Tuesday, April 24, 2001 11:21 AM
> > > Subject: Re: Problem with session tracking
> > >
> > >
> > > > Sam wrote:
> > > >
> > > > >
> > > > > There is a difference between the way HttpSessions arew created and
> > > handled,
> > > > > and the Cookie objetc. The cookie object creates a persistant cookie on
> > > the
> > > > > clients machine. The HttpSession is just a memory cookie, and as such is
> > > > > non-persistant and doesn't sit on disk.
> > > >
> > > > Interesting. Would you expect to see the session cookie when you call the
> > > getCookies() method then or not?
> > > >
> > > > Since my last mail, I've made some progress and can get isNew() to return
> > > false if I comment out the last few lines of the servlet method which
> > > forward to a jsp page:
> > > >
> > > > ServletContext servletContext = getServletConfig().getServletContext();
> > > > RequestDispatcher requestDispatcher =
> > > servletContext.getRequestDispatcher("/myapp/jsp/hello.jsp");
> > > > requestDispatcher.forward(request, response);
> > > >
> > > > Why would this screw up my session?
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > __________________________________________________________________
> > > > Get your own FREE, personal Netscape Webmail account today at
> > > http://webmail.netscape.com/
> > > >
> > >
> > >
> > __________________________________________________________________
> > Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/


Re: Problem with session tracking

Posted by "Daniel A. Melo" <da...@codata.com.br>.
I´m having the same problem.
I use a servlet to control my application and Jsp´s just to apresentation..

How did you resolve that?

[]´s

Daniel A.

gouldina@netscape.net wrote:

> My understanding is that sendRedirect() goes back to the client to do the redirection rather than doing it on the server which is obviously more efficient.
> Have made some more progress and can now get a redirect to a servlet in the same package and keep the same session so my guess is now that it's something to do with the way that Tomcat thinks the web apps are partitioned - i.e. I don't think it thinks that it can/should reuse the same session for the jsp page I was redirecting it to for some reason.
>
> tomcat-user@jakarta.apache.org wrote:
> >
> > Hmm.....
> > I guess its because your forwarding the context wholesale. I'd guess the
> > sesion is new within that given context, and because your effectively
> > recreating the context when you forward in that manner, it still counts as
> > new. I use sendRedirect  instead and this works fine - what do you loose my
> > using sendRedirect rather than forwarding?
> >
> > sam
> > ----- Original Message -----
> > From: <go...@netscape.net>
> > To: <to...@jakarta.apache.org>
> > Sent: Tuesday, April 24, 2001 11:21 AM
> > Subject: Re: Problem with session tracking
> >
> >
> > > Sam wrote:
> > >
> > > >
> > > > There is a difference between the way HttpSessions arew created and
> > handled,
> > > > and the Cookie objetc. The cookie object creates a persistant cookie on
> > the
> > > > clients machine. The HttpSession is just a memory cookie, and as such is
> > > > non-persistant and doesn't sit on disk.
> > >
> > > Interesting. Would you expect to see the session cookie when you call the
> > getCookies() method then or not?
> > >
> > > Since my last mail, I've made some progress and can get isNew() to return
> > false if I comment out the last few lines of the servlet method which
> > forward to a jsp page:
> > >
> > > ServletContext servletContext = getServletConfig().getServletContext();
> > > RequestDispatcher requestDispatcher =
> > servletContext.getRequestDispatcher("/myapp/jsp/hello.jsp");
> > > requestDispatcher.forward(request, response);
> > >
> > > Why would this screw up my session?
> > >
> > >
> > >
> > >
> > >
> > > __________________________________________________________________
> > > Get your own FREE, personal Netscape Webmail account today at
> > http://webmail.netscape.com/
> > >
> >
> >
> __________________________________________________________________
> Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/


Re: Problem with session tracking

Posted by Mark Howell <ma...@nullcraft.org>.
Some background:
I have a dispatcher servlet that processes request parameters, processes
appropriate business logic, populates a 'model' with data, then forwards
control to a jsp page to display the results.  Typical MVC setup.

I am using Tomcat 4.0 beta 3, and for some reason,
RequestDispatcher.forward(..) or .include(..) doesn't compile the jsp page
being forwarded to.  As a result, I had to use a response.sendRedirect(..)
to forward control to a jsp page so that it would compile.  I guess I
could pre-compile the jsp pages, but I chose not to.

What you lose by using this technique, is any request-scoped attributes or
parameters sent in the original request.  If you had any POST data sent to
the original dispatcher servlet that needs to be communicated further down
the chain, it must be placed in the session scope.  My temporary solution
to GET requests is to append the original query args to the URL that gets
redirected to.  This is somewhat of a kludge, but it works for now.

Good luck,
-Mark Howell
mark at nullcraft.org



 On Tue, 24 Apr 2001 gouldina@netscape.net wrote:

> My understanding is that sendRedirect() goes back to the client to do the redirection rather than doing it on the server which is obviously more efficient.
> Have made some more progress and can now get a redirect to a servlet in the same package and keep the same session so my guess is now that it's something to do with the way that Tomcat thinks the web apps are partitioned - i.e. I don't think it thinks that it can/should reuse the same session for the jsp page I was redirecting it to for some reason. 
> 
> 
> tomcat-user@jakarta.apache.org wrote:
> >
> > Hmm.....
> > I guess its because your forwarding the context wholesale. I'd guess the
> > sesion is new within that given context, and because your effectively
> > recreating the context when you forward in that manner, it still counts as
> > new. I use sendRedirect �instead and this works fine - what do you loose my
> > using sendRedirect rather than forwarding?
> > 
> > sam
> > ----- Original Message -----
> > From: <go...@netscape.net>
> > To: <to...@jakarta.apache.org>
> > Sent: Tuesday, April 24, 2001 11:21 AM
> > Subject: Re: Problem with session tracking
> > 
> > 
> > > Sam wrote:
> > >
> > > >
> > > > There is a difference between the way HttpSessions arew created and
> > handled,
> > > > and the Cookie objetc. The cookie object creates a persistant cookie on
> > the
> > > > clients machine. The HttpSession is just a memory cookie, and as such is
> > > > non-persistant and doesn't sit on disk.
> > >
> > > Interesting. Would you expect to see the session cookie when you call the
> > getCookies() method then or not?
> > >
> > > Since my last mail, I've made some progress and can get isNew() to return
> > false if I comment out the last few lines of the servlet method which
> > forward to a jsp page:
> > >
> > > ServletContext servletContext = getServletConfig().getServletContext();
> > > RequestDispatcher requestDispatcher =
> > servletContext.getRequestDispatcher("/myapp/jsp/hello.jsp");
> > > requestDispatcher.forward(request, response);
> > >
> > > Why would this screw up my session?
> > >
> > >
> > >
> > >
> > >
> > > __________________________________________________________________
> > > Get your own FREE, personal Netscape Webmail account today at
> > http://webmail.netscape.com/
> > >
> > 
> > 
> __________________________________________________________________
> Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/
> 


Re: Problem with session tracking

Posted by go...@netscape.net.
My understanding is that sendRedirect() goes back to the client to do the redirection rather than doing it on the server which is obviously more efficient.
Have made some more progress and can now get a redirect to a servlet in the same package and keep the same session so my guess is now that it's something to do with the way that Tomcat thinks the web apps are partitioned - i.e. I don't think it thinks that it can/should reuse the same session for the jsp page I was redirecting it to for some reason. 


tomcat-user@jakarta.apache.org wrote:
>
> Hmm.....
> I guess its because your forwarding the context wholesale. I'd guess the
> sesion is new within that given context, and because your effectively
> recreating the context when you forward in that manner, it still counts as
> new. I use sendRedirect  instead and this works fine - what do you loose my
> using sendRedirect rather than forwarding?
> 
> sam
> ----- Original Message -----
> From: <go...@netscape.net>
> To: <to...@jakarta.apache.org>
> Sent: Tuesday, April 24, 2001 11:21 AM
> Subject: Re: Problem with session tracking
> 
> 
> > Sam wrote:
> >
> > >
> > > There is a difference between the way HttpSessions arew created and
> handled,
> > > and the Cookie objetc. The cookie object creates a persistant cookie on
> the
> > > clients machine. The HttpSession is just a memory cookie, and as such is
> > > non-persistant and doesn't sit on disk.
> >
> > Interesting. Would you expect to see the session cookie when you call the
> getCookies() method then or not?
> >
> > Since my last mail, I've made some progress and can get isNew() to return
> false if I comment out the last few lines of the servlet method which
> forward to a jsp page:
> >
> > ServletContext servletContext = getServletConfig().getServletContext();
> > RequestDispatcher requestDispatcher =
> servletContext.getRequestDispatcher("/myapp/jsp/hello.jsp");
> > requestDispatcher.forward(request, response);
> >
> > Why would this screw up my session?
> >
> >
> >
> >
> >
> > __________________________________________________________________
> > Get your own FREE, personal Netscape Webmail account today at
> http://webmail.netscape.com/
> >
> 
> 
__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/

Re: Problem with session tracking

Posted by Sam Newman <sa...@www.stamplets.com>.
Hmm.....
I guess its because your forwarding the context wholesale. I'd guess the
sesion is new within that given context, and because your effectively
recreating the context when you forward in that manner, it still counts as
new. I use sendRedirect  instead and this works fine - what do you loose my
using sendRedirect rather than forwarding?

sam
----- Original Message -----
From: <go...@netscape.net>
To: <to...@jakarta.apache.org>
Sent: Tuesday, April 24, 2001 11:21 AM
Subject: Re: Problem with session tracking


> Sam wrote:
>
> >
> > There is a difference between the way HttpSessions arew created and
handled,
> > and the Cookie objetc. The cookie object creates a persistant cookie on
the
> > clients machine. The HttpSession is just a memory cookie, and as such is
> > non-persistant and doesn't sit on disk.
>
> Interesting. Would you expect to see the session cookie when you call the
getCookies() method then or not?
>
> Since my last mail, I've made some progress and can get isNew() to return
false if I comment out the last few lines of the servlet method which
forward to a jsp page:
>
> ServletContext servletContext = getServletConfig().getServletContext();
> RequestDispatcher requestDispatcher =
servletContext.getRequestDispatcher("/myapp/jsp/hello.jsp");
> requestDispatcher.forward(request, response);
>
> Why would this screw up my session?
>
>
>
>
>
> __________________________________________________________________
> Get your own FREE, personal Netscape Webmail account today at
http://webmail.netscape.com/
>


Re: Problem with session tracking

Posted by go...@netscape.net.
Sam wrote:

>
> There is a difference between the way HttpSessions arew created and handled,
> and the Cookie objetc. The cookie object creates a persistant cookie on the
> clients machine. The HttpSession is just a memory cookie, and as such is
> non-persistant and doesn't sit on disk. 

Interesting. Would you expect to see the session cookie when you call the getCookies() method then or not?

Since my last mail, I've made some progress and can get isNew() to return false if I comment out the last few lines of the servlet method which forward to a jsp page:

ServletContext servletContext = getServletConfig().getServletContext();
RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/myapp/jsp/hello.jsp");
requestDispatcher.forward(request, response);

Why would this screw up my session?





__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/

Re: Problem with session tracking

Posted by Sam Newman <sa...@www.stamplets.com>.
There is a difference between the way HttpSessions arew created and handled,
and the Cookie objetc. The cookie object creates a persistant cookie on the
clients machine. The HttpSession is just a memory cookie, and as such is
non-persistant and doesn't sit on disk. Looking at your code,
request.getSession() will return either the current session, or will create
a new one if none exists. I would expect that your servlet is the first one
to create the session. Try accessing another servlet after this one - have
that servlet get the session and see if its new. You could just put a link
to itself on the servlets page. The example servlets include a session
example which does just this.  Basically, there are several ways of tracking
sesions, and to make matters really complicated you can use allot of them at
once. It doesn't actually mean they share the data though :-)
It depends on circumstances and personal tastes as to which one you use.

sam
----- Original Message -----
From: <go...@netscape.net>
To: <to...@jakarta.apache.org>
Sent: Tuesday, April 24, 2001 9:36 AM
Subject: Problem with session tracking


> Hi there,
>
> I'm having trouble with session tracking in a servlet in that the session
always appears to be new. When exercising the following code fragment:
>
> HttpSession session = request.getSession();
> if (session.isNew())
> {
>       // Some stuff here
> }
>
> the isNew() call always evaluates to true. I also have noticed that no
cookies are being written since if I try:
>
> Cookie[] cookies = request.getCookies();
> System.out.print("Number of cookies is ");
> System.out.println(cookies.length);
>
> it is always zero. However, if I write cookies myself by calling
response.addCookie(), I get a cookie next time round.
>
> I'm finding this confusing since my understanding is that the session
tracking is by default implemented using cookies. I'm guessing that the lack
of cookies and the always-new-session are not unconnected but I've failed so
far to find a solution or a reference to this problem in the Tomcat FAQ.
>
> btw, I do have cookies turned on in my browser and the "noCookies"
parameter in server.xml is set to false.
>
> I would appreciate any ideas you might have.
>
> Regards,
> Alan Goulding.
> __________________________________________________________________
> Get your own FREE, personal Netscape Webmail account today at
http://webmail.netscape.com/
>


Spammer

Posted by Tim O'Neil <ti...@xythos.com>.
At 10:45 AM 4/24/2001 +0200, you wrote:
>Undeliverable mail.

Morten Ronseth whatever, consider yourself
kilfiled and your undeliverable mail replies
returned to you. Thank god I control the mail
server here.


Re: Problem with session tracking

Posted by Morten Rønseth <Mo...@coil.no>.
Undeliverable mail.


Re: Problem with session tracking

Posted by Morten Rønseth <Mo...@coil.no>.
Undeliverable mail.


Re: Problem with session tracking

Posted by Morten Rønseth <Mo...@coil.no>.
Undeliverable mail.