• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sessions Doubt??

 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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!
 
Swetha S
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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);
 
Roger Stratus
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Elan Ram
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi,

what is your welcome file extension? Is it html or jsp?
 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My welcome file is a html?? But does that really matters??
 
Bob CHOI
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic