Share on Google+Share on Google+

Response Filter Servlet Example

This Example shows how to use of response filter in Java Servlet. Filter reads own initial parameters and adds its value to the response.

Response Filter Servlet Example


This Example shows how to use of response filter in  Java Servlet. Filter reads own initial parameters and adds its value to the response. Use the init-param child element of the filter element to declare the initialization parameter and its value. Inside the filter, access the init parameter by calling the FilterConfig object's getInitParameter method.




Here is the source code of

import javax.servlet.*;
import javax.servlet.http.*;

public class ResponseFilterExample implements Filter{
  private FilterConfig config = null;
  public void doFilter(ServletRequest request, ServletResponse response,
  FilterChain filterchain)
throws IOException, ServletException{
  filterchain.doFilter(request, response);
  PrintWriter out = response.getWriter();
  out.println("<b>Filter Received Your Message Successfully:</b>" 
  public void destroy() { }
  public void init(FilterConfig config) {
  this.config = config;

The above code initialized the FilterConfig object in its init methods, which is called once when the web container creates an instance of the filter. The code then gets the value of the filter's init parameter by calling: config.getInitParameter("response").

Mapping for the filter in web.xml:

  <param-value>Hello Response Filter Example!</param-value>

The source code of response.jsp:

  <title>Response Filter Example</title>
  <h2><font color="green">Response Filter and Initialized Parameters</font></h2>

Running the program by this url: http://localhost:8080/JavaExample/JSP/response.jsp  the message will display below

Download Source Code



Posted on: July 5, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Response Filter Servlet Example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 16, 2011
Discuss: Response Filter Servlet Example

I have an error while compiling ResponseFilterExample so what should i do to solve an error
June 10, 2011

how to use Servlet filer
Bipin Anghan
June 22, 2011

I like this comments.......... Thanks For giving me this type of blog.
August 1, 2012
How to avoid XSS cross scripting through Java code

Please suggest what is the best way to avoid cross scripting error in IE8 browser through Java code. I have already tried Setting the response object as response.setHeader("X-XSS-protection", "0") ; but this is not working for me. Whenever the control navigates from one page to another, it gets blocked because of cross scripting.