Hi Asha,
Not quite sure why yours doesn't work. I use the counter from Jason's
Hunters book. It's very simple too. I use it as a SSI (server side include).
It will also store the count if you reboot your server. Here is the code.
Tom Kochanowicz
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SSICounter extends GenericCounter{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
PrintWriter out = res.getWriter();
// Fetch the page we're on.
String uri = req.getRequestURI();
// Get and increment the count for that page.
int count = incrementAndGetCount(uri);
// FulFill our purpose: print the count
out.println("<html><head><title>Counter</title></head>");
out.println("<body><h6>This page has been viewed " + count + "
times.</h6></body></html>");
out.close();
}
}
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GenericCounter extends HttpServlet{
private Hashtable counts = new Hashtable();
public void init(ServletConfig config) throws ServletException{
// Always call super.init(config) first.
super.init(config);
// Try to load the initial page counts from the saved persistent state.
try{
FileReader fileReader = new FileReader(getClass().getName() +
".counts");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
String uri = null;
String count = null;
int[] holder = null; // holder for the count, to make it an
object.
while ((line = bufferedReader.readLine()) != null){
StringTokenizer tokenizer = new StringTokenizer(line);
if(tokenizer.countTokens() < 2) continue; // bogus line
uri = tokenizer.nextToken();
count = tokenizer.nextToken();
// Store the uri/count pair in the counts hashtable.
// The count is saved as an int[1] to make it an
"object".
try{
holder = new int[1];
holder[0] = Integer.parseInt(count);
counts.put(uri, holder);
}
catch (NumberFormatException e) {} // bogus line
}
}
catch (FileNotFoundException e) {} // no saved state.
catch (IOException e) {} // no problem during read.
}
// Increment and return the count for the given URI
public int incrementAndGetCount(String uri){
int[] holder = (int[])counts.get(uri);
if(holder == null){
// Initialize the count to 0.
holder = new int[1];
// holder[0] = 0;
holder[0] = 999; // Start at 1000 to make site look busy.
Change back to
0 later?
counts.put(uri, holder); // save the holder.
}
holder[0]++; // increment
return holder[0];
}
public void distroy(){
// Try to save the accumulated count.
try{
FileWriter fileWriter = new FileWriter(getClass().getName() +
".counts");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
Enumeration keys = counts.keys();
Enumeration elements = counts.elements();
String output = null;
while (keys.hasMoreElements() && elements.hasMoreElements()){
bufferedWriter.write(keys.nextElement() + " " +
elements.nextElement() + "\n");
}
bufferedWriter.close();
fileWriter.close();
return;
}
catch (IOException e){} // problem during write.
}
}
-----Original Message-----
From: A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of Asha
Balasubramanyan
Sent: Monday, November 29, 1999 2:37 PM
To: [EMAIL PROTECTED]
Subject: Problem in Session Tracking:Beginner
Hi :
This is My simple seesion tracking program , count no of times the pages
is hited. It is giving alwys me count 1 even i refresh the page 10 times.
Please Help me!
Thanks
Asha
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionTracker extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws
ServletException ,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// Get the current session object, create one if necessary
HttpSession session = req.getSession(true);
out.println("<HTML><HEAD><TITLE>SessionTracker</TITLE></HEAD>");
out.println("<BODY><H1>Session Tracking Demo</H1>");
// Increment the hit count for this page. The value is saved
// in this client's session under the name "tracker.count".
Integer count = (Integer)session.getValue("SessionTracker.count");
out.println("after gettig ="+count+"<br>");
if (count == null)
{
out.println("inside null gettig ="+count+"<br>");
count = new Integer(1);
}
else
count = new Integer(count.intValue() + 1);
session.putValue("SessionTracker.count", count);
// Display the hit count for this page
out.println("You've visited this page " + count +
((count.intValue() == 1) ? " time." : " times."));
out.println("<P>");
out.println("<H2>Here is your session data:</H2>");
String[] names = session.getValueNames();
for (int i = 0; i < names.length; i++) {
out.println(names[i] + ": " + session.getValue(names[i]) + "<BR>");
}
out.println("</BODY></HTML>");
file://out.close <file://out.close> ();
}
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html