I am creating Hello World program of custom Tag. for this, I have developed following files:
1. Tag Handler: HelloTag.java
package coreservlets.tags;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class HelloTag extends TagSupport {
public int doStartTag() {
try {
JspWriter out = pageContext.getOut();
out.print("Custom tag example " +
"(coreservlets.tags.ExampleTag)");
} catch(IOException ioe) {
System.out.println("Error in ExampleTag: " + ioe);
}
return(SKIP_BODY);
}
}
2. Tag Library Descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<short-name>mytld</short-name>
<uri>/WEB-INF/tags/mytld.tld</uri>
<!-- A validator verifies that the tags are used correctly at
JSP translation time. Validator entries look like this:
<validator>
<validator-class>com.mycompany.TagLibValidator</validator-class>
<init-param>
<param-name>parameter</param-name>
<param-value>value</param-value>
</init-param>
</validator>
-->
<!-- A tag library can register
Servlet Context event listeners in
case it needs to react to such events. Listener entries look
like this:
<listener>
<listener-class>com.mycompany.TagLibListener</listener-class>
</listener>
-->
<tag>
<name>myTag</name>
<tagclass>coreservlets.tags.HelloTag</tagclass>
<info>Simplest example: inserts one line of output</info>
<bodycontent>EMPTY</bodycontent>
</tag>
</taglib>
3. JSP file:
<%@ taglib tagdir="/WEB-INF/tags/mytld.tld" prefix="mytld" %>
<HTML>
<HEAD>
<TITLE>Tag Example</TITLE>
</HEAD>
<BODY>
<strong>JSTL Hello World Program </strong>
<H1><mytld:myTag />
</BODY>
</HTML>
Folder structure in
tomcat:
root
|
|--index.jsp
|--WEB-INF
|
|--classes
|
|-core-servlets
| |
| |--tags
| |
| |--HelloTag.class
|
|--tags
|
|-mytld.tld
Problem: requested resource is not available... what is the problem?
Please help me: I am in trouble from long time.
Thanks in advance.