Solved

session.isNew returns always false?

Posted on 2004-08-31
14
492 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 500 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse compiler vs Installed JREs option 3 76
servlet doXXX methods 3 36
Oracle SQL syntax check  without executing 6 34
varialbe initialization 11 30
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

910 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

24 Experts available now in Live!

Get 1:1 Help Now