Home

This Chapter
-Chapter 21: Decorating Request Objects
-The Decorator Pattern
-A Trimmer Filter
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

Chapter 21

Decorating Request Objects

When the Servlet specification first introduced filters, it was very exciting. A filter is a Java object that can intercept the HttpServletRequest object before reaching a servlet’s service method and the HttpServletResponse object after control leaves the service method. You can do useful things with filters, such as verifying user input, compressing Web contents, etc. Nevertheless, many other forms of creativity using filters are hindered by the fact that you cannot change the request parameters of an HttpServletRequest object, because the java.util.Map that contains parameters in an HttpServletRequest object is immutable, i.e. it cannot be changed. This greatly reduces the usefulness of filters. At least half of the times, you wish you could change the object you are applying your filter to. Wouldn’t it great if you could, for example, write a filter that trims users input before the HttpServletRequest object reaches the Struts action servlet. This way, you would not need to trim them in your action forms’ validate methods.

Fortunately, using the Decorator design pattern you can change the behaviors of an immutable object, even though you cannot change the object itself. This chapter deals with this pattern and provides an example of a filter that trims user input.

Previous
Next