Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Servlets Response Filter Servlet Example

Related Tutorials


 
 

Share on Google+Share on Google+

Response Filter Servlet Example

Advertisement
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  ResponseFilterExample.java

import java.io.*;
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);
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<b>Filter Received Your Message Successfully:</b>" 
  config.getInitParameter(
"response"));
  }
  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:

<filter>
  <filter-name>ResponseFilterExample</filter-name>
  <filter-class>ResponseFilterExample</filter-class>
  <init-param>
  <param-name>response</param-name>
  <param-value>Hello Response Filter Example!</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>ResponseFilterExample</filter-name>
  <url-pattern>/JSP/response.jsp</url-pattern>
</filter-mapping>

The source code of response.jsp:

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

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

Download Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: July 5, 2008

Related Tutorials

Discuss: Response Filter Servlet Example   View All Comments

Post your Comment


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

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

how to use Servlet filer
Bipin Anghan
June 22, 2011
J2ee

I like this comments.......... Thanks For giving me this type of blog.
Ankita
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.
DMCA.com