Solved

session.isNew returns always false?

Posted on 2004-08-31
14
459 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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
hehehe...  girionis, you're quick today! ;-)

Gah!  And 45K ahead!

;-)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
>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
Comment Utility
> 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
Comment Utility
Hehe, we take turns it seems :)
0
 

Author Comment

by:mali_djuro
Comment Utility
i tried, but still is same situation. it return false.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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:mayankeagle
Comment Utility
>> session = req.getSession(true);

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

Expert Comment

by:mayankeagle
Comment Utility
req.getSession ( true ) always creates a new session.
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
>  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
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
>> 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now