Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for accessing LoggingFilter in Servlet
Previous Home Next

In this program, a LoggingFilter is used for printing message/information over server (like Tomcat) console log. Steps involved in creating filter class and mapping into web.xml file. click here

Application directory structure

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
  xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <filter>
  <filter-name>LogInFilter</filter-name>
  <filter-class>r4r.Filter.LogInFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>LogInFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <session-config>
  <session-timeout>
    30
  </session-timeout>
 </session-config>
</web-app>

Index.jsp

<%-- 
 Document: index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" 
	content="text/html; charset=UTF-8">
  <title>r4r.co.in-index</title>
 </head>
 <body>
  <h1>Logging Filter! Welcome to R4R</h1>
 </body>
</html>

Filter Program

/*
 * Save as a LogInFilter.java
 */
package r4r.Filter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServletRequest;
/**
 *
 * @author R4R
 */
public class LogInFilter implements Filter,
	SingleThreadModel {

 private static final boolean debug = true;
 private FilterConfig filterConfig = null;
 protected int count;

 // default constructor
 public LogInFilter() {
 }

 /**
  * Init method for this filter 
  */
 @Override
 public void init(FilterConfig filterConfig) {
  this.filterConfig = filterConfig;
  this.count = 0;
  if (filterConfig != null) {
if (debug) {
 log("LogInFilter:Initializing filter");
}
  }
 }

 /**
  * doFilter method for this filter
  */
 @Override
 public void doFilter(ServletRequest request,
  ServletResponse response, FilterChain chain)throws IOException, 
	 ServletException {
  PrintWriter pw = response.getWriter();
  HttpServletRequest req = (HttpServletRequest) request;
  if (debug) {
log("LogInFilter:doFilter()");
  }
  synchronized (this) {
count = (++count);
//increment count by 1 on every time whenever doFilter method invoke
  }

  if (request instanceof HttpServletRequest) {
pw.write("Number of time filter invoke at <b>"
+ req.getRequestURL() + "</b> is <b>"
  + count + "</b>, for more detail please check server log");
chain.doFilter(request, response);
log("Filter applying at " 
	+ req.getRequestURL().toString() + " with access time " + count
  + " from Server Name: " 
	+ getServerName(req.getServletContext().getServerInfo()).toString()
  + " with Server Version: "
  + getServerVersion(req.getServletContext().getServerInfo()).toString());

  }
 }

 /**
  * Return the filter configuration object for this filter.
  */
 public FilterConfig getFilterConfig() {
  return (this.filterConfig);
 }

 /**
  * Set the filter configuration object for this filter.
  */
 public void setFilterConfig(FilterConfig filterConfig) {
  this.filterConfig = filterConfig;
 }

 /**
  * Destroy method for this filter 
  */
 @Override
 public void destroy() {
  filterConfig = null;
  count = 0;
 }

 /**
  * Return a String representation of this object.
  */
 @Override
 public String toString() {
  if (filterConfig == null) {
return ("AuditFilter()");
  }
  StringBuilder sb = new StringBuilder("AuditFilter(");
  sb.append(filterConfig);
  sb.append(")");
  return (sb.toString());
 }

 /**
  * log method for print msg over server console
  */
 public void log(String msg) {
  filterConfig.getServletContext().log(msg);
 }

 private String getServerName(String serverInfo) {
  int slashBar = serverInfo.indexOf("/");
  if (slashBar == 0) {
return serverInfo;
  } else {
return serverInfo.substring(0, slashBar);
  }
 }

 private String getServerVersion(String serverInfo) {
  int slashBar = serverInfo.indexOf('/');
  if (slashBar == -1) {
return null;
  }
  int spaceBar = serverInfo.indexOf(' ', slashBar);
  if (spaceBar == -1) {
spaceBar = serverInfo.length();
  }
  return serverInfo.substring(slashBar + 1, spaceBar);
 }
}
Output of Program
Previous Home Next