Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP Tag Libraries Lec - 38. Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.

Similar presentations


Presentation on theme: "JSP Tag Libraries Lec - 38. Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp."— Presentation transcript:

1 JSP Tag Libraries Lec - 38

2 Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp & web.jsp As discussed, JSPs are built for presentation purpose

3 Last Lecture Example What if we replace all code of scriptlet with one single line (custom tag) <% CourseDAO courseDAO = new CourseDAO(); …… for ( ……. ) { …….. } …… %>

4 Contents What is a Custom Tag ? Why build Custom Tag? Types of Tags Developing Custom Tags Using Custom Tags Examples

5 What is Custom Tag ? A user defined component to perform certain action. Provides mechanism for encapsulating complex functionality for use in JSPs. We have already seen & used many built- in tags etc

6 Advantages More cleaner separation of processing logic and presentation, than Java Beans Have access to all the JSP page objects Can be customized using attributes

7 Types of Tags

8 1. Simple Tag 2. Tag with Attributes 3. Tag with Body

9 Types of tags 1.Simple Tags Start and End of tag No body within tag No Attributes For Example: Tag library prefix Tag Name

10 Types of tags 2.Tags with attributes Start and End of tag Attributes within tag No body enclosed For Example :

11 Types of tags 3.Tags with body Start and End of tag Body enclosed within tag Attributes (optional) For Example : some body

12 Building Custom Tags

13 We can build custom tag using either of the following specs JSP 1.2 JSP 2.0 We will use JSP 2.0

14 Building Custom Tags - Steps 1.Develop the Tag Handler class 2.Write the Tag Library Descriptor (tld) file 3.Deployment

15 1 - Tag Handler class Tag Handler is the java class Implicitly called when the associated tag is encountered in the JSP Must implement SimpleTag interface Usually extend SimpleTagSupport class. For example public class MyTagHandler extends SimpleTagSupport { ………….. }

16 1 - Tag Handler class cont. doTag() method By default does nothing Need to implement / override to code tag’s functionality Invoked when the end element of the tag encountered

17 1 - Tag Handler class cont. Implicit objects are available to tag handler class through pageContext object pageContext object can be obtained using getJspContext() method. For example, to retrieve out object PageContext pc = (PageContext)getJspContext(); JspWriter out = pc.getOut();

18 2 - Tag Library Descriptor (tld) XML based document Specifies information required by the JSP container such as: Tag library version JSP version Tag name Tag handler class name Attribute names etc.

19 3 - Deployment Place Tag Handler class in myapp/WEB- INF/classes folder of web application Place TLD file in myapp/WEB-INF/tlds folder

20 Using Custom Tags

21 Use “taglib” directive in JSP to refer to the tag library Call the tag by its name as defined in TLD If tag name defined in TLD is hello then we write Behind the Scenes Container calls the appropriate Tag Handler Tag Handler will write the appropriate response back to the page

22 Building Simple Tag

23 A simple tag that displays “Hello World” Approach Extend Tag Handler class from SimpleTagSupport class Override doTag() method Build TLD file Deploy

24 Example Code Building Simple Tag (displays “Hello World” )

25 Building Tag with Attributes

26 Building Tags with Attributes E.g. To handle attributes, need to add Instance variables and Corresponding setter methods Container call these setter methods Passed the custom tag attribute’s value as arguments

27 Building Tag with Attribute (cont.) Example: We will modify our Course Outline Example to incorporate tags Based on attribute value, tag will display the respective course outline

28 Example Code Building Tag with Attribute (Modifying CourseOutline Example)


Download ppt "JSP Tag Libraries Lec - 38. Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp."

Similar presentations


Ads by Google