Introduction
In this project, you will learn how to build a simple IP visitor tracking application using Java Servlet and JSP. The application will keep track of the IP addresses of visitors accessing the website and the number of times each IP address has visited.
Preview

🎯 Tasks
In this project, you will learn:
- How to implement the
index.jspfile to display the IP addresses and their visit counts. - How to implement the
MyListenerclass to create and store theipVisitMapin theServletContext. - How to implement the
MyFilterclass to count the number of visits for each IP address and update theipVisitMap.
🏆 Achievements
After completing this project, you will be able to:
- Build a simple web application using Java Servlet and JSP.
- Use the
ServletContextto store and retrieve application-level data. - Use a
Filterto intercept and process incoming requests. - Use synchronization to ensure thread-safe access to shared resources.
Implement the index.jsp file
In this step, you will learn how to implement the index.jsp file to display the IP addresses and their visit counts.
Open the
index.jspfile located in the/home/labex/project/IpProject/src/main/webappdirectory.Add the following code to the
index.jspfile:
<%@ page import="java.util.Map" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>IP Visitor Tracker</title>
</head>
<body>
<h1>IP Visitor Tracker</h1>
<table border="1">
<tr>
<th>IP Address</th>
<th>Visit Count</th>
</tr>
<%
Map<String, Integer> ipVisitMap = (Map<String, Integer>) getServletContext().getAttribute("ipVisitMap");
if (ipVisitMap != null) {
for (String ip : ipVisitMap.keySet()) {
%>
<tr>
<td><%= ip %></td>
<td><%= ipVisitMap.get(ip) %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
This code retrieves the ipVisitMap from the ServletContext and displays the IP addresses and their visit counts in a table.
Implement the MyListener class
In this step, you will learn how to implement the MyListener class to create and store the ipVisitMap in the ServletContext.
Open the
MyListener.javafile located in the/home/labex/project/IpProject/src/main/java/org/labex/listenerdirectory.Add the following code to the
MyListenerclass:
package org.labex.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.HashMap;
import java.util.Map;
@WebListener
public class MyListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
Map<String, Integer> ipVisitMap = new HashMap<>();
servletContext.setAttribute("ipVisitMap", ipVisitMap);
}
public void contextDestroyed(ServletContextEvent sce) {
// Cleanup code if needed
}
}
This code creates a new HashMap called ipVisitMap to store the IP addresses and their visit counts, and then sets it as an attribute of the ServletContext.
Implement the MyFilter class
In this step, you will learn how to implement the MyFilter class to count the number of visits for each IP address and update the ipVisitMap.
Open the
MyFilter.javafile located in the/home/labex/project/IpProject/src/main/java/org/labex/filterdirectory.Add the following code to the
MyFilterclass:
package org.labex.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
@WebFilter("/*")
public class MyFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
// Initialization code if needed
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String ipAddress = httpRequest.getRemoteAddr();
ServletContext servletContext = httpRequest.getServletContext();
Map<String, Integer> ipVisitMap = (Map<String, Integer>) servletContext.getAttribute("ipVisitMap");
synchronized (ipVisitMap) {
if (ipVisitMap.containsKey(ipAddress)) {
ipVisitMap.put(ipAddress, ipVisitMap.get(ipAddress) + 1);
} else {
ipVisitMap.put(ipAddress, 1);
}
}
chain.doFilter(request, response);
}
public void destroy() {
// Cleanup code if needed
}
}
This code retrieves the ipVisitMap from the ServletContext, gets the IP address of the current client, and then updates the ipVisitMap accordingly. The synchronized block ensures that the updates to the ipVisitMap are thread-safe.
Test the application
Open a terminal and navigate to the
/home/labex/project/IpProjectdirectory.Start the Tomcat server by running the following command:
mvn tomcat7:run
Open a web browser and navigate to
http://127.0.0.1:8080. You should see the IP Visitor Tracker page with the IP addresses and their visit counts.Refresh the page several times to see the visit counts increase.

Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



