AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-servlet: What is filter in java?

How to define filter and how to use it ? Please explain with example.

java x 211
servlet x 20
Posted On : 2013-12-01 19:02:18.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188245674
up-rate
3
down-rate

Answers


A filter is an object that is used to perform filtering tasks such as conversion, log maintain, compression, encryption and decryption, input validation etc. A filter is invoked at the pre-processing and post-processing of a request. It is pluggable that´s why  it´s entry is defined in the web.xml file. If we remove the entry of filter from the web.xml file, filter will be removed automatically and we don´t need to change the servlet. So it will be easier to maintain the web application.filter servlet-anyforum.in
Let´s see the example of Filter.

Filter API :
-----------------
Like servlet filter have its own API.The javax.servlet package contains the three interfaces of Filter API
Filter
FilterChain
FilterConfig

1) Filter interface :
---------------------------
For creating any filter, you must implement the Filter interface.Filter interface provides the life cycle methods for a filter.
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.It is used to perform filtering tasks.
public void destroy():This is invoked only once when filter is taken out of the service.

2) FilterChain interface :
---------------------------------
The object of FilterChain is responsible to invoke the next filter or resource in the chain.This object is passed in the doFilter method of Filter interface.The FilterChain interface contains only one method:
public void doFilter(HttpServletRequest request, HttpServletResponse response): it passes the control to the next filter or resource.

*********************************  How to define Filter  *********************************

We can define filter same as servlet. Let´s see the elements of filter and filter-mapping.

<web-app>
 
<filter>  
<filter-name>...</filter-name>  
<filter-class>...</filter-class>  
</filter>  
 
<filter-mapping>  
<filter-name>...</filter-name>  
<url-pattern>...</url-pattern>  
</filter-mapping>  
 
</web-app>  

For mapping filter we can use, either url-pattern or servlet-name. The url-pattern elements has an advantage over servlet-name element i.e. it can be applied on servlet, JSP or HTML.

******************************  Simple Example of Filter  ******************************

In this example, we are simply displaying information that filter is invoked automatically after the post processing of the request.

index.html :
------------------
<a href="servlet1">click here</a>  
--------------------------------------------------------------------------------------------------------------------------------------

MyFilter.java :
---------------------
import java.io.IOException;  
import java.io.PrintWriter;  
 
import javax.servlet.*;  
 
public class MyFilter implements Filter{  
 
public void init(FilterConfig arg0) throws ServletException {}  
     
public void doFilter(ServletRequest req, ServletResponse resp,  
   FilterChain chain) throws IOException, ServletException {  
         
   PrintWriter out=resp.getWriter();  
   out.print("filter is invoked before");  
         
   chain.doFilter(req, resp);//sends request to next resource  
         
   out.print("filter is invoked after");  
   }  
   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>welcome to servlet<br>");  
         
   }  
 
}  
--------------------------------------------------------------------------------------------------------------------------------------

web.xml :
---------------
For defining the filter, filter element of web-app must be defined just like servlet.
<web-app>  
 
<servlet>  
<servlet-name>s1</servlet-name>  
<servlet-class>HelloServlet</servlet-class>  
</servlet>  
 
<servlet-mapping>  
<servlet-name>s1</servlet-name>  
<url-pattern>/servlet1</url-pattern>  
</servlet-mapping>  
 
<filter>  
<filter-name>f1</filter-name>  
<filter-class>MyFilter</filter-class>  
</filter>  
 
<filter-mapping>  
<filter-name>f1</filter-name>  
<url-pattern>/servlet1</url-pattern>  
</filter-mapping>  
 
</web-app>  

Posted On : 2013-12-01 19:26:03
Satisfied : 1 Yes  0 No
profile Saksham Kumar - anyforum.in Saksham Kumar
73433936114
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in