The following example code demonstrates how to Create a JSP Filter that Prints the IP Address.

Here is an example code for a JSP filter that prints the IP address of the client.

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;

public class IpFilter implements Filter {
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
         throws IOException, ServletException {
      String ipAddress = request.getRemoteAddr();
      System.out.println("IP address: " + ipAddress);
      chain.doFilter(request, response);

   public void init(FilterConfig fConfig) throws ServletException {}
   public void destroy() {}
  1. web.xml

This code creates a JSP filter named IpFilter that implements the Filter interface. The doFilter method is called every time a request is made to the web application. In this method, the client’s IP address is retrieved using request.getRemoteAddr() and printed to the console. The filter is applied to all URLs in the web application by specifying /* as the url-pattern in the web.xml configuration file.

Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise