• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a counter in my page?

 
Eric Wang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, All:
I need a counter to keep track how many people browse this page, the counter will save in a text file, and this counter can be read from the text file and increase it next time. Any suggest how to read it from a file and update the counter in the text file.
Thank you very much
Hongwei
 
Sandeep Jain
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,
I am not sure whther u were asking for the same. But what I could undestand is, u were having some problem with reading and writing the data from and to the file ,. U can put the followinf code in a bean and can dyanmically generate the counter value.
The code is as followsimport java.io.*;
class counter
{
public counter() throws IOException
{
try
{
DataInputStream dis=new DataInputStream(new FileInputStream("counter.txt"));
int x=dis.readInt();
x++;
dis.close();
DataOutputStream dos=new DataOutputStream(new FileOutputStream("counter.txt"));
dos.writeInt(x);
dos.close();
System.out.println(x);
}
catch(FileNotFoundException ie)
{
try
{
DataOutputStream dos=new DataOutputStream(new FileOutputStream("counter.txt"));
dos.writeInt(1);
System.out.println("1");
dos.close();
}
catch(Exception ert)
{
System.out.println(ert);
}
}
}
public static void main(String[] args) throws Exception
{
new counter();
}
}

------------------
Sandeep Jain
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would concider looking for a free already made counter. I get a free counter at angelfire, and I think if I wanted I could use it on my other site also.
Of course if you want to do it as a project my answer is offbase.
[This message has been edited by Randall Twede (edited February 19, 2001).]
 
Ajan Balakrishnan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some websites which offers this feature. No extra programming required and for a price nobody can beat, FReeeee
For more see the post http://www.javaranch.com/ubb/Forum7/HTML/001929.html

Ajan
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HAHA jinx ajan, we posted the same answer at the same minute.
 
Ajan Balakrishnan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool coincedence huh..
Ajan
 
Eric Wang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Thank you for all the replies. Thanks a lot.
Eric
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
declare a variable x=0
increment this x anywhere in your jsp or servlets (x++)
every time a user logs in this variable will increment itself and display the previous hits to this page ...if still have a prob mail it to me at rax_india@yahoo.com
 
Ajan Balakrishnan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One question Raghav on the same subject . What you are doing in case if your server goes down and comes up again???. Won't the x variable gets reset and shows "0 hits" when the server comes back up. How do you retrieve the last value of x before the server went down ? Do you store the x variable in
1. a database
2. a file
3. reading from the log file???
4. Any other solution ???

Thanks
Ajan
 
Raghavendra Holla
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Sandeep's answer seems to be o.k. But make sure that while updating the counter amke the method as synchronised. Otherwise integrity of value can not be guranteed.
regards,
holla.
 
Anjali Ajwani
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,
I hope I'm not late for the solution you're looking for. See if the following code helps you. w.r.t. Ajan's question, I think this answers them all.
/*
Servlet showing persistence across loads - a counter that doesn't have to start over whenthe server is shut down or the servlet is reloaded. The counter keeps count of the number of times the site has been visited.
*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InitCounter extends HttpServlet {
int count;
public void init(ServletConfig config) throws servletException {
super.init(config);
//Try to load the initial count from the saved persistence state
//this fails the first time the servlet runs because the file doesn't exist
try{
FileReader fileReader = new FileReader("InitCounter.initial");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String initial = bufferedReader.readLine();
count = Integer.parseInt(initial);
return;
}
catch (FileNotFoundException e) { }// no saved state
catch (IOException e) { } // problem during read
catch (NumberFormatException e) { }// corrupt saved state
//If no luck with the saved state(first time), check for an init parameter
//specifying the starting count.
String initial = getInitParameter("initial");
try {
count = Integer.parseInt(initial);
return;
}
catch (NumberFormatException e) { }
//default initial count of "0" --- if the above two fail
count = 0;
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
//code multithreaded access
int local_count;
synchronized(this) {
local_count = ++count;
if (count % 10 == 0) saveState();
}
out.println("Since loading, this servlet has been accessed " +
local_count + " times.");
}
public void destroy() { //destory method is called just once per servlet instance
saveState();
}
public void saveState() {
//save the accumulated count
try {
FileWriter fileWriter = new FileWriter("InitCounter.initial");
String initial = Integer.toString(count);
fileWriter.write(initial, 0, initial.length());
fileWriter.close();
return;
}
catch (IOException e) { } //problem during write
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic