Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.

Similar presentations


Presentation on theme: "Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page."— Presentation transcript:

1 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page directive specifies that the current JSP page is an error page. a.errorPage b.isErrorPage c.contentType d.buffer 2.The _____________ is used to insert contents of some other file, such as an HTML file, JavaBean, or another JSP page, into the current JSP page during compilation of the JSP page into a servlet. a.taglib directive b.page directive c.include directive d.include action

2 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 2 of 37 J2EE Web Components Pre-assessment Questions (Contd.) 3.Identify the correct option that allows you to embed Java codes in a JSP page. a.declarations b.directives c.action d.scriptlets 4.Identify the valid directive to import the java.util package in a JSP page. a. b. c. d.

3 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 3 of 37 J2EE Web Components Pre-assessment Questions (Contd.) 5.Which object is not a JSP implicit object? a.out b.page c.request d.context

4 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 4 of 37 J2EE Web Components Solution to Pre-assessment Questions 1.b. isErrorPage 2.c. include directive 3.d. scriptlets 4.b. 5.d. context

5 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 5 of 37 J2EE Web Components Objectives In this lesson, you will learn about: Using JavaBeans in JSP Developing JSP Custom Tags

6 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 6 of 37 J2EE Web Components Using JavaBeans in JSP JSP allows separation of the roles of Web designer and programmers involved in developing Web applications using JavaBeans. A JavaBean is a reusable and self-contained software component that take advantage of all the security and platform independent features of Java. The programmer can create a JavaBean to access the database and develop data processing code in the bean, while the designer can design the user interface in the JSP page. The JavaBean can be included in a JSP page to start the processing of user requests.

7 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 7 of 37 J2EE Web Components Using JavaBeans in JSP (Contd.) To use a JavaBean in your JSP page, you need to: Include the JavaBean reference in the JSP page Set the JavaBean property Get the JavaBean property

8 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 8 of 37 J2EE Web Components Using JavaBeans in JSP (Contd.) Including Bean Reference You can use the JSP action tag, to include a bean reference in your JSP page. The action tag creates an instance of the JavaBean and stores the bean reference in a variable. The variable can then be used to access the JavaBean throughout the JSP page. The following code snippet shows the syntax of the action tag: <jsp:useBean id=”Bean Name” scope=”ScopeName” class=”class name/>

9 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 9 of 37 J2EE Web Components Using JavaBeans in JSP (Contd.) Scope of JavaBean in a JSP page page: Specifies that the JavaBean object is available only for the current page. The following code snippet shows the action tag that includes a bean with a page scope: request: Specifies that the JavaBean object is available for the current request. The following code snippet shows the action tag that includes a bean with a request scope:

10 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 10 of 37 J2EE Web Components Using JavaBeans in JSP (Contd.) session: Specifies that the JavaBean object is available only for the current session. The following code snippet shows the action tag that includes a bean with a session scope: application: Specifies that the JavaBean object is available for the entire Web application. The following code snippet shows the action tag that includes a bean with an application scope:

11 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 11 of 37 J2EE Web Components Using JavaBeans in JSP (Contd.) Setting Beans Properties The tag enables you to set properties for a JavaBean to specific values. The tag can be defined inside tag or anywhere in the JSP file after the declaration of tag. The following syntax shows how to set a property of a JavaBean using tag:

12 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 12 of 37 J2EE Web Components Using JavaBeans in JSP (Contd.) The following table describes various attributes of the tag: AttributeDescription name Specifies the name of the bean object. The value of this attribute should be the same as the value of the id attribute of the tag. property Represents the name of JavaBean property to be set.

13 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 13 of 37 J2EE Web Components Using JavaBeans in JSP (Contd.) Reading Bean Properties The action tag is used to read the value of a JavaBean property in your JSP page. The value returned by the tag, is converted into java.lang.String and is placed into an implicit object, out. The following syntax shows how to get the JavaBean property using the tag:

14 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 14 of 37 J2EE Web Components Using JavaBeans in JSP (Contd.) The following table describes various attributes of the tag: AttributeDescription name Represents the name of the JavaBean object. property Represents the bean property for which you want to retrieve the value.

15 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 15 of 37 J2EE Web Components Demonstration-Implementing JavaBeans in JSP Problem Statement Create a JSP application that connects to a database and retrieve the details, such as author id, address, city, state, and zip code related to authors. The JavaBean component should accept the author id, driver name, and URL as a parameter. The information is to be retrieved from author’s table.

16 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 16 of 37 J2EE Web Components Demonstration-Implementing JavaBeans in JSP (Contd.) Solution To solve the given problem, perform the following task: 1. Create a JavaBean that implements the business logic. 2. Create a JSP page that instantiates a JavaBean. 3. Create the user interface using HTML. 4. Access the JSP application.

17 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 17 of 37 J2EE Web Components Developing JSP Custom Tags JSP Custom Tags Provide a mechanism to a Web programmer to reuse and encapsulate complex recurring code in a JSP application. Provide simplicity and reusability of Java code. Enable you to perform various functions, such as: Accessing all implicit variables of a JSP page, such as request, response, in, and out. Modifying the response generated by a calling JSP page. Initializing and instantiating a JavaBean component.

18 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 18 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) The various types of custom tags that you can develop in JSP are: Empty tags: Refer to the custom tags that do not have any attribute or body. The following code snippet shows an empty custom tag: Tags with attributes: Refer to custom tags for which you can define attributes to customize the behavior of the custom tag. The following code snippet shows a custom tag with an attribute color:

19 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 19 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) The various types of custom tags that you can develop in JSP are: Tags with a body: Refer to the custom tag within which you can define nested custom tags, scripting elements, actions, HTML text, and JSP directives. The following code snippet shows a custom tag that contains a JSP scripting element as its body: Nested tags: Refer to the set of custom tags in which one custom tag encloses one or more custom tags. The following code snippet shows a nested custom tag: “ > The expression evaluates to true

20 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 20 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Creating a Custom Tag To develop a custom tag, you need to perform following steps: Develop a tag handler Develop the Tag Library Descriptor (TLD) file Include the Tag Library in a JSP page Deploy the application

21 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 21 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Developing a Tag Handler All custom tags have a corresponding tag handler, which is a Java class that implements the functionality of the custom tag. The javax.servlet.jsp.tagext package provides the classes and interfaces that you can use to develop tag handlers. Base classes, such as TagSupport and BodyTagSupport of the javax.servlet.jsp.tagext package implements the Tag interface to provide implementation of the interface methods. You can extend these helper classes in your tag handler classes and override those methods that are required to implement the functionality of your tag.

22 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 22 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Developing a Tag Handler (Contd.) You can extend the TagSupport class of the javax.servlet.jsp.tagext package in your tag handler to develop a tag handler for an empty tag. The following code snippet shows a tag handler, WelcomeTag that extends the TagSupport class to implement a custom tag: import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; /* Extending the TagSupport interface */ public class WelcomeTag extends TagSupport

23 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 23 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Developing a Tag Handler (Contd.) The methods that you need to override in the tag handler of an empty custom tag are: doStartTag() : Is called when the container encounters the start tag of a custom tag. The following code snippet shows the doStartTag() method of a tag handler for a custom tag that displays a welcome message: public int doStartTag() throws JspException { try { JspWriter out=pageContext.getOut(); out.println(" Welcome to New Tech Books Inc. "); }catch (Exception ioException) { System.err.println("IO Exception"); System.err.println("ioException.toString()"); } return SKIP_BODY;}

24 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 24 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Developing a Tag Handler (Contd.) doEndTag() : Is called when the container encounters the end tag of a custom tag. The following code snippet shows the doEndTag() method of a tag handler for a custom tag that displays a welcome message: public int doEndTag() throws JspException { /* Skip the processing of the rest of the page */ return SKIP_PAGE; }

25 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 25 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Developing the TLD File A TLD file: Defines a custom tag in XML format. Provides information, such as tag library version, name of the tag, description of the tag, and the name of the tag handler that implements the tag. Contains a root element within which various elements appear. Defines multiple custom tag using a element within element.

26 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 26 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) The following table describes the various elements: Element NameDescription Defines the version of the tag library. The element,, must be declared when you define a new tag library. Defines the version of the JSP page that the tag library uses. Defines a short name of the tag library that refers to the tag library.

27 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 27 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Various elements are (Contd.): Element NameDescription Describes the tag library, such as the type of tags contained in the tag library. Defines a unique id for the tag library. Contains elements to define a custom tag.

28 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 28 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Various elements are (Contd.): Element NameDescription Defines the name of custom tag. This is a required tag because it must be defined while creating a JSP custom tag. Defines the tag handler class that provides the functionality of custom tag. It is a required element and you must specify the fully qualified name of the class.

29 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 29 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Various elements are (Contd.): Element NameDescription Defines the tag functionality. This is an optional element. Defines the body content enclosed within the opening and closing tag of the custom tag. For empty custom tag, the body of this element is empty.

30 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 30 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) The following code snippet shows a TLD file that defines an empty tag, Welcome implemented by the WelcomeTag tag handler: 1.0 1.2 Welcome Tag A custom tag to display welcome message Welcome welcome.WelcomeTag empty

31 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 31 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Including the Tag Library in a JSP The directive allows you to include a tag library in a JSP page. The directive contains the uri attribute that specifies the location of the TLD file and a prefix attribute that specifies the name with which the JSP page will use the custom tags. You can use the following code snippet to include the tag library defined by Welcome.tld in your JSP page: You can use the following code snippet to use the custom tag, once you have included the tag library:

32 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 32 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Deploying the Application You need to deploy the JSP page along with the class file of the tag handler and the TLD file to access the JSP page that uses custom tags. You need to map the tag library, uri specified in the uri attribute of the directive to the actual location of the TLD file during the deployment. You can specify the mapping in the JSP Tag Libraries pane under the File Refs tab of the J2EE Deploytool window.

33 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 33 of 37 J2EE Web Components Developing JSP Custom Tags (Contd.) Deploying the Application (Contd.) The deploytool utility internally updates the web.xml deployment descriptor with the element, as shown in the following code snippet: /Welcome.tld /web-inf/Welcome.tld

34 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 34 of 37 J2EE Web Components Demonstration-Implementing Custom Tags Problem Statement The senior management of New Tech Books has decided to have the copyright information on all the pages of their application designed using JSP. They also require that incase a new JSP page is added to the application, the code for displaying the copyright information can be reused. Jerry Smith, the software developer at the company decides to use custom tags for creating this part of the application.

35 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 35 of 37 J2EE Web Components Demonstration-Implementing Custom Tags (Contd.) Solution To solve the given problem, perform the following tasks: 1. Create a tag handler 2. Create a TLD file 3. Create a JSP page 4. Package the JSP custom tag application 5. Map the TLD file 6. Deploy the custom tag application 7. Access the custom tag application

36 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 36 of 37 J2EE Web Components Summary In this lesson, you learned: JavaBeans are reusable components that are used to separate business logic from the JSP page. JavaBeans are accessible in one of these scopes, page, request, session, or application scopes. tag is used to use a JavaBean in the JSP page. tag is used in a JSP page to set properties of a JavaBean. tag is used in a JSP page to retrieve a property value from a JavaBean. Custom tags are user defined reusable tags developed to perform repetitive tasks in a JSP page. A custom tag can be empty, can contain attributes, body, or nest within another tag.

37 Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 37 of 37 J2EE Web Components Summary (Contd.) A tag handler is a Java class that implements the functionality of a custom tag. The javax.servlet.jsp.tagext package contains classes and interfaces to develop tag handlers. You can create a tag handler for an empty custom tag by extending the tag handler class from the TagSupport class. A TLD file defines a custom tag. The taglib element is the root element of the TLD file and can define one or more custom tags using the tag elements. A JSP page uses the directive to include a tag library. The following steps are used to develop a custom tag: Develop a tag handler Develop the TLD file Include the Tag Library in a JSP page Deploy the application


Download ppt "Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page."

Similar presentations


Ads by Google