• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sessions Doubt??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I was trying out some examples on sessions. Here goes my code as below,

My welcome file,

<form action="SelectServlet.do" method="GET">
<input type = "text" name="Username">
<input type="submit" value="Submit">
</form>

On hitting submit, it takes me to SelectServlet.do where I have the following code in my doGet method...

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("Testing sessions for " + request.getParameter("Username"));
HttpSession session = request.getSession();
if(session.isNew()){
out.println("This is a new session");
}else{
out.println("Welcome back");
}

When I ran it, it always gives me Welcome back..??? Even for the first request, it gives me Welcome back...How is this possible?? I'm confused! Please anyone help me on this.

Thanks in advance!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jyothi,

You are using
HttpSession session = request.getSession();
which always returns an already existing session or creates a new session and gives you.Hence session.isNew() is always false.

if you use like this
HttpSession session = request.getSession(false);
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Please read the following Servlet Documentation

http://www.cs.arizona.edu/~collberg/Research/Java-JSDK/docs/api/javax/servlet/http/HttpSession.html

isNew
public boolean isNew()Returns true if the Web server has created a session but the client has not yet joined. For example, if the server used only cookie-based sessions, and the client had disabled the use of cookies, then a session would be new.

Returns:
true if the server has created a session, but the client has not yet joined

Throws:
IllegalStateException - if the session is invalid
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jothi,

what is your welcome file extension? Is it html or jsp?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My welcome file is a html?? But does that really matters??
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The browser might have disabled cookie. you can check whether jsessionid is really present in the cookie header of the response to the client.
 
reply
    Bookmark Topic Watch Topic
  • New Topic