If we want to perform any operation before and after servlet gets executed then filter is the option we can use. In java it is possible to inject filter before and/or after any of the servlet gets called. Because of filter's nature, generally filters are used to perform common operation.
Some of the typical use of Filter are:
1) Intercept request from client side before reaching out to actual server side resource
2) Manipulate Server side response before it reaches to client side
Example where filters are used are:
1) Logging few parameters before every request / response
2) Check Authentication of the user before providing any server response to the Client
We can also plug series of filters to segregate the responsibilities among filters.
Lets see how to use filters in java:
There are simple two steps:
1) Create a java class which implements "Filter"
2) Modify web.xml file to write "<filter>" and <"filter-mapping>" entry
Here are the steps in detailed:
1) Create a java class which implements "Filter"
Here are the code snippet of the "Authentication" Filter for the quick reference:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
System.out.println("I am in Authentication Filter Before");
// pass the request along the filter chain
chain.doFilter(request, response);
System.out.println("I am in Authentication Filter After");
}
<!-- Entry of the Servlet -->
<!-- Entry of the filter which gets fired when any request comes for the "Result" servlet -->
Some of the typical use of Filter are:
1) Intercept request from client side before reaching out to actual server side resource
2) Manipulate Server side response before it reaches to client side
Example where filters are used are:
1) Logging few parameters before every request / response
2) Check Authentication of the user before providing any server response to the Client
We can also plug series of filters to segregate the responsibilities among filters.
Lets see how to use filters in java:
There are simple two steps:
1) Create a java class which implements "Filter"
2) Modify web.xml file to write "<filter>" and <"filter-mapping>" entry
Here are the steps in detailed:
1) Create a java class which implements "Filter"
Here are the code snippet of the "Authentication" Filter for the quick reference:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
System.out.println("I am in Authentication Filter Before");
// pass the request along the filter chain
chain.doFilter(request, response);
System.out.println("I am in Authentication Filter After");
}
2) Modify web.xml file to write "<filter>" and <"filter-mapping>" entry
<!-- Entry of the Servlet -->
<servlet>
<description></description>
<display-name>Result</display-name>
<servlet-name>Result</servlet-name>
<servlet-class>com.registration.Result</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Result</servlet-name>
<url-pattern>/Result</url-pattern>
</servlet-mapping>
<!-- Entry of the filter which gets fired when any request comes for the "Result" servlet -->
<filter>
<display-name>Authentication</display-name>
<filter-name>Authentication</filter-name>
<filter-class>com.fileter.Authentication</filter-class>
</filter>
<filter-mapping>
<filter-name>Authentication</filter-name>
<url-pattern>/Result</url-pattern>
</filter-mapping>
Now, whenever we hit the Url http://localhost:8080/student/Result at that time we will see output in catalina.out as follows:
I am in Authentication Filter Before (Line#1)
I am in Result Servlet (Line #2)
I am in Authentication Filter After (Line #3)
Assuming that we have one servlet created with the name "Result" whose "doGet" method prints "I am in Result Servlet" in console.
We can observe that filter has wrapped the actual Servlet call. So our filter got called and it has printed line #1 then Servlet got called it has printed line #2 and then after "Result" servlet is completed it again came back to "Authentication" filter and printed remaining statement i.e. line #3.
Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics
Cheers!
Henal Saraiya
(Lead Consultant)
CIGNEX Datamatics
No comments:
Post a Comment