Extending Servlet Code

Servlet Filters in the VSM

Hit Counter Filter

The Hit Counter filter keeps track of how many people visit the Virtual Shopping Mall (VSM). It does this by counting requests for a specific resource: the gateway page—the JSP that serves as the application' s entry point.

Following is the code from web.xml that declares and configures HitCounterFilter. In addition to the basic filter elements, this code defines initialization parameters that are passed to the container when the filter is first invoked. Here, the value of the parameter Counter is set to zero. A listing of the Java code for the HitCounterFilter class (which follows) shows how the Counter parameter is used.

...
   <filter>
<filter-name>HitCounterFilter</filter-name>
<filter-class> oracle.otnsamples.vsm.business.HitCounterFilter </filter-class>
<init-param>
<param-name>Counter</param-name>
<param-value>0</param-value>
</init-param>

</filter>
<filter-mapping>
<filter-name>HitCounterFilter</filter-name>
<url-pattern>/webstore/main</url-pattern>
</filter-mapping> ...

The code above assigns a value of /webstore/main to the <url-pattern> tag, but it's not obvious how this value maps to the gateway JSP mentioned earlier. Two points will help fill in the gaps:

  1. The path /webstore is defined elsewhere in web.xml as the URL pattern for the controlling servlet in the VSM application.
  2. The VSM uses an XML file to map convenient aliases to specific URLs. Following is the portion of requestmap.xml that links the URL pattern /webstore/main with the resource jsps/mallUser/shoppingMall.jsp.
...
  <uri-mapping>
<uri>/main</uri>
<jsp-template>jsps/mallUser/shoppingMall.jsp</jsp-template>
<controller class="oracle.otnsamples.vsm.controllers.CatalogueController" method="mainPage"/>
...
</uri-mapping> ...

The controling servlet in the VSM application is called RouterServlet, and it receives requests from clients and dispatches them to supporting classes on the server side. The figure below shows what happens when RouterServlet is initialized: it initializes the RequestMap class, which in turn reads mappings from requestmap.xml.

Initializing the request map.

So, when a user enters the VSM mall, the container has the information it needs to apply filters. The following listing shows a portion of the Java code for the HitCounterFilter class. The filtering mechanism enabled VSM developers to add this functionality to the application without touching existing application code.

...
  /**
* This method is called by the servlet container before the filter goes
* into service, and sets the filter's configuration object. The method
* initializes the counter to the value specified in the init param in web.xml
* @param <b>config</b> The filter config passed by the servlet engine
* @throws <b>ServletException</b> if any error in initializing the filter
*/
public void init( FilterConfig filterConfig )
throws ServletException {
this.filterConfig = filterConfig;
counter = Integer.parseInt( filterConfig.getInitParameter( "Counter" ) );
} ... /**
* This method performs the actual filtering work. In its doFilter() method,
* each filter receives the current request and response, as well as a
* FilterChain containing the filters that still must be processed.
* If the session is a new one, the counter is incremented and the
* control is passed down the filter chain.
* @param <b>request</b> The servlet request
* @param <b>response</b> The servlet response
* @param <b>chain</b> Object representing the chain of all filters
* @throws <b>ServletException</b> If there was an error while checking the
* role privileges
* @throws <b>IOException</b>
*/
public void doFilter( ServletRequest request,
ServletResponse response,
FilterChain chain )
throws IOException, ServletException {


if( filterConfig == null ) {
return ;
}
// Get the session object
HttpSession session = ( (HttpServletRequest)request ).getSession();
// If its a new request, its a new user to the site
if( session.getAttribute( "_counter" ) == null ) {
// Get the Counter stored as a context attribute.
counter++;
session.setAttribute( "_counter", "c" );

}
// Invokes the doFilter of the filter chain object
chain.doFilter( request, response );
}

This implementation of HitCounterFilter.doFilter ends with a call to chain.doFilter. This call passes control to the next filter in the chain, if such a filter exists (otherwise, it invokes the requested resource). A filter doesn't have to make this call; it could swallow the request and send a response to the client. Note also that chain.doFilter is a standard synchronous method call, so the filter could wait for the call to return and then operate on the response. In other words, chain.doFilter doesn't have to be the last method you call.

The next section describes how the VSM uses a filter to handle file uploads.

< Back to Introduction

Questions or comments? Post a message in the OTN Sample Code discussion forum or send email to the author.

Extending Servlet Code: Servlet Filters in the VSM
Author: Robert Hall, Oracle Corporation
Date: June 2002

This document is provided for information purposes only and the information herein is subject to change without notice. Please report any errors herein to Oracle Corporation. Oracle Corporation does not provide any warranties covering and specifically disclaims any liability in connection with this document.
Oracle is a registered trademark and Enabling the Information Age is a trademark or registered trademark of Oracle Corporation. All other company and product names mentioned are used for identification purposes only and may be trademarks of their respective owners.

Oracle

Oracle Corporation
World Headquarters
500 Oracle Parkway
Redwood Shores, CA 94065
U.S.A.

Worldwide Inquiries:
+1.650.506.7200


E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy