Solved

session.isNew returns always false?

Posted on 2004-08-31
14
502 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 103
Cipher Configuration on Apache HTTPD 4 62
servlet requestdispatcher include and forward differences 1 30
egit plugin on eclipse 8 62
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

776 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