In this tutorial you will learn how to count the hit of the web sight visited by the user
Page Visit Count Example
An Example Of page visit is given below. This example counts the page visit. If you are visiting fist time then it will display a message Welcome to the fist Time if you are visited more than one the the message display you have visited "no of time" page.
CountPageVisit.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CountPageVisit extends HttpServlet { static int count = 0; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out = response.getWriter(); response.setContentType("text/html"); if (count == 0) { out.print("<body bgcolor=pink>"); out.print("<center>"); out.print("<h1>"); out.print("Welcome to the fist Time"); count++; } else { out.print("<center>"); out.print("<h1>You have visited " + count + " times"); count++; out.print("</h1>"); out.print("</center>"); out.print("</body>"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } }
When you run this application it will display message as shown below:
At First Time...........
After Fist Time...............
[ 0 ] Comments