Monday, December 5, 2016

Servlet Filter

A filter is an object that dynamically intercepts requests and responses to transform or use the information contained in the requests or responses.

Filters typically do not themselves create responses but instead provide universal functions that can be "attached" to any type of servlet or JSP page.

The filter is run before rendering view but after controller rendered response.

A Filter is used in the web layer only as it is defined in web.xml.

Filters are more suitable when treating your request/response as a black box system. They'll work regardless of how the servlet is implemented.

Filters are used to perform filtering tasks such as l
ogin authentication ,auditing of incoming requests from web pages, conversion, logging, compression, encryption and decryption, input validation etc.

A Servlet Filter is used in the web layer only, you can't use it outside of a
web context.



servlet filter


Filter Interface methods :-



MethodDescription
public void init(FilterConfig config)init() method is invoked only once.
It is used to initialize the filter.
public void doFilter( HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
doFilter() method is invoked every time when user
request to any resource, to which the filter is mapped.
public void destroy()This is invoked only once when filter is taken
out of the service.

Filter Example :-


MyFilter.java

import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain filterChain)
    throws IOException, ServletException {
      // your code goes here
    }

    public void destroy() {
    }
}


HelloServlet.java
import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.ServletException;  
import javax.servlet.http.*;  
  
public class HelloServlet extends HttpServlet {  
    public void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
  
        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  
      
        out.print("<br>Hello from servlet<br>");  
          
    }  
  
}  


Configuring the Servlet Filter in web.xml

<web-app>  
  <servlet>  
    <servlet-name>Servlet1</servlet-name>  
    <servlet-class>HelloServlet</servlet-class>  
  </servlet>               
  <servlet-mapping>  
    <servlet-name>Servlet1</servlet-name>  
    <url-pattern>/servlet1</url-pattern>  
  </servlet-mapping>  
   
 <filter>  
   <filter-name>Filter1</filter-name>  
   <filter-class>MyFilter</filter-class>  
 </filter>  
   
 <filter-mapping>  
   <filter-name>Filter1</filter-name>  
   <url-pattern>/servlet1</url-pattern>  
 </filter-mapping>  
</web-app>  


For more detail :-


https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/Filter.html

No comments:

Post a Comment