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 servlet Filter API?

Please give me an example of servlet Filter. Thanks!

java x 211
servlet x 20
Posted On : 2014-04-12 23:09:46.0
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939909
up-rate
5
down-rate

Answers


A filter can be defined as an object that is invoked at the time of pre-processing and post-processing of the request. It is mostly used to perform filtering tasks such as conversion, compression, logging, input validation encryption and decryption, etc.

Servlet filter is pluggable, i.e. filter´s entry is defined in web.xml file, if we remove the entry of filter from the web.xml file, It will be removed automatically and we don´t need to make any changes to the servlet.

Note: Unlike Servlet, One filter doesn´t have any kind of dependency on another filter.


Filter API: Like servlet filter also have its own API. The javax.servlet package have three interfaces of Filter API.
1. Filter
2. FilterChain
3. FilterConfig

1) Filter interface:
--------------------------
For creating any filter, you must need to implement the Filter interface. Filter interface provides the life cycle methods for a filter.

public void init(FilterConfig config): is invoked only once to initialize the filter.
public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain): is invoked every time when the user requests to any resource, to which the filter is mapped. It is used to perform filtering tasks described as above.
public void destroy(): 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 as a parameter in the doFilter method of Filter interface. The FilterChain interface contains only one method:

public void doFilter(HttpServletRequest request, HttpServletResponse response): it is responsible to pass the control to the next filter or resource.

How to define Filter in web.xml:
----------------------------------------------
We can define filter same as servlet.

<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.

Let´ see the Simple Example of Filter:
------------------------------------------------------
In this example, we are displaying some 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 : 2014-04-13 00:06:45
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129560202
Reply This Thread
up-rate
5
down-rate



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