Unable to load tag handler class

7.7k Views Asked by At

I have this class inside tag pachage that it's used to tld file

package tag;

import java.io.IOException;
import java.time.LocalDate;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;


public class printDateTag extends TagSupport{

@Override
public int doStartTag() throws JspException {
    try {
        JspWriter writer = pageContext.getOut();
        writer.print(LocalDate.now());
    } catch (IOException ex) {System.out.println(ex);}
    return SKIP_BODY;
}

}

My tld file that uses that class is this

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee          
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>tags</short-name>
<uri>/WEB-INF/tlds/tags</uri>


<tag>
    <name>printDate</name>
    <tag-class>tag.printDateTag</tag-class>
</tag>

And the jsp where i have the error is this

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ta" uri="/WEB-INF/tlds/tags.tld"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <ta:printDate/>
    <a href="index">go</a>
</body>
</html>

Here in <ta:printDate/> i have the error unable to load tag handler class "tag.printDateTag" for tag "ta:printDate

1

There are 1 best solutions below

1
On BEST ANSWER

AS per seen your code,

looks like your tld file is not properly configure,

see below code which i have tried ,

printDateTag.java

package tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class printDateTag extends TagSupport{
    @Override
    public int doStartTag() throws JspException {
        try {
            JspWriter writer = pageContext.getOut();
            writer.print("<u>Hello From Tag</u>"); //  <u>Hello From Tag</u>... it will display on JSP page
        } catch (IOException ex) {System.out.println(ex);}
        return SKIP_BODY;
    }
}

tags.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/j2ee/dtd/web-jsptaglibrary_1_2.dtd">  

<taglib>  

  <tlib-version>1.0</tlib-version>  
  <jsp-version>1.2</jsp-version>  
  <short-name>simple</short-name>  
 <!--  <uri>http://tomcat.apache.org/example-taglib</uri> -->  
  <uri>/WEB-INF/tlds/tags</uri>
<tag>
    <name>printDate</name>
    <tag-class>tag.printDateTag</tag-class>
</tag> 

</taglib>  

test.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="ta" uri="/WEB-INF/tlds/tags.tld"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <ta:printDate/>
    <a href="index">go</a>
</body>
</html>

Output :

enter image description here