Pin It

How to create a Filter that adds url and time of request to a database in JSP and Servlets ?.

A simple Web Application demonstrating the usage of Filters in Java EE. Here it is showing how to create a Filter that adds url and time of request to a database in JSP and Servlets.


package com.hubberspot.filter;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

//Any request accessed by pattern /* will be intercepted by this
//filter whose name is DatabaseEntryFilter. No web.xml is needed 
//if we are using annotations
@WebFilter(urlPatterns={"/*"} , filterName="DatabaseEntryFilter" )
public class DatabaseEntryFilter implements Filter {
 // This filter implements Filter so it has to provide implementations
 // for Filter interface 

 Connection connection = null;
 ResultSet resultSet = null;
 Statement statement = null;
 static String query = null;
 String url = null;
 String username = null;
 String password = null;
 private FilterConfig filterConfig = null;

 public FilterConfig getFilterConfig() {
  return filterConfig;
 }

 public void setFilterConfig(FilterConfig fConfig) {
  this.filterConfig = fConfig;
 }

 // 3rd implementation of Filter interface
 public void destroy() {
  this.filterConfig = null;
 }

 // 2nd implementation of Filter interface 
 public void doFilter(
   ServletRequest servletRequest, 
   ServletResponse servletResponse, 
   FilterChain chain
   ) throws IOException, ServletException {

  // extracting the ServletContext from filterConfig
  ServletContext context = filterConfig.getServletContext();
  // Taking incoming time of request
  double incomingTime = System.currentTimeMillis();

  // Pre processing of filter before doFilter() method
  chain.doFilter(servletRequest, servletResponse); // here request goes to server
  // Post processing of filter before doFilter() method  

  // Taking outgoing time of response
  double outgoingTime = System.currentTimeMillis();

  String diffInTime = "" ;  
  String url = "";
  String client = "";
  String date = "";

  // Downcasting to HttpServletRequest
  HttpServletRequest request = (HttpServletRequest)servletRequest;
  // Extracting the URL of request
  url = request.getRequestURI();
  // Extracting the remote address of request
  client = request.getRemoteAddr();
  // Extracting the time difference 
  diffInTime= (outgoingTime - incomingTime)/1000.0 + "secs";
  // creating a new Date object 
  date = new Date().toString();
  // making the following insert query each time a 
  //new request is made to the server
  query = "insert into status () values " +
    "('"+ client + "','" + url + "','" + date + "','" + diffInTime + "');";
  executeQuery(query);

 }

 private void executeQuery(String query) {
  try {

   statement = connection.createStatement();
   statement.execute(query);

  }
  catch (SQLException e) {

   e.printStackTrace();

  }
 }

 // 3rd implementation of Filter interface
 public void init(FilterConfig fConfig) throws ServletException {
  this.filterConfig = fConfig;
  url = "jdbc:mysql://localhost:3306/javaee";
  username = "root"; 
  password = "root"; 
  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();

   connection = DriverManager.getConnection(url, username , password);
  }
  catch (Exception e) {

   e.printStackTrace();
  }
 }
}




Inserted Queries result in database on browsing few links :











Video tutorial to demonstrate @WebFilter annotation. 





Using annotation





Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot