Escolar Documentos
Profissional Documentos
Cultura Documentos
Introduction
Filters are Java classes that can intercept requests from a client before they access a resource; manipulate requests from clients; intercept responses from resources before they are sent back to the client; and manipulate responses before they are sent to the client.
Working Mechanism
The implementation of filters is a new mechanism being introduced into the Servlets 2.3 standard.
Filters provide a powerful and standard alternative to the nonstandard servlet chaining supported by some early servers. A filter is a program that runs on the server before the servlet or JSP page with which it is associated. A filter can be attached to one or more servlets or JSP pages and can examine the request information going into these resources.
Working Mechanism
After doing so, it can choose among the following options. Invoke the resource (i.e., the servlet or JSP page) in the normal manner. Invoke the resource with modified request information. Invoke the resource but modify the response before sending it to the client. Prevent the resource from being invoked and instead redirect to a different resource, return a particular status code, or generate replacement output.
2. Put the filtering behavior in the doFilter method. - The first argument to the doFilter method is a ServletRequest object. - This object gives your filter full access to the incoming information, including form data, cookies, and HTTP request headers. - The second argument is a ServletResponse - The final argument is a FilterChain, it is used to invoke the servlet or JSP page.
4. Register the filter with the appropriate servlets and JSP pages. - Use the filter and filter-mapping elements in the deployment descriptor (web.xml).
5. Disable the invoker servlet. - Prevent users from bypassing filter settings by using default servlet URLs
public void init(FilterConfig config) throws ServletException The init method is executed only when the filter is first initialized. It is not executed each time the filter is invoked. First, the FilterConfig object provides access to the servlet context and to the name of the filter that is assigned in the web.xml file. Second, the FilterConfig object has a getInit-Parameter method that accesses filter initialization parameters that are assigned in the deployment descriptor (web.xml).
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException It is executed each time a filter is invoked It contains the bulk of the filtering logic. The first argument is the ServletRequest associated with the incoming request. The second argument is the ServletResponse. It is called:
- To send a response directly to the client by calling response.getWriter(). - To modify the output of the associated servlet or JSP page
The final argument to doFilter is a FilterChain object. It is called to: To invoke the next filter that is associated with the servlet or JSP page. If no other filters are in effect, then the call to doFilter invokes the servlet or JSP page itself.
public void destroy() This method is called when a server is permanently finished with a given filter object (e.g., when the server is being shut down). can be used for : cleanup tasks like closing files closing database connection pools that are used by the filter.
Register the Filter with the Appropriate Servlets and JSP Pages
Version 2.3 of the deployment descriptor introduced two elements for use with filters: filter and filter-mapping. - The <filter> element registers a filtering object with the system. - The <filter-mapping> element specifies the URLs to which the filtering object applies.
Example
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <filter> <filter-name>MyFilter</filter-name> <filter-class>myPackage.FilterClass</filter-class> </filter> <!-- ... --> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/someDirectory/SomePage.jsp</url-pattern> </filter-mapping> </web-app>
If a servlet name is supplied, that name must match a name given in the servlet element of web.xml.
If a URL pattern is applied to a servlet, the pattern must match a pattern that is specified with the servlet-mapping web.xml element
Next, you use servlet and servlet-mapping to stipulate that the URL http://host/ webAppPrefix/Blah should invoke SomeServlet, as below.
<servlet> <servlet-name>SomeServlet</servlet-name> <servlet-class>somePackage.SomeServletClass</servlet-class> </servlet> <!-- ... --> <servlet-mapping> <servlet-name>SomeServlet</servlet-name> <url-pattern>/Blah</url-pattern> </servlet-mapping>
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TimerFilter implements Filter { private FilterConfig config = null; public void init( FilterConfig config ) throws ServletException { this.config = config; } public void destroy() { config = null; }
public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain ) throws IOException, ServletException {
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> <web-app> <!-- Filter elements MUST be defined before Servlet elements --> <!-- Filter definitions --> <filter> <filter-name>timerFilter</filter-name> <filter-class>TimerFilter</filter-class> </filter> <!-- Filter mappings --> <filter-mapping> <filter-name>timerFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!-- Servlet definitions --> <servlet> <servlet-name>welcome</servlet-name> <servlet-class>WelcomeServlet</servlet-class> </servlet> <!-- Servlet mappings --> <servlet-mapping> <servlet-name>welcome</servlet-name> <url-pattern>/welcome</url-pattern> </servlet-mapping>
</web-app>
Child elements of <web-app> MUST be defined in this order: <display-name> <description> <distributable> <context-param> <filter> <filter-mapping> <listener> <servlet> <servlet-mapping> <session-config> <mime-mapping> <welcome-file-list> <error-page> <taglib> <resource-env-ref> <resource-ref> <security-constraint> <login-config> <security-role> <env-entry> <ejb-ref> <ejb-local-ref>
Uses of Filters