?
Solved

session.isNew returns always false?

Posted on 2004-08-31
14
Medium Priority
?
658 Views
Last Modified: 2011-09-20
Hi, all

i work in WSAD 5.
i need to check is session new or not. because i keep data in it. and if it is new i need to fill it with data. in other case i get data from session.

i tried

if (session.isNew()){
  System.out.println("new session");
 ...
}
else{
  System.out.println("not new session");
...
}

but it always output is
not new session

then i tried just to check does this method works fine with

session.invalidate();
session = req.getSession(true);
if (session.isNew()){
  System.out.println("new session");
 ...
}
else{
  System.out.println("not new session");
...
}

again is same output

after all i tried with this:

while(!session.isNew()){
   session.invalidate();
   session = req.getSession(true);
}

and i got infintive loop.

all these examples works properly in VisaulAge for Java 4,
but not in WSAD 5 .

do you have any idea where is a catch, and why it deosn't work?

thanks in advance
0
Comment
Question by:mali_djuro
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11941235
Try setting the session to null before you obtain it again and tell us if it helps:

session = null;
session = req.getSession(true);
if (session.isNew()){
  System.out.println("new session");
 ...
}
else{
  System.out.println("not new session");
...
}
0
 
LVL 35

Accepted Solution

by:
TimYates earned 1500 total points
ID: 11941262
It depends on the implementation of the server...

Basically, if the client machine has allocated a cookie for this session, then "isNew" will return false...

You are better putting a variable into the session, so you can see if it is null...

if( session.getAttribute( "old" ) == null )
{
    // new session
}
session.setAttribute( "old", "yes" ) ;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11941274
hehehe...  girionis, you're quick today! ;-)

Gah!  And 45K ahead!

;-)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 11941306
>hehehe...  girionis, you're quick today! ;-)
>
>Gah!  And 45K ahead!

Hehe, that's because I do not have work and I am online all day :)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11941316
> Hehe, that's because I do not have work and I am online all day :)

Curse my job!! ;-)  Can't wait for you to be busy again ;-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11941330
Hehe, we take turns it seems :)
0
 

Author Comment

by:mali_djuro
ID: 11941399
i tried, but still is same situation. it return false.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11941448
Have you tried using the "flag" option I suggested?

It sounds like the WSAD 5 implementation of "isNew" is very loose (to the already vague specification)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11941509
>> session = req.getSession(true);

Try: session = req.getSession ( false ) != null ? req.getSession ( false ) : req.getSession ( true ) ;
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11941544
req.getSession ( true ) always creates a new session.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11941554
>  req.getSession ( true ) always creates a new session.

I thought it created a new session if one didn't exist already?

If one already exists, it behaves the same as getSession( false ) ;
0
 
LVL 35

Expert Comment

by:girionis
ID: 11941731
Yes that's what it's doing. It creates a new session only if an old one does not exist.

getSession(boolean create)
          Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.
0
 

Author Comment

by:mali_djuro
ID: 11941985
thanks

i need to do it very quick, so this way with attribute is fine for me right now.
when i would make some corrections in future i will consider about other options.

thanks a lot!!!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11943672
>> I thought it created a new session if one didn't exist already?

Yeah, you're right there. I got confused for a minute, myself :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question