Please check the code why it is not working

I am a newjava servlets and jsp learner. I am running below example of session in Tomcat and "AccessCount" supposed to be increased by one value whenever servelet is refereshed. But I am always getting 0 value even after refereshing it.

I will appreciate your help.

=======================================================

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.net.*;

import java.util.*;

/** Simple example of session tracking. See the shopping

* cart example for a more detailed one.

*/

public class ShowSession extends HttpServlet {

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String title = "Session Tracking Example";

String heading;

HttpSession session = request.getSession(true);

// Use getAttribute instead of getValue in version 2.2.

Integer accessCount = (Integer)session.getAttribute("accessCount");

if (accessCount == null) {

accessCount = new Integer(0);

heading = "Welcome, Newcomer";

} else {

heading = "Welcome Back";

accessCount = new Integer(accessCount.intValue() + 1);

}

// Use setAttribute instead of putValue in version 2.2.

session.setAttribute("accessCount", accessCount);

out.println("<html><h1>" + title + "</h1>" +

"<BODY BGCOLOR=\"#FDF5E6\">\n" +

"<H1 ALIGN=\"CENTER\">" + heading + "</H1>\n" +

"<H2>Information on Your Session:</H2>\n" +

"<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +

"<TR BGCOLOR=\"#FFAD00\">\n" +

" <TH>Info Type<TH>Value\n" +

"<TR>\n" +

" <TD>ID\n" +

" <TD>" + session.getId() + "\n" +

"<TR>\n" +

" <TD>Creation Time\n" +

" <TD>" +

new Date(session.getCreationTime()) + "\n" +

"<TR>\n" +

" <TD>Time of Last Access\n" +

" <TD>" +

new Date(session.getLastAccessedTime()) + "\n" +

"<TR>\n" +

" <TD>Number of Previous Accesses\n" +

" <TD>" + accessCount + "\n" +

"</TABLE>\n" +

"</BODY></HTML>");

}

/** Handle GET and POST requests identically. */

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

========================================================

[2723 byte] By [Java_Sessiona] at [2007-10-2 5:55:50]
# 1

Change the code

HttpSession session = request.getSession(true);

AS

HttpSession session = request.getSession(false);

If you pass "false", then the Session is not created. If you pass "true" the session is always created new.

Thanks and regards,

Pazhanikanthan. P

pazhanikanthana at 2007-7-16 2:05:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

> Change the code

> HttpSession session =

> request.getSession(true);

>

> AS

>

> HttpSession session =

> request.getSession(false);

>

> If you pass "false", then the Session is not created.

> If you pass "true" the session is always created

> new.

>

> Thanks and regards,

> Pazhanikanthan. P

I dont think that's correct -

1. request.getSession(false) returns a Session object if one exists.

2. request.getSession(true) creates a new Session object if it doesnt exist or returns the reference to the existing object if the request is part of an valid session.

OP, check with default session-timeout values in web.xml

Ram.

Madathil_Prasada at 2007-7-16 2:05:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
> 1. request.getSession(false) returns a Session object if one exists. should have been1. request.getSession(false) returns a Session object only if a valid session exists. Else it returns nullRam.
Madathil_Prasada at 2007-7-16 2:05:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...