What are Custom tags. Why do you need Custom tags. How do you create Custom tag

Showing Answers 1 - 2 of 2 Answers

Harikrishna

  • May 26th, 2005
 

1) Custom tags are those which are user defined. 
2) Inorder to separate the presentation logic in a separate class rather than keeping in jsp page we canuse custom tags. 
3) 
Step 1 : Build a class that implements the javax.servlet.jsp.tagext.Tag  
interface as follows. Compile it and place it under the web-inf/classes  
directory (in the appropriate package structure).  
package examples; 
 
import java.io.*; //// THIS PROGRAM IS EVERY TIME I MEAN WHEN  
U REFRESH THAT PARTICULAR  
CURRENT DATE THIS CUSTOM TAG WILL DISPLAY 
 
import javax.servlet.jsp.*; 
import javax.servlet.jsp.tagext.*; 
 
public class ShowDateTag implements Tag { 
 
private PageContext pageContext; 
private Tag parent; 
 
public int doStartTag() throws JspException { 
return SKIP_BODY; 

 
public int doEndTag() throws JspException { 
try { 
pageContext.getOut().write("" + new java.util.Date()); 
} catch (IOException ioe) { 
throw new JspException(ioe.getMessage()); 

 
return EVAL_PAGE; 

 
public void release() { 

 
public void setPageContext(PageContext page) { 
this.pageContext = page; 

 
public void setParent(Tag tag) { 
this.parent = tag; 

 
public Tag getParent() { 
return this.parent; 

 

 
Step 2 : Now we need to describe the tag, so create a file called  
taglib.tld and place it under the web-inf directory.  
 
1.1//EN"  
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 
 
 
1.0 
1.1 
myTag 
http://www.mycompany.com/taglib 
My own tag library 
 
 
showDate 
examples.ShowDateTag 
Show the current date 
 
 
 
 
Step 3 : Now we need to tell the web application where to find the  
custom tags, and how they will be referenced from JSP pages. Edit the  
web.xml file under the web-inf directory and insert the following XML  
fragement.  
 
 
http://www.mycompany.com/taglib 
 
 
/WEB-INF/taglib.tld 
 
 
 
Step 4 : And finally, create a JSP page that uses the custom tag.  
 
 
Date tag example 
 
 
<%@ taglib uri="http://www.mycompany.com/taglib" prefix="myTag" %> 
 
 
 
 
 
Now restart the server and call up the JSP page! You should notice that  
every time the page is requested, the current date is displayed in the  
browser.  
 
Whilst this doesn't explain what all the various parts of the tag are  
for (e.g. the tag description, page context, etc) it should get you  
going. If you use the tutorial (above) and this example, you should be able  
to grasp what's going on!  
 
 

  Was this answer useful?  Yes

Chinna

  • Oct 3rd, 2005
 

Custom tags are User defined tags.The main reason to need custom tags is To eliminate the java code in Jsp's.i.e in case of java code we simple inser the custom tags.Exapmle:Data base Connection etc.....

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions