Logging Filter Servlet Example

This example illustrates how one can write Logging Filter servlet to provide control over log file.

Logging Filter Servlet Example

Logging Filter Servlet Example

     

Example program to demonstrate Logging Filter

This example illustrates how one can write Logging Filter servlet to provide control over log file. You can have additional controls over these log files and these all are available to use by implementing "Filter" class. Filters are very important in servlet access and handling due to number of reasons, for example, it encapsulates recurring tasks in a reusable unit, modularizing codes so that they become easy to manage, transforming request from a servlet to JSP page. Most common task for a web application is to format data sent back to client since most clients require different format (e.g in WML,XML etc.) rather than only HTML so to accomplish these tasks of clients, Filtering is important to develop a fully featured Web Application. Filters can perform many different tasks, in which logging is one of the most important task. You can create filter class by implementing javax.servlet.Filter, which has three methods as follows:

  • void init(FilterConfig filterConfigObject) throws ServletException
  • void destroy()
  • void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchainObject) 
    throws IOException, ServletException

init(FilterConfig) is called once by the server to get prepared for service and then it calls doFilter() number of times for request processing. 

In this example there is LoggingFilterExample servlet which is writing Remote Address, URI , Protocol of calling JSP file into log file as server calls LoggingFilterExample via logging.jsp. Source code for LoggingFilterExample.java is given as below:

1. LoggingFilterExample.java

mport java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public final class LoggingFilterExample implements Filter 
{
  private FilterConfig filterConfigObj = null;

  public void init(FilterConfig filterConfigObj) {
  this.filterConfigObj = filterConfigObj;
  }

  public void doFilter(ServletRequest request, 
ServletResponse response,

  FilterChain chain)
  throws IOException, ServletException 
  {
  String remoteAddress =  request.getRemoteAddr();
  String uri = ((HttpServletRequestrequest).getRequestURI();
  String protocol = request.getProtocol();

  chain.doFilter(request, response);
  filterConfigObj.getServletContext().log("Logging 
Filter Servlet called"
);
    filterConfigObj.getServletContext().log("*******
*******************"
);
  filterConfigObj.getServletContext().log("User 
Logged ! " 
User IP: " + remoteAddress + 
   " Resource File: " + uri +
Protocol: " 
+ protocol);
  }

  public void destroy() { }
}

LoggingFilterExample writes content in log file by the following method:

  filterConfigObj.getServletContext().log()

2. logging.jsp

<%@ page language="java" %>
 <html>
   <head>
   <title>Logging Filter Example</title>
   </head>
  <body>
  <h1>Logging Filter</h1>
  This filter writes log file of Tomcat Web Server.
  <hr>
   See log file of Web server.
  <br>
  </body>
</html>

When this JSP file is called by the server then through the filter mapping LoggingFilterExample filter would be called and it will write content into log file of your web server. To do working we have to do mapping in the web.xml deployment descriptor. <filter> and <filter-mapping> element requires <filter-name> to tell the name of filter to which you want to map a servlet or URL pattern. This mapping is as follows:

3. web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
   <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/
xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">
  <display-name>Welcome to Tomcat</display-name>
  <description>Welcome to Tomcat</description>
  <filter>
   <filter-name>LoggingFilterExample</filter-name>
  <filter-class>LoggingFilterExample</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>LoggingFilterExample</filter-name>
  <url-pattern>/logging.jsp</url-pattern>
  </filter-mapping>

</web-app>

To run this example you have to follow these few steps given as below:

  1. Create and Save LoggingFilterExample.java
  2. Compile and put this LoggingFilterExample.java into classes folder
  3. Create and save logging.jsp
  4. Do the filter-mapping in web.xml
  5. Start Tomcat web server
  6. Type following URL into address bar
    http://localhost:8080/vin/logging.jsp

Output:

By typing above URL, the opened page would be like this

and now go and see your tomcat's logs/localhost.<current date>.log file. Last few lines in this file would be like this.

Download Source Code