Pr.Pg Next Pg

Defining a Filter tutorials

  • You can define filter same as servlet. Let’s see below the elements of filter and filter-mapping.

 

<web-app>

 

<filter>

<filter-name>…</filter-name>

<filter-class>…</filter-name>

</filter>

 

<filter-mapping>

<filter-name>…</filter-name>

<url-pattern>…</url-pattern>

</filter-mapping>

 

</web-app>

 

 

Example: Filter program to show how to use filter with servlet

Sam Sir

//Program name MyFilter.java

// Filter program to show how to use filter with servlet

 

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Enumeration;

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 MyFilter implements Filter {

FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

}

public void destroy() {

}

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)

throws IOException, ServletException {

servletResponse.setContentType("text/html");

PrintWriter out = servletResponse.getWriter();

out.println("my-param (InitParameter): " + filterConfig.getInitParameter("my-param"));

out.println("<br/><br/>Parameters:<br/>");

Enumeration<String> parameterNames = servletRequest.getParameterNames();

if (parameterNames.hasMoreElements()) {

while (parameterNames.hasMoreElements()) {

String name = parameterNames.nextElement();

String value = servletRequest.getParameter(name);

out.println("name:" + name + ", value: " + value + "<br/>");

}

} else {

out.println("---None---<br/>");

}

out.println("<br/>Start Regular Content:<br/><hr/>");

filterChain.doFilter(servletRequest, servletResponse);

out.println("<br/><hr/>End Regular Content:<br/>");

}

}

 

 

 

 

Example: Servlet program for communication with above Filter

Sam Sir

//Program name TestServlet.java

// Servlet program for communication with above Filter

 

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpServlet;

public class TestServlet extends HttpServlet implements javax.servlet.Servlet {

 

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

performTask(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,

IOException {

performTask(request, response);

}

private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,

IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("TestServlet says hi");

}

}

Output

Filters

 

  • Note: The above Filter Example must have below configuration as below in web.xml file:

<web-app>

<filter>

<filter-name>MyFilter</filter-name>

<filter-class>MyFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>MyFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<servlet>

<servlet-name>TestServlet</servlet-name>

<servlet-class>TestServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>TestServlet</servlet-name>

<url-pattern>/TestServlet</url-pattern>

</servlet-mapping>

<web-app>

Pr.Pg border                                              Next Pg