1. Create TargetServlet class.
  2. Create DemoFilter class.
  3. Configure TargetServlet & DemoFilter in web.xml
  4. Deploy & Run the application.

Create TargetServlet class.

package com.java2learn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TargetServlet extends HttpServlet{

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("Inside TargetServlet:: doGet");
		PrintWriter out=resp.getWriter();
		out.println("This is target servlet ");
	}
	

}

Create DemoFilter class.

package com.java2learn.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;

public class DemoFilter implements Filter {

	public void init(FilterConfig fConfig) throws ServletException {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		PrintWriter out=response.getWriter();
		out.println("This line added by Filter Before processing request ");
		chain.doFilter(request, response);
		out.println("This line added by Filter After processing request ");
	}

	public void destroy() {

	}

}

Configure TargetServlet & DemoFilter in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>FilterDemo</display-name>

	<servlet>
		<servlet-name>TargetServlet</servlet-name>
		<servlet-class>com.java2learn.servlet.TargetServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>TargetServlet</servlet-name>
		<url-pattern>/test1</url-pattern>
	</servlet-mapping>

	<filter>
		<filter-name>DemoFilter</filter-name>
		<filter-class>com.java2learn.filter.DemoFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>DemoFilter</filter-name>
		<url-pattern>/test1</url-pattern>
	</filter-mapping>
</web-app>

Final Project Structure:
PackageStructure

Deploy & Run the application.

Output

Analysis:

Analysis

  1. When ever browser sending a request to the servlet, web container will check is there any filter configured for this Servlet or not
  2. If any filter is configured then web container forwards the request to the filter instead of servlet
  3. After execution of filtering Logic filter forwards the request to the Servlet
  4. After processing the request by Servlet the response will be forwarded to the filter instead of browser.
  5. After execution of filtering Logic the total response will be forwarded to the browser