Custom Tags and Design Patterns Preassessment Questions 1


































- Slides: 34

Custom Tags and Design Patterns Pre-assessment Questions 1. The syntax to instantiate a Java. Bean, Option. Bean contained in the package test is: a. <jsp: use. Bean id="option_bean" scope ="page“ class="test. Option. Bean"/> b. <% jsp: use. Bean id="option_bean" scope ="page“ class="test. Option. Bean" %> c. <jsp: use. Bean id="option_bean" scope ="page“ class="test. Option. Bean"> d. <%jsp: use. Bean id="option_bean" scope ="page“ class="test. Option. Bean"/> 2. Which JSP action tag can be used to get a property of a Java. Bean? a. <jsp: use. Bean> b. <jsp: get. Property> c. <jsp: get. Attribute> d. <jsp: set. Property> J 2 EE Web Components Lesson 4 A / Slide 1 of 34

Custom Tags and Design Patterns Pre-assessment Questions (Contd. ) 3. Identify the empty custom tag where the prefix string is tag. Prefix and name of the tag is tag. Name. a. <tag. Prefix: tag. Name/> b. <%@ tag. Prefix: tag. Name/> c. <%@ tag. Prefix: tag. Name> d. <tag. Name: tag. Prefix/> 4. The syntax to initialize a Java. Bean property using JSP actions is: a. <jsp: set. Property " name = bean. Name property = property. Name "> b. <jsp: set. Property name="bean. Name" property=" property. Name"/> c. <jsp: set. Property "name=bean. Name" "property= property. Name"> d. <jsp: set. Property name="bean. Name" property=" property. Name"> J 2 EE Web Components Lesson 4 A / Slide 2 of 34

Custom Tags and Design Patterns Pre-assessment Questions (Contd. ) 5. Identify the correct scope for which a Java. Bean object is available to all other components of the Web application. a. page b. request c. application d. session J 2 EE Web Components Lesson 4 A / Slide 3 of 34

Custom Tags and Design Patterns Solution to Pre-assessment Questions 1. a. <jsp: use. Bean id="option_bean" scope ="page“ class="test. Option. Bean"/> 2. b. <jsp: get. Property> 3. a. <tag. Prefix: tag. Name/> 4. b. <jsp: set. Property name="bean. Name" property=" property. Name"/> 5. c. application J 2 EE Web Components Lesson 4 A / Slide 4 of 34

Custom Tags and Design Patterns Objectives In this lesson, you will learn about: • • • Types of Custom Tags Classes and interfaces of Custom Tag API Web Application Design Patterns J 2 EE Web Components Lesson 4 A / Slide 5 of 34

Custom Tags and Design Patterns Advance Custom Tag Features Custom tags are user-defined reusable components that helps in minimizing the complex and recurring business logic in JSP. • Custom Tag API: The javax. servlet. jsp. tagext package is used to develop custom tag. The interfaces defined in the javax. servlet. jsp. tagext package are: • Tag interface: Defines the methods that are called during the life cycle of the tag. • Iteration. Tag interface: Extends the Tag interface and defines the methods that enable the tag handler to re-evaluate the body content of the custom tags. • Body. Tag interface: Extends the Iteration. Tag interface and defines the methods that enable the tag handler to manipulate the body content of the custom tag. J 2 EE Web Components Lesson 4 A / Slide 6 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • The following table describes the various classes defined in the javax. servlet. jsp. tagext package: Class Description Body. Content Is a subclass of the JSPWriter class and represents the body content of a tag. Tag. Support Acts as a base class for tag handlers and implements empty tags. Body. Tag. Support Implements the Body. Tag interface. This class is used to develop custom tags with body. Tag. Data Represents the attributes and their values. J 2 EE Web Components Lesson 4 A / Slide 7 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Various classes defined in the javax. servlet. jsp. tagext package are (Contd. ): Class Description Tag. Info Represents the information specified in the <tag></tag> element of the TLD file. This class is used by the JSP engine while translating a JSP page to a servlet. Tag. Library. Info Represents the information of the TLD file, such as tags and versioning information. Tag. Variable. Info Represents information about the variables of a custom tag. J 2 EE Web Components Lesson 4 A / Slide 8 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • The following table describes the various methods that you can implement in a tag handler: Method public int do. Start. Tag() public void release() Description Defined by the Tag interface. This method is invoked when the start tag of the custom tag is encountered. The do. Start. Tag() method returns the SKIP_BODY value to specify that the processing of the body content should be skipped. This method can also return EVAL_BODY_INCLUDE value to specify that the body content of the tag should be processed. Defined by the Tag interface. This method is invoked to allow the tag handler to release some of its resources. J 2 EE Web Components Lesson 4 A / Slide 9 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Various methods that you can implement in a tag handler are (Contd. ): Method Description do. After. Body() Implemented by the Body. Tag. Support class. This method is invoked after the body tag is evaluated. The do. After. Body() method returns the EVAL_BODY_AGAIN value to specify that the body content should be reevaluated. This method can also return SKIP_BODY value to specify that the evaluation of the body content should be skipped. public int do. End. Tag() Defined by the Tag interface. This method is invoked when the end tag of a custom tag is encountered. The do. End. Tag() method returns the EVAL_PAGE value to process the remaining JSP page or the SKIP_PAGE value to skip the processing of the remaining page. J 2 EE Web Components Lesson 4 A / Slide 10 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • The following table describes the various methods of the Page. Context class: Method Description public abstract Jsp. Writer get. Out() Returns the JSP implicit object, out. public abstract Servlet. Request get. Request() Returns the JSP implicit object, request. public abstract Servlet. Response get. Response() Returns the JSP implicit object, response. public abstract Http. Session get. Session() Returns the JSP implicit object, session. J 2 EE Web Components Lesson 4 A / Slide 11 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Various methods of the Page. Context class are (Contd. ): Method Description public abstract Exception get. Exception() Returns the JSP implicit object, exception. public abstract Servlet. Context get. Servlet. Context() Returns the JSP implicit object, application. public abstract Servlet. Config get. Servlet. Config() Returns the JSP implicit object, config. public abstract Object get. Page() Returns the JSP implicit object, page. J 2 EE Web Components Lesson 4 A / Slide 12 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Various types of custom tags are: • • • Custom tag with attributes Custom tag with body Nested custom tags J 2 EE Web Components Lesson 4 A / Slide 13 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Custom tag with attributes • Contains various attributes. You need to define a property for each attribute. You use the get. XXX() and set. XXX() methods to access and set the properties in the tag handler class. For example, the following code snippet shows a custom tag with an attribute, copyright and the value as books: <cpyrt: Copyright. Tag copyright=”books”> J 2 EE Web Components Lesson 4 A / Slide 14 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Custom tag with attributes (Contd. ) • The following code snippet shows how to declare an attribute in the tag handler class: • public class First. Custom. Tag extends Tag. Support { String copyright; . . . /* Defines a method that retrieves the copyright information */ public String get. Copyright() { return(this. copyright); } J 2 EE Web Components Lesson 4 A / Slide 15 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Custom tag with attributes (Contd. ) /* Defines a method that sets the copyright information */ public set. Copyright(String copyright) { this. copyright=copyright; } … } J 2 EE Web Components Lesson 4 A / Slide 16 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Custom tag with attributes (Contd. ) • You need to define the name of the attribute in the TLD file under the attribute element that appears within the tag element. <taglib> <tag> <name>First. Custom. Tag</name> <tag-class>copyright. First. Custom. Tag</tag-class> <body-content>empty</body-content> <attribute> <name>copyright</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </taglib J 2 EE Web Components Lesson 4 A / Slide 17 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Custom Tag with Body • Encloses other JSP components. The following code shows how to evaluate the body text “Hello! Welcome to the site. ”: <hello: Hello. Tag> Hello! Welcome to the site. </hello: Hello. Tag> The following code snippet shows a tag element within the TLD file that defines a tag with body: <tag> <name>Hello. Tag</name> <tag-class>hello. Hello. Tag. Handler</tag-class> <body-content>JSP</body-content> </tag J 2 EE Web Components Lesson 4 A / Slide 18 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Custom Tag with Body (Contd. ) • The following code snippet shows how to define the do. After. Body() method in a tag handler for a custom tag with body: • public int do. After. Body() throws Jsp. Exception { try { /* Obtain an instance of the Body. Content class */ Body. Content bc = get. Body. Content(); Get the bodycontent as string String body = bc. get. String(); Jsp. Writer out = bc. get. Enclosing. Writer(); out. println(body. to. Upper. Case()); } J 2 EE Web Components Lesson 4 A / Slide 19 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Nested Custom Tags • Are defined within the opening and closing tag of other custom tags. The following code snippet shows a nested tag: <pdtd: Parent. Tag condition= “true” > <chtd: Child. Tag> The expression evaluates to true </chtd: Child. Tag> </pdtd: Parent. Tag> J 2 EE Web Components Lesson 4 A / Slide 20 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Nested Custom Tags (Contd. ) • The following code shows how to define the tag handler for the parent tag, <pdtd: Parent. Tag>, in the tag handler file: import javax. servlet. jsp. tagext. *; import class Parent. Tag extends Tag. Support { private boolean condition; public int do. Start. Tag(){ return EVAL_BODY_INCLUDE; } public void set. Condition (Boolean condition) { this. condition = condition; } J 2 EE Web Components Lesson 4 A / Slide 21 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Nested Custom Tags (Contd. ) public boolean get. Condition() { return condition; } } J 2 EE Web Components Lesson 4 A / Slide 22 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Nested Custom Tags (Contd. ) • The following code shows how to define a tag handler for the child tag in the tag handler file: • import javax. servlet. jsp. tagext. *; import class Child. Tag extends Tag. Support { public int do. Start. Tag(){ Parent. Tag parent = (Parent. Tag) get. Parent(); /* Access the parent tag using the get. Parent() method */ boolean condition = parent. get. Condition(); /* Access the parent eval_condition attribute using the get. Condition() method */ if(condition){ return EVAL_BODY_INCLUDE; } J 2 EE Web Components Lesson 4 A / Slide 23 of 34

Custom Tags and Design Patterns Advance Custom Tag Features (Contd. ) • Nested Custom Tags (Contd. ) else{ return SKIP_BODY; } }/* End do. Start. Tag() */ } J 2 EE Web Components Lesson 4 A / Slide 24 of 34

Custom Tags and Design Patterns Demonstration-Developing a Custom Tag Application • Problem Statement • Larry Williams, the CEO of ABC Inc. wants the company to have an interactive Web site, which greets end users who visit the Web site and displays the current time. He also wants the end user to be able to specify the background color of the home page. Larry asks John, the company’s Web master to modify the home page of the Web site so that a customized greeting message is displayed to the end user along with the current system time. J 2 EE Web Components Lesson 4 A / Slide 25 of 34

Custom Tags and Design Patterns Demonstration-Developing a Custom Tag Application (Contd. ) • Solution • To solve the preceding problem, perform the following steps: 1. Create an HTML Page. 2. Create a Tag Handler. 3. Create a TLD File of the Tag Handler. 4. Create a JSP page. 5. Specify the Location of the TLD File. 6. Access the Custom Tag Application. J 2 EE Web Components Lesson 4 A / Slide 26 of 34

Custom Tags and Design Patterns Web Application Design Patterns • The design patterns used in the J 2 EE applications are: • • Value Object Model-View-Controller (MVC) Architecture Data Access Object (DAO) Business Delegate J 2 EE Web Components Lesson 4 A / Slide 27 of 34

Custom Tags and Design Patterns Web Application Design Patterns (Contd. ) • Value Object Design Pattern: Is a business tier Pattern that provides solutions to the problems that occur when a client accesses a business object. J 2 EE Web Components Lesson 4 A / Slide 28 of 34

Custom Tags and Design Patterns Web Application Design Patterns (Contd. ) • Model View Controller (MVC): Is a presentation tier pattern and is also known as the client tier pattern. MVC pattern is used to develop interactive J 2 EE applications that support different types of application clients. J 2 EE Web Components Lesson 4 A / Slide 29 of 34

Custom Tags and Design Patterns Web Application Design Patterns (Contd. ) • Data Access Objects Design Pattern: Provides solution to the various problems that might occur while accessing external resources from the J 2 EE application. J 2 EE Web Components Lesson 4 A / Slide 30 of 34

Custom Tags and Design Patterns Web Application Design Patterns (Contd. ) • Business Delegate Design Pattern: Hides the complexity for calling the remote business methods of EJB components by Web clients. J 2 EE Web Components Lesson 4 A / Slide 31 of 34

Custom Tags and Design Patterns Summary In this lesson, you learned: • • • Custom tag API consists of classes and interfaces, of the javax. servlet. jsp. tagext package. The interfaces defined in the javax. servlet. jsp. tagext package are, Tag, Iteration. Tag and Body. Tag. There are 3 types of Custom tags in JSP: • Custom Tags with attributes • Custom Tags with body • Nested custom tags J 2 EE Web Components Lesson 4 A / Slide 32 of 34

Custom Tags and Design Patterns Summary (Contd. ) • • JSP provides the following event handling methods: • do. Start. Tag(): Invoked when the start tag of a custom tag is encountered. • do. End. Tag(): Invoked when the end tag of a custom tag is encountered. • do. Init. Body(): Invoked before the evaluation of the tag’s body. • do. After. Body(): Invoked after the evaluation the tag’s body. • release(): Releases the resources. Design pattern is a document that describes the general solutions to the design level problems in a software project. The design patterns suggest general problems at a macro level, which can be modified by the software developer to suit their specific needs. J 2 EE Web Components Lesson 4 A / Slide 33 of 34

Custom Tags and Design Patterns Summary (Contd. ) • • J 2 EE design patterns are described as: • Presentation tier pattern • Integration tier pattern • Business tier pattern Model View Controller (MVC) Design Pattern represents the presentation tier and is known as the client tier. Data Access Objects Design (DAO) Pattern represents the integration of an application with various external resources, such as databases and other utility software used by an application. Business Delegate Design Pattern represents the business logic part. It abstracts the client information from the business logic. J 2 EE Web Components Lesson 4 A / Slide 34 of 34