Servlet Tutorial
A hit counter servlet can be used to track the number of times a web page has been accessed or "hit". Here's a simple example of how you can create a hit counter servlet.
Step 1: Creating a new dynamic web project
In Eclipse:
Step 2: Creating the Servlet
Step 3: Writing the Servlet code
In the created servlet, write the following code:
package com.example; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/HitCounterServlet") public class HitCounterServlet extends HttpServlet { private static final long serialVersionUID = 1L; private int hitCount; public void init() { // Reset hit counter. hitCount = 0; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // increase hit counter hitCount++; // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<h1>Hello, you are visitor number " + hitCount + "!</h1>"); } }
In this servlet, we have a class-level variable hitCount
that we increment every time the doGet
method is called. The init
method is called when the servlet is first loaded into the application server, and we use it to initialize hitCount
to zero. Then, every time a GET request is made to the servlet, we increment hitCount
and display the current count to the user.
Step 4: Running the Servlet
You should now be able to see the hit counter displayed in your web browser. If the server is running on your local machine, you can access your servlet at http://localhost:8080/HitCounterServlet/HitCounterServlet
(replace 8080 with your server's port number if it's different).
Note: This servlet's hit count will reset to 0 every time the servlet is reloaded or the application server is restarted. In a real-world application, you might want to store the hit count in a database or another persistent storage system to prevent this from happening.
Session-based hit counter in Servlet: Implement a hit counter using session attributes.
HttpSession session = request.getSession(true); Integer hitCount = (Integer) session.getAttribute("hitCount"); if (hitCount == null) { hitCount = 1; } else { hitCount++; } session.setAttribute("hitCount", hitCount);
Using Servlet context for hit tracking: Utilize Servlet context to store hit counters globally.
ServletContext context = getServletContext(); Integer hitCount = (Integer) context.getAttribute("hitCount");
Servlet hit counter example code: An example of a simple Servlet hit counter.
public class HitCounterServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Hit counter code } }
Servlet hit counter with cookies: Use cookies to store and retrieve hit counts.
Cookie[] cookies = request.getCookies();
Displaying hit count on the web page with Servlet: Show the hit count on the web page.
response.getWriter().println("Page Hits: " + hitCount);