Sunteți pe pagina 1din 6

12/29/13

JSP - Custom Tags

HOME

JAVA

PHP

Python

Ruby

Perl

HTML

CSS

Javascript

MySQL

C++

UNIX

MORE...

JSP - Custom Tags


Advertisements

Previous Page

Next Pag

Basic JSP Tutorial


JSP - Home JSP - Overview JSP - Environment JSP - Architecture JSP - Life Cycle JSP - Syntax JSP - Directives JSP - Actions JSP - Implicit Objects JSP - Client Request JSP - Server Response JSP - Http Codes JSP - Form Processing JSP - Writing Filters JSP - Cookies Handling JSP - Session Tracking JSP - File Uploading JSP - Handling Date JSP - Page Redirect JSP - Hits Counter JSP - Auto Refresh
www.tutorialspoint.com/jsp/jsp_custom_tags.htm

A custom tag is a user-defined JSP language element. When a JSP page containing a custo translated into a servlet, the tag is converted to operations on an object called a tag handler. T container then invokes those operations when the JSP page's servlet is executed.

JSP tag extensions let you create new tags that you can insert directly into a JavaServer Pag you would the built-in tags you learned about in earlier chapter. The JSP 2.0 specification in Simple Tag Handlers for writing these custom tags.

To write a customer tab you can simply extend SimpleTagSupport class and override the method, where you can place your code to generate content for the tag.

Create "Hello" Tag:


Consider you want to define a custom tag named <ex:Hello> and you want to use it in the fashion without a body: < e x : H e l l o/ >

To create a custom JSP tag, you must first create a Java class that acts as a tag handler. S create HelloTag class as follows: p a c k a g ec o m . t u t o r i a l s p o i n t ; i m p o r tj a v a x . s e r v l e t . j s p . t a g e x t . * ; i m p o r tj a v a x . s e r v l e t . j s p . * ; i m p o r tj a v a . i o . * ; p u b l i cc l a s sH e l l o T a ge x t e n d sS i m p l e T a g S u p p o r t{ p u b l i cv o i dd o T a g ( )t h r o w sJ s p E x c e p t i o n ,I O E x c e p t i o n{ J s p W r i t e ro u t=g e t J s p C o n t e x t ( ) . g e t O u t ( ) ; o u t . p r i n t l n ( " H e l l oC u s t o mT a g ! " ) ; } }

Above code has simple coding where doTag() method takes the current JspContext obje getJspContext() method and uses it to send "Hello Custom Tag!" to the current JspWriter objec
1/6

12/29/13

JSP - Custom Tags

JSP - Sending Email

Advanced JSP Tutorials


JSP - Standard Tag Library JSP - Database Access JSP - XML Data JSP - Java Beans

Let us compile above class and copy it in a directory available in environment variable CLAS Finally create following tag library file: <Tomcat-Installation-Directory>webapps\ROO INF\custom.tld. < t a g l i b > < t l i b v e r s i o n > 1 . 0 < / t l i b v e r s i o n > < j s p v e r s i o n > 2 . 0 < / j s p v e r s i o n > < s h o r t n a m e > E x a m p l eT L D < / s h o r t n a m e > < t a g > < n a m e > H e l l o < / n a m e > < t a g c l a s s > c o m . t u t o r i a l s p o i n t . H e l l o T a g < / t a g c l a s s > < b o d y c o n t e n t > e m p t y < / b o d y c o n t e n t > < / t a g > < / t a g l i b > Now it's time to use above defined custom tag Hello in our JSP program as follows: < % @t a g l i bp r e f i x = " e x "u r i = " W E B I N F / c u s t o m . t l d " % > < h t m l > < h e a d > < t i t l e > As a m p l ec u s t o mt a g < / t i t l e > < / h e a d > < b o d y > < e x : H e l l o / > < / b o d y > < / h t m l > Try to call above JSP and this should produce following result: H e l l oC u s t o mT a g !

JSP - Custom Tags


JSP - Expression Language JSP - Exception Handling JSP - Debugging JSP - Security JSP - Internationalization

JSP Useful Resources


JSP - Quick Guide JSP Useful Resources

Selected Reading
Developer's Best Practices Effective Resume Writing Computer Glossary Who is Who

Accessing the Tag Body:

You can include a message in the body of the tag as you have seen with standard tags. Cons want to define a custom tag named <ex:Hello> and you want to use it in the following fashio body: < e x : H e l l o > T h i si sm e s s a g eb o d y < / e x : H e l l o > Let us make following changes in above our tag code to process the body of the tag: p a c k a g ec o m . t u t o r i a l s p o i n t ; i m p o r tj a v a x . s e r v l e t . j s p . t a g e x t . * ; i m p o r tj a v a x . s e r v l e t . j s p . * ; i m p o r tj a v a . i o . * ; p u b l i cc l a s sH e l l o T a ge x t e n d sS i m p l e T a g S u p p o r t{ S t r i n g W r i t e rs w=n e wS t r i n g W r i t e r ( ) ; p u b l i cv o i dd o T a g ( ) t h r o w sJ s p E x c e p t i o n ,I O E x c e p t i o n { g e t J s p B o d y ( ) . i n v o k e ( s w ) ; g e t J s p C o n t e x t ( ) . g e t O u t ( ) . p r i n t l n ( s w . t o S t r i n g ( ) ) ;

www.tutorialspoint.com/jsp/jsp_custom_tags.htm

2/6

12/29/13

JSP - Custom Tags

} }

In this case, the output resulting from the invocation is first captured into a StringWriter befo written to the JspWriter associated with the tag. Now accordingly we need to change TLD follows: < t a g l i b > < t l i b v e r s i o n > 1 . 0 < / t l i b v e r s i o n > < j s p v e r s i o n > 2 . 0 < / j s p v e r s i o n > < s h o r t n a m e > E x a m p l eT L Dw i t hB o d y < / s h o r t n a m e > < t a g > < n a m e > H e l l o < / n a m e > < t a g c l a s s > c o m . t u t o r i a l s p o i n t . H e l l o T a g < / t a g c l a s s > < b o d y c o n t e n t > s c r i p t l e s s < / b o d y c o n t e n t > < / t a g > < / t a g l i b > Now let us call above tag with proper body as follows: < % @t a g l i bp r e f i x = " e x "u r i = " W E B I N F / c u s t o m . t l d " % > < h t m l > < h e a d > < t i t l e > As a m p l ec u s t o mt a g < / t i t l e > < / h e a d > < b o d y > < e x : H e l l o > T h i si sm e s s a g eb o d y < / e x : H e l l o > < / b o d y > < / h t m l > This will produce following result: T h i si sm e s s a g eb o d y

Custom Tag Attributes:

You can use various attributes along with your custom tags. To accept an attribute value, a cu class needs to implement setter methods, identical to JavaBean setter methods as shown bel p a c k a g ec o m . t u t o r i a l s p o i n t ; i m p o r tj a v a x . s e r v l e t . j s p . t a g e x t . * ; i m p o r tj a v a x . s e r v l e t . j s p . * ; i m p o r tj a v a . i o . * ; p u b l i cc l a s sH e l l o T a ge x t e n d sS i m p l e T a g S u p p o r t{ p r i v a t eS t r i n gm e s s a g e ; p u b l i cv o i ds e t M e s s a g e ( S t r i n gm s g ){ t h i s . m e s s a g e=m s g ; } S t r i n g W r i t e rs w=n e wS t r i n g W r i t e r ( ) ;
www.tutorialspoint.com/jsp/jsp_custom_tags.htm 3/6

12/29/13

JSP - Custom Tags

p u b l i cv o i dd o T a g ( ) t h r o w sJ s p E x c e p t i o n ,I O E x c e p t i o n { i f( m e s s a g e! =n u l l ){ / *U s em e s s a g ef r o ma t t r i b u t e* / J s p W r i t e ro u t=g e t J s p C o n t e x t ( ) . g e t O u t ( ) ; o u t . p r i n t l n (m e s s a g e) ; } e l s e{ / *u s em e s s a g ef r o mt h eb o d y* / g e t J s p B o d y ( ) . i n v o k e ( s w ) ; g e t J s p C o n t e x t ( ) . g e t O u t ( ) . p r i n t l n ( s w . t o S t r i n g ( ) ) ; } } }

The attribute's name is "message", so the setter method is setMessage(). Now let us add this in TLD file using <attribute> element as follows: < t a g l i b > < t l i b v e r s i o n > 1 . 0 < / t l i b v e r s i o n > < j s p v e r s i o n > 2 . 0 < / j s p v e r s i o n > < s h o r t n a m e > E x a m p l eT L Dw i t hB o d y < / s h o r t n a m e > < t a g > < n a m e > H e l l o < / n a m e > < t a g c l a s s > c o m . t u t o r i a l s p o i n t . H e l l o T a g < / t a g c l a s s > < b o d y c o n t e n t > s c r i p t l e s s < / b o d y c o n t e n t > < a t t r i b u t e > < n a m e > m e s s a g e < / n a m e > < / a t t r i b u t e > < / t a g > < / t a g l i b > Now let us try following JSP with message attribute as follows: < % @t a g l i bp r e f i x = " e x "u r i = " W E B I N F / c u s t o m . t l d " % > < h t m l > < h e a d > < t i t l e > As a m p l ec u s t o mt a g < / t i t l e > < / h e a d > < b o d y > < e x : H e l l om e s s a g e = " T h i si sc u s t o mt a g "/ > < / b o d y > < / h t m l > This will produce following result: T h i si sc u s t o mt a g Hope above example makes sense for you. It would be worth to note that you can include properties for an attribute: Property name
www.tutorialspoint.com/jsp/jsp_custom_tags.htm

Purpose

The name element defines the name of an attribute. Each attrib name must be unique for a particular tag.
4/6

12/29/13

JSP - Custom Tags

required rtexprvalue type description fragment

This specifies if this attribute is required or optional. It would be for optional. Declares if a runtime expression value for a tag attribute is vali Defines the Java class-type of this attribute. By default it is ass as String Informational description can be provided.

Declares if this attribute value should be treated as a JspFragm

Following is the example to specify properties related to an attribute: . . . . . < a t t r i b u t e > < n a m e > a t t r i b u t e _ n a m e < / n a m e > < r e q u i r e d > f a l s e < / r e q u i r e d > < t y p e > j a v a . u t i l . D a t e < / t y p e > < f r a g m e n t > f a l s e < / f r a g m e n t > < / a t t r i b u t e > . . . . . If you are using two attributes then you can modify your TLD as follows: . . . . . < a t t r i b u t e > < n a m e > a t t r i b u t e _ n a m e 1 < / n a m e > < r e q u i r e d > f a l s e < / r e q u i r e d > < t y p e > j a v a . u t i l . B o o l e a n < / t y p e > < f r a g m e n t > f a l s e < / f r a g m e n t > < / a t t r i b u t e > < a t t r i b u t e > < n a m e > a t t r i b u t e _ n a m e 2 < / n a m e > < r e q u i r e d > t r u e < / r e q u i r e d > < t y p e > j a v a . u t i l . D a t e < / t y p e > < / a t t r i b u t e > . . . . .

Previous Page
Advertisements

Print Version

PDF Version

Next Pag

www.tutorialspoint.com/jsp/jsp_custom_tags.htm

5/6

12/29/13

JSP - Custom Tags

JUDCon2014:India
theyounion.in/JUDCon/2014-india Attend JBoss User & Dev. Conference Network. Learn & Grow. Register Now

ASP.NET | jQuery | AJAX | ANT | JSP | Servlets | log4j | iBATIS | Hibernate | JDBC | Struts | HTML5

Copyright 2012 by tutorialspoint. All Rights Reserved.

www.tutorialspoint.com/jsp/jsp_custom_tags.htm

6/6

S-ar putea să vă placă și