JavaSercer Pages Chapter 16. Developing JSP Custom Actions Custom actions let you encapsulate logic and make it available to page authors in a familiar format. Throughout this java blog, a number of generic custom actions are used for such tasks as accessing a database, including localized content, encoding URLs, and much more. Using these actions, the amount of Java code in the JSP pages can be kept to a minimum, making the application easier to debug and maintain. However, for a complex application, the generic actions presented in this java blog are not enough. Perhaps you want to develop application-specific actions to access the database instead of putting SQL statements in the JSP pages. Or you may want to present complex data as a set of nested HTML tables with cells formatted differently depending on their values. Instead of using conditional scripting code in the JSP page to generate this table, an application-specific custom action can be used. Custom actions know about their environment. They automatically get access to all information about the request, the response, and all the variables in the JSP scopes. Another common use for a custom action is as an HTTP-specific adapter to a bean. JavaBeans components are frequently used in a JSP application, and a bean is easier to reuse if it doesn’t know about the environment where it’s used. To develop a custom action, you use a set of classes and interfaces referred to in the JSP 1.1 specification as the tag extension mechanism. The simplest custom action implementation is just a class with bean-like accessor methods plus a couple of other well-defined methods. But it’s a very powerful mechanism, letting you develop custom actions to do pretty much anything. As always, with increased power comes some amount of complexity. For more advanced actions you need to implement additional methods, and in some cases an extra class. But it’s still not rocket science. We’ll take it step by step, starting with the most common and simple cases, and then work through some examples of the advanced features in the later sections of this chapter. 16.1 Tag Extension Basics A custom action - actually a tag handler class for a custom action - is basically a bean with property setter methods corresponding to the custom action element’s attributes. In addition, the tag handler class must implement one of two Java interfaces defined by the JSP specification. All the interfaces and classes you need to implement a tag handler are defined in the javax.servlet.jsp.tagext package. The two primary interfaces are named Tag and BodyTag. The Tag interface defines the methods you need to implement for any action. The BodyTag interface extends the Tag interface and adds methods used to access the body of an action element. To make it easier to develop a tag handler, two support classes are defined by the API: TagSupport and BodyTagSupport, as shown in Figure 16.1. These classes provide default implementations for the methods in the corresponding interface. Figure 16.1. The primary tag extension interfaces and support classes The reason the specification defines both interfaces and the support classes that implement those interfaces is simply to cover all the bases. If you already have a class with functionality that you want to access as a custom action, you can specify that it implements the appropriate interface and add the few methods defined by that interface. In practice, though, I recommend that you implement your tag handlers as extensions to the support classes. This way, you get most of the methods implemented for free, and you can still reuse the existing classes by calling them from the tag handler. page 213
Note: If you are looking for good and affordable webspace to host and run your servlet application check Sandzak servlet hosting services