WEB.XML FILTERING EXAMPLE
Filtering is related with pre-processing. Before you go into your page you can do manipulation with your data. This mapping is defined in web.xml file of your project. And you can open a java file at the appropriate package with the suitable interface and you must implement its methods correctly. For example :
<filter>
<filter>
<filter-name>coding</filter-name>
<filter-class>com.example.code.Coding</filter-class>
<init-param>
<param-name>coding</param-name>
<param-value>ISO-8859-1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> coding </filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
You can declare your filter that you
use for the pre processor for your each request for example in Struts framework
if you have an action mapping for the path of url, you can jump into the filter
before action class. For example in this example code for each request with any
of the url parameters get into the filter name and this filter name maps the
action to the filter class that you can pre process somethink before action.
Your filter class in the appropriate package implements Filter as in the example code below.
package com.example.code;
import java.io.IOException;
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 Coding implements Filter{
@Override
public void destroy() {
// TODO Auto-generated
method stub
}
@Override
public void
doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain
arg2) throws IOException,
ServletException {
// TODO Auto-generated
method stub
System.out.println("Now in Filter
You can Pre process in here");
arg2.doFilter(arg0,
arg1);
}
@Override
public void init(FilterConfig
arg0) throws ServletException {
// TODO Auto-generated
method stub
}
}
For example if you start your server
and go into one of your page from url you can see from console the result of
test.
No comments:
Post a Comment