Sample Program: Writing Filters for the BookShoppingServlet

Sample Program: Writing Filters for the BookShoppingServlet

The source code of the DemonstrateFilters.java program is given in Listing 4.3.

Listing 4.3 DemonstrateFilters.java
 /******************************************************************************   * Class Name:DemonstrateFilters   * Implements:Filter   * Description:Servlet to demonstrate filter functionality   *             This is invoked when the BookShoppingServlet is called, and is   *             executed before the BookShoppingServlet is executed.   * @author Mandar S. Chitnis, Lakshmi AM.       @version 1.1   * Copyright (c) by Sams Publishing. All Rights Reserved. *******************************************************************************/ package com.sams.learnweblogic7.servlets; //importing the servlet and security related packages import javax.servlet.*; import javax.servlet.http.*; import weblogic.servlet.security.*; public class DemonstrateFilters implements Filter {   //defining the global variables   private FilterConfig filterConfig; // Constructor for this class  public DemonstrateFilters() {    log("DemonstrateFilters constructer invoked ... ");   } //This method is called only once when the servlet is loaded for the first time //by the container   public void init(FilterConfig filterConfig)       throws ServletException {       this.filterConfig = filterConfig;    } //This method is called only once when the servlet is unloaded by the container  public void destroy() {       this.filterConfig = null;    } //This method is called by the container  public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)      throws java.io.IOException, javax.servlet.ServletException {         log("The doFilter method has been invoked.");   /* the user is authenticated, pass request to the next filter in the list or    * if there are no more filters in the list, pass the request to the servlet    */    fc.doFilter(req,res);  } //Returns the filterConfig object for this filter  public FilterConfig getFilterConfig() {   return filterConfig;  } //Called by the container when this Filter is instantiated  public void setFilterConfig(FilterConfig cfg) {   filterConfig = cfg;  } //prints the messages onto the console  public void log(String s) {   System.out.println("[loginFilter]: " + s);  } } }//end of BookShoppingServlet 

The DemonstrateFilters.java filter consists of the init(), doFilter(), getFilterConfig(), and setFilterConfig() methods explained earlier and the wrapper log() method, which prints the output to the system console of the WebLogic Server.

Compile the Program

Use the compile.bat batch file provided to compile the servlet. The batch file compiles the DemonstrateFilters.java file located in the following directory in your domain:

 applications\ShoppingApp\WEB-INF\classes\com\sams\learnweblogic7\servlets\  

To verify that the compilation was successful, check that the corresponding .class file for DemonstrateFilters.java was created in the following directory as shown in Figure 4.3:

 c:\bea\user_domains\mydomain\applications\ShoppingApp\WEB-         INF\classes\com\sams\learnweblogic7\servlets 
Figure 4.3. Directory structure after compiling DemonstrateFilters.java.

graphics/04fig03.jpg

Deploy the Program

After a successful compilation, you need to deploy the filter in the WebLogic Server environment. Since the Web application archive for the servlets is already in place, you will not create a new Web archive file. The deployment activities that need to be carried out are described in the following sections.

Updating the .war Web Archive File

Filters are support applications for servlets. Hence, to deploy Web applications in WebLogic, the servlet classes need to be packaged in a Web archive file, called a .war file. You will be adding your filter to the existing Web archive file for the book-shopping application. To update the .war file, go to the root directory of your Web application at a DOS prompt:

 c:\bea\user_domains\mydomain\applications\ShoppingApp\WEB-INF\  

Then type the following command:

 jar  uv0f ShoppingApp.war .  
Registering Your Filter in web.xml

Once the .war file containing your filter class is ready, you need to register your filter class with WebLogic Server. To register your filter, you need to edit the web.xml file in your deployment directory, in this case the directory

 c:\bea\user_domains\mydomain\applications\ShoppingApp\WEB-INF\  

Add the following tags to the web.xml file:

 <filter>     <filter-name>DemonstrateFilters></filter-name>    <filter-class>                 com.sams.learnweblogic7.servlets.DemonstrateFilters    </filter-class> </filter> 

Notice that this process is similar to the registration of a servlet in this deployment descriptor file. The <filter></filter> tags encapsulate the registration information for your filter. Define the name for your filter within the <filter-name></filter-name> tags. The actual class filename qualified with the package name should be listed in the <filter-class></filter-class> tags.

The next step is to associate the filter with a servlet or servlets. Essentially, you map a filter to a URL. To do so, add the following tags to the web.xml file:

 <filter-mapping>      <filter-name>DemonstrateFilters</filter-name>     <url-pattern> /BookShoppingServlet</url-pattern> </filter-mapping> 

The WebLogic Server interprets these settings and invokes the DemonstrateFilters filter class when it receives a request from the browser for the URL defined in the <urlpattern></url-pattern> tags. (You can even define a filter for the entire domain of your Web site, thanks to the flexibility provided by filters.) Your deployment descriptor file web.xml should look similar to the screen shot in Figure 4.4.

Figure 4.4. Registering the filter in the web.xml file.

graphics/04fig04.jpg

In this example, when the WebLogic Server receives a request from the browser for /BookShoppingServlet, it invokes the DemonstrateFilters filter class before passing control to the BookShoppingServlet servlet.

To set a chain of filters, you just have to define the filter mappings that map to the same URL. While loading the settings defined in the deployment descriptor file, the WebLogic Server builds the list of filters in the order in which they are defined. For filters pointing to the same URL, the server builds a chain of filters in the order in which they are defined in the deployment descriptor file. For example:

 <filter-mapping>      <filter-name>myFilter1</filter-name>     <url-pattern>/Day4SampleServlet3</url-pattern> </filter-mapping> <filter-mapping>     <filter-name>myFilter2</filter-name>     <url-pattern>/Day4SampleServlet3</url-pattern> </filter-mapping> 

For these settings, the WebLogic Server builds a chain of filters. On receiving a request for the /Day4SampleServlet3, the WebLogic Server invokes the first filter, myFilter1; after its execution is complete, the next filter in the chain, myFilter2, is invoked. If you had defined more filters for this URL, they would be invoked in turn. Finally, the /Day4SampleServlet3 is invoked and receives the last preprocessed input from the last filter in the filter chain.

Tip

Apart from the order of definition of filters in a filter chain, WebLogic Server builds the filter chain using the following priority: Filters for a URL pattern mapping to a servlet are added to the chain after the filters that map to a URL pattern not mapping to the same servlet.


Execute the Program

The filter is invoked from the browser by calling the following URL:

http://localhost:7001/ShoppingApp/BookShoppingServlet

Since filters are an intermediary application for your servlets a behind-the-scenes functionality you cannot show the output on the screen. Hence, you will be looking at the server console and the server log to verify whether or not the filter was invoked.

The contents of the console should look similar to the screen shot in Figure 4.5.

Figure 4.5. The server console with the system output showing the invocation of the filter.

graphics/04fig05.jpg



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net