How to make custom tag work
The test.jsp works fine in my custom folder "tomcat-main-dir/webapps/lei".
Now I created a custom tag handler called "SimpleTag.java" and a mytaglib.tld file.
I put "SimpleTag.class" and mytaglib.tld in the "tomcat-main-dir/webapps/lei". When I run the .jsp, I got the following error msg:
root cause
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: -1 in the jsp file: null
Generated servlet error:
[javac] Compiling 1 source file
C:\jakarta-tomcat-4.1.7\work\Standalone\localhost\lei\test$jsp.java:79: cannot resolve symbol
symbol : class SimpleTag
location: class org.apache.jsp.test$jsp
SimpleTag _jspx_th_mytag_getSimpleTag_0 = (SimpleTag) _jspx_tagPool_mytag_getSimpleTag.get(SimpleTag.class);
^
C:\jakarta-tomcat-4.1.7\work\Standalone\localhost\lei\test$jsp.java:79: cannot resolve symbol
symbol : class SimpleTag
location: class org.apache.jsp.test$jsp
SimpleTag _jspx_th_mytag_getSimpleTag_0 = (SimpleTag) _jspx_tagPool_mytag_getSimpleTag.get(SimpleTag.class);
^
C:\jakarta-tomcat-4.1.7\work\Standalone\localhost\lei\test$jsp.java:79: cannot resolve symbol
symbol : class SimpleTag
location: class org.apache.jsp.test$jsp
SimpleTag _jspx_th_mytag_getSimpleTag_0 = (SimpleTag) _jspx_tagPool_mytag_getSimpleTag.get(SimpleTag.class);
mytaglib.tld is
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>ToMytaglib</shortname>
<info>My first tag library descriptor file</info>
<tag>
<name>getSimpleTag</name>
<tagclass>SimpleTag</tagclass>
</tag>
</taglib>
and SimpleTag.java is
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
class SimpleTag extends TagSupport {
public int doStartTag() throws JspException {
try {
pageContext.getOut().print("Welcome to my Web site.");
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
}
How to make it work?
Any advice is appreciated.
Thanks,
The problem seems that the .class was put in a wrong place. But where should it go?
The following is complete info about the problem. Any advices are really appreciated.
Structure: /webapps/lei/WEB-INF/classes/
test.jsp is in /lei
mytaglib.tld is in /WEB-INF
SimpleTag.class is in /classes
****** test.jsp file ******
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ taglib uri="/WEB-INF/mytaglib.tld" prefix="mytag" %>
<html>
<head>
<title>Untitled</title>
</head>
<body>
Hello.
<mytag:getSimpleTag />
</body>
</html>
****** mytaglib.tld ******
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.2</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>MyFirstTag</short-name>
<tag>
<name>getSimpleTag</name>
<tag-class>SimpleTag</tag-class>
</tag>
</taglib>
****** SimpleTag.java ******
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class SimpleTag extends TagSupport {
public int doStartTag() throws JspException {
try {
pageContext.getOut().print("Welcome to my Web site.");
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
}
When requesting "http://localhost:8080/lei/test.jsp", got the following error:
root cause
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: -1 in the jsp file: null
Generated servlet error:
[javac] Compiling 1 source file
C:\jakarta-tomcat-4.1.7\work\Standalone\localhost\lei\test$jsp.java:80: cannot resolve symbol
symbol : class SimpleTag
location: class org.apache.jsp.test$jsp
SimpleTag _jspx_th_mytag_getSimpleTag_0 = (SimpleTag) _jspx_tagPool_mytag_getSimpleTag.get(SimpleTag.class);
^
C:\jakarta-tomcat-4.1.7\work\Standalone\localhost\lei\test$jsp.java:80: cannot resolve symbol
symbol : class SimpleTag
location: class org.apache.jsp.test$jsp
SimpleTag _jspx_th_mytag_getSimpleTag_0 = (SimpleTag) _jspx_tagPool_mytag_getSimpleTag.get(SimpleTag.class);
^
C:\jakarta-tomcat-4.1.7\work\Standalone\localhost\lei\test$jsp.java:80: cannot resolve symbol
symbol : class SimpleTag
location: class org.apache.jsp.test$jsp
SimpleTag _jspx_th_mytag_getSimpleTag_0 = (SimpleTag) _jspx_tagPool_mytag_getSimpleTag.get(SimpleTag.class);
^
3 errors
Thanks,