You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jason Novotny <JD...@lbl.gov> on 2001/04/18 23:19:07 UTC

getSession(false)

Hi,

    I'm using the following snippet in my main servlet:

public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

    HttpSession session = req.getSession(false);
     if (session == null) {
          System.out.println("session is null");
     } else {
          System.out.println("session is not null");
     }

}

what I find is that when I go to the servlet, I first see "session is
null". Fine by me, and since I passed "false" to getSession, I expect
not to have one created. However, when I reload the page, now I get
"session is not null". can anyone please explain why getSession(false)
actually creates a session, or what I might be doing wrong.

    Thanks, Jason

--
Jason Novotny               novotny@george.lbl.gov
Home: (510) 549-0574        Work: (510) 486-8662
NERSC Distributed Computing http://www-didc.lbl.gov




Re: getSession(false)

Posted by Bo Wang <bo...@home.com>.
I've got a different problem.

I have a method called getHttpSession(request, response), as below:
HttpSession getHttpSession(HttpRequest request, HttpResponse response)
{
    HttpSession session = req.getSession(false);
    if ( session==null)
    {
        System.out.println("session = null";
    }
    else
    {
        .....................
    }
    return session;
}

which returns an HttpSesion, this method is in BaseServlet, then I create a
new servelt called Login which extend from BaseServlet, this method was
invoked in Login servlet.

When I run the Login servlet, in IE, sometimes I get session=null, and I
also get some error message said "the instruction in address xxxxxxxxxx is
not correct", or something like that, sometimes I can get correct session,
but in Netscape, I never get session=null.

Did you try your servlet both on IE and Netscape? If so, do they come with
different result?

Becky

----- Original Message -----
From: "Jason Novotny" <JD...@lbl.gov>
To: "Stuart Thomson" <st...@swtsoft.demon.co.uk>
Cc: <to...@jakarta.apache.org>
Sent: Thursday, April 26, 2001 1:51 PM
Subject: Re: getSession(false)


>
>     No- but my lame workaround is to stick something in the session and
> test to see if it's null in which case I explicitly set session to null.
>
> e.g.
>
>  HttpSession session = req.getSession(false);
>
>  // Unless a user profile exists, the session is null
>  if (session != null) {
>    Object user  = session.getAttribute("user");
>    if (user == null)
>        session = null;
>  }
>
>
>     Jason
>
> Stuart Thomson wrote:
>
> > Did you ever find out what was causing this? I seem to be hitting the
> > same problem.
> >
> > Jason Novotny wrote:
> > >
> > > Hi,
> > >
> > >     I'm using the following snippet in my main servlet:
> > >
> > > public void service(HttpServletRequest req, HttpServletResponse res)
> > > throws ServletException, IOException {
> > >
> > >     HttpSession session = req.getSession(false);
> > >      if (session == null) {
> > >           System.out.println("session is null");
> > >      } else {
> > >           System.out.println("session is not null");
> > >      }
> > >
> > > }
> > >
> > > what I find is that when I go to the servlet, I first see "session is
> > > null". Fine by me, and since I passed "false" to getSession, I expect
> > > not to have one created. However, when I reload the page, now I get
> > > "session is not null". can anyone please explain why getSession(false)
> > > actually creates a session, or what I might be doing wrong.
> > >
> > >     Thanks, Jason
> > >
> > > --
> > > Jason Novotny               novotny@george.lbl.gov
> > > Home: (510) 549-0574        Work: (510) 486-8662
> > > NERSC Distributed Computing http://www-didc.lbl.gov
>
> --
> Jason Novotny               novotny@george.lbl.gov
> Home: (510) 610-8360        Work: (510) 486-8662
> NERSC Distributed Computing http://www-didc.lbl.gov
>
>
>
>


Re: setAttribute/getAttribute problem

Posted by Aaron Lindsey <aa...@redneptune.com>.
In response to Bill Kaufman's suggestion here is the output from his code
snippet:

Request object: class=class PopBean, loader=AdaptiveClassLoader( )
PopBean object: class=class PopBean, loader=AdaptiveClassLoader( )
Myself:         class=class sla._0002fsla_0002ftest_0002ejsptest_jsp_9, 
loader=JspLoader( /usr/local/tomcat/work/localhost_8080 ) / 
AdaptiveClassLoader( )

Also, I'm trying to remember everyone's questions.  I am using forward and
not sendRedirect.  I tried using the session to pass this rather than the
request with the same results.  I do have the correct import statement
because I can instantiate PopBean objects in the jsp just fine.

Hmmm.  Just when I think I know everything...

Aaron



Re: getSession(false)

Posted by Jason Novotny <JD...@lbl.gov>.
    No- but my lame workaround is to stick something in the session and
test to see if it's null in which case I explicitly set session to null.

e.g.

 HttpSession session = req.getSession(false);

 // Unless a user profile exists, the session is null
 if (session != null) {
   Object user  = session.getAttribute("user");
   if (user == null)
       session = null;
 }


    Jason

Stuart Thomson wrote:

> Did you ever find out what was causing this? I seem to be hitting the
> same problem.
>
> Jason Novotny wrote:
> >
> > Hi,
> >
> >     I'm using the following snippet in my main servlet:
> >
> > public void service(HttpServletRequest req, HttpServletResponse res)
> > throws ServletException, IOException {
> >
> >     HttpSession session = req.getSession(false);
> >      if (session == null) {
> >           System.out.println("session is null");
> >      } else {
> >           System.out.println("session is not null");
> >      }
> >
> > }
> >
> > what I find is that when I go to the servlet, I first see "session is
> > null". Fine by me, and since I passed "false" to getSession, I expect
> > not to have one created. However, when I reload the page, now I get
> > "session is not null". can anyone please explain why getSession(false)
> > actually creates a session, or what I might be doing wrong.
> >
> >     Thanks, Jason
> >
> > --
> > Jason Novotny               novotny@george.lbl.gov
> > Home: (510) 549-0574        Work: (510) 486-8662
> > NERSC Distributed Computing http://www-didc.lbl.gov

--
Jason Novotny               novotny@george.lbl.gov
Home: (510) 610-8360        Work: (510) 486-8662
NERSC Distributed Computing http://www-didc.lbl.gov




Re: getSession(false)

Posted by Stuart Thomson <st...@swtsoft.demon.co.uk>.
Did you ever find out what was causing this? I seem to be hitting the
same problem.

Jason Novotny wrote:
> 
> Hi,
> 
>     I'm using the following snippet in my main servlet:
> 
> public void service(HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException {
> 
>     HttpSession session = req.getSession(false);
>      if (session == null) {
>           System.out.println("session is null");
>      } else {
>           System.out.println("session is not null");
>      }
> 
> }
> 
> what I find is that when I go to the servlet, I first see "session is
> null". Fine by me, and since I passed "false" to getSession, I expect
> not to have one created. However, when I reload the page, now I get
> "session is not null". can anyone please explain why getSession(false)
> actually creates a session, or what I might be doing wrong.
> 
>     Thanks, Jason
> 
> --
> Jason Novotny               novotny@george.lbl.gov
> Home: (510) 549-0574        Work: (510) 486-8662
> NERSC Distributed Computing http://www-didc.lbl.gov