?
Solved

session.isNew returns always false?

Posted on 2004-08-31
14
Medium Priority
?
589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

Industry Leaders: 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!

Question has a verified solution.

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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

765 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