GTU WTAD Practical 22 : Create a servlet filter that logs all access to and from servlets in an application and prints

GTU WTAD Practical 22 :    Create a servlet filter that logs all access to and from servlets in an application and prints
the following to System.out:
1.  the time the request was received
2.  the time the response was sent
3.  how much time it took to process the request
4.  the URL of the resource requested
5.  the IP address of the visitor

GTU WTAD Practical 22 : Create a servlet filter that logs all access to and from servlets in an application and prints

Jump to WTAD Question Bank

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu22\

<%
	response.sendRedirect("./FilterDemo_Servlet");
%>

 

File Name : FilterDemo.java

File Location : Tomcat_Apache\webapps\gtu22\WEB-INF\classes

/*
		GTU WTAD Practical 22 :    Create a servlet filter that logs all access to and from servlets in an application and prints 
			the following to System.out:  
			1.  the time the request was received  
			2.  the time the response was sent  
			3.  how much time it took to process the request  
			4.  the URL of the resource requested  
			5.  the IP address of the visitor  
*/

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

public class FilterDemo implements Filter
{

	protected FilterConfig config;
	private ServletContext context;
	public void init(FilterConfig con)throws javax.servlet.ServletException
	{
		config=con;
	}
	public void destroy()
	{
	}
	public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)
			        throws java.io.IOException, javax.servlet.ServletException
	{
		PrintWriter out = response.getWriter();
		
		try
		{
			long startTime=System.currentTimeMillis();
			long endTime=System.currentTimeMillis();
			HttpServletRequest req=(HttpServletRequest)request;
			String path=req.getRequestURI();
			String visitor=req.getRemoteHost();
			out.print("filter is called... and can modify objects");  
			chain.doFilter(request,response);
			out.print("after actual servlet again filter is called");  
		}
		catch(Exception e)
		{
		}
	}

}

 

File Name : FilterDemo_Servlet.java

File Location : Tomcat_Apache\webapps\gtu22\WEB-INF\classes

/*
		GTU WTAD Practical 22 :    Create a servlet filter that logs all access to and from servlets in an application and prints 
			the following to System.out:  
			1.  the time the request was received  
			2.  the time the response was sent  
			3.  how much time it took to process the request  
			4.  the URL of the resource requested  
			5.  the IP address of the visitor  
*/

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

public class FilterDemo_Servlet extends HttpServlet
{

	public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
	{
		PrintWriter out = res.getWriter();
		out.println();
		out.println("Hello");
		out.println();
	}

}

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu22\WEB-INF

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
  version="3.0"
  metadata-complete="true">  
  
  <display-name>GTU WTAD Practical 22  </display-name>
  <description>   Create a servlet filter that logs all access to and from servlets in an application and prints the following to System.out:  1.  the time the request was received  2.  the time the response was sent  3.  how much time it took to process the request  4.  the URL of the resource requested  5.  the IP address of the visitor   </description>
  
  <servlet>
	<servlet-name>FilterDemo_Servlet</servlet-name>
	<servlet-class>FilterDemo_Servlet</servlet-class>
  </servlet>

  <servlet-mapping>
	<servlet-name>FilterDemo_Servlet</servlet-name>
	<url-pattern>/FilterDemo_Servlet</url-pattern>
  </servlet-mapping>
  
  <filter>
    <filter-name>FilterDemo</filter-name>
    <filter-class>FilterDemo</filter-class>
  </filter>
  
  <filter-mapping>
    <filter-name>FilterDemo</filter-name>
    <servlet-name>FilterDemo_Servlet</servlet-name>
  </filter-mapping>
  
    
</web-app>

 

Output

GTU WTAD Practical 22 : Create a servlet filter that logs all access to and from servlets in an application and prints

Leave a Reply