Servlet - Advanced Servlet Servlet Filter Rating: 5.0/5 (4 votes cast) | Level : Intermediate Author : Arunkumar S Download Source : filter.zip
Java Servelt Filter Tutorial - Deployment The <filter> configuration block
The filter class is defined with a <filter> block, which takes the following child elements: filter-name The name which will be used to identify the filter elsewhere in the web.xml file. filter-class The classname, including package, of the filter. This name will be used by the servlet container to load the filter class. init-param Initialization parameters to pass to the filter. We'll discuss these shortly. description Long description for the filter, this may be used by configuration tools also. icon Optional paths to image files for GUI configuration tools to use to represent the filter. display-name Optional descriptive text for the filter, mainly useful for configuration tools.
The only required elements are the name and class; the icon and display-name are pointless unless you're using a tool which uses them. Here is the configuration for the TimerFilter.
<filter> <filter-name>Timer</filter-name> <filter-class>com.kief.FilterDemo.TimerFilter</filter-class> <description> This filter times the execution of the request after the filter itself, and prints the execution time to the standard output. </description> </filter>
Note that the same filter class can be defined in multiple <filter> blocks, each with a different name. This creates a separate instance of the class for each <filter> block, each of which can have different configuration parameters.
The <filter-mapping> block
Now we need to tell the servlet container when to apply the filter, which is done with a <filter-mapping> block. The filter-mapping block includes the filter-name, which must match the name given in a <filter> block, and either a URL pattern or a servlet name. Most often you will use a URL pattern, but you can also specify the name of a servlet as defined in a <servlet> block.
URL patterns for filter-mappings are the same as for the <servlet-mapping> block, normally either a subpath such as "/secure/*", or a file ending like "*.gif". Exact paths may also be used, like "/filtered_page.html".
To have our filter handle all requests, we'll use the following filter-mapping block:
<filter-mapping> <filter-name>Timer</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
This filter will run for every single request handled by the servlet engine. In most cases this would put an unnecessary load on the server, since few filters really need to execute for every image file as well as flat HTML, JSP, and servlet calls.
To use this filter, you must declare it in the web.xmldeployment descriptor using the <filter>tag, as shown below: <filter> <filter-name>FirstFilter</filter-name> <filter-class> com.javaorigin.filter.example.FirstFilter </filter-class> </filter>
<servlet> <description></description> <display-name>FirstServlet</display-name> <servlet-name>FirstServlet</servlet-name> <servlet-class> com.javaorigin.filter.example.FirstServlet </servlet-class> </servlet>
<filter-mapping> <filter-name>FirstFilter</filter-name> <url-pattern>/FirstServlet</url-pattern> </filter-mapping>
<servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping>
Download Source : filter.zip Run our filter example http://localhost:8080/filter/FirstServlet
Outpult is look like this

< Previous 1 | 2 |
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|