File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Sessions Doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Sessions Doubt??" Watch "Sessions Doubt??" New topic
Author

Sessions Doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9616
    
    2

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!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Swetha S
Greenhorn

Joined: Dec 06, 2006
Posts: 16
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

Joined: Jan 07, 2007
Posts: 3
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

Joined: Dec 14, 2006
Posts: 40
Hi Jothi,

what is your welcome file extension? Is it html or jsp?


Thanks and regards,
Elan
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9616
    
    2

My welcome file is a html?? But does that really matters??
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
The browser might have disabled cookie. you can check whether jsessionid is really present in the cookie header of the response to the client.


Hard work rewards
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sessions Doubt??