Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

JSP custom tag issues

Hi,

I was trying JSP Custom Tag example. In my JSP CustomTag is not recognized some reason. Do I need to configure something in web.xml as well. Please check the code, screenshots of the project structure, error.
CustomTag.jsp. tagclass in mytag.tld I removed customtags(since CalculatorTag is directly under default package for me) still did not work. Please advise on how can I make it work.

Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance
<%@ taglib uri="/WEB-INF/mytags.tld" prefix="calc"%>
<html>
 <head>
  <title> Scripting Variable in JSP</title>
   <style>
  h1 { font-family:Comic Sans MS; font-size:15pt; }
   </style>
  </head>
<body>
  <calc:sum count="100"/>
</body>
</html>



mytas.tld



<?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/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
      <tlibversion>1.0</tlibversion>
      <jspversion>1.1</jspversion>
      <shortname>examples</shortname>
      <info>Simple Library</info>
      <tag>
            <name>sum</name>
            <tagclass>customtags.CalculatorTag</tagclass>
            <bodycontent>JSP</bodycontent>
            <attribute>
                  <name>count</name>
                  <required>true</required>
                  <rtexprvalue>false</rtexprvalue>
            </attribute>
      </tag>
</taglib>



Calculator Bean class


package beans;
public class Calculator {
      String count;
      String sum;
      public String getCount() {
            return count;
      }
      public void setCount(String count) {
            this.count = count;
      }
      public String getSum() {
            int s = 0;
            int maxCount = Integer.parseInt(count);
            for (int i = 1; i <= maxCount; i++) {
                  s += i;
            }
            sum = s + "";
            return sum;
      }
      public void setSum(String sum) {
            this.sum = sum;
      }
}





WEB.xml looks like


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Hello1</servlet-name>
    <servlet-class>LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello1</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Order</servlet-name>
    <servlet-class>OrderProcessingServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Order</servlet-name>
    <url-pattern>/OrderProcessingServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>billing</servlet-name>
    <servlet-class>BillingServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>billing</servlet-name>
    <url-pattern>/BillingServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>Confirmation</servlet-name>
    <servlet-class>ConfirmationServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Confirmation</servlet-name>
    <url-pattern>/ConfirmationServlet</url-pattern>
  </servlet-mapping>
    <servlet>
    <servlet-name>Banner</servlet-name>
    <servlet-class>BannerServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Banner</servlet-name>
    <url-pattern>/BannerServlet</url-pattern>
  </servlet-mapping>
</web-app>

CustomTag.JPG
CustomTag2.JPG
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

I tried this example from this link as well


http://shivasoft.in/blog/java/how-to-create-jsp-custom-tag-%E2%80%93-using-tag-interface-or-tagsupport/

I am attaching same code and screen with project structure.
index.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  pageEncoding="ISO-8859-1"%>  

 <%@taglib uri="/WEB-INF/tlds/CustomTags.tld" prefix="ct"%>  

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

 <html>  

 <head>  

 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  

 <title>Custom Tag</title>  

 </head>  

 <body>  

 <h2>Custom tag to display date</h2>  

 <ct:ShowDate displayDate="true" />  

 </body>  

 </html> 






DisplayDate.java




package com.G2.CustomTag;  

   

 import java.io.IOException;  

 import java.text.SimpleDateFormat;  

 import java.util.Date;  

    

 import javax.servlet.jsp.JspException;  

 import javax.servlet.jsp.JspWriter;  
 import javax.servlet.jsp.PageContext;  

 import javax.servlet.jsp.tagext.Tag;  

    

 public class DisplayDate implements Tag {  

    

     private PageContext pc = null;  

     private Tag parent = null;  

     private boolean displayDate;  

    

     public boolean isDisplayDate() {  

         return displayDate;  

     }  

    

     public void setDisplayDate(boolean displayDate) {  

         this.displayDate = displayDate;  

     }  

    

     @Override 

     public int doEndTag() throws JspException {  

         return EVAL_PAGE;  

     }  

    

     @Override 

     public int doStartTag() throws JspException {  

         try {  

           JspWriter out = pc.getOut();  
             if (isDisplayDate()) {  

                 SimpleDateFormat frm = new SimpleDateFormat("dd-MMM-yyy EEEE");  

                 out.print(frm.format(new Date()));  

             } else {  

                 out.print("Custom Tag is not configured to display Date");  

             }  

         } catch (IOException e) {  

	throw new JspException(e);  

         }  

         return SKIP_BODY;  

     }  

    

     @Override 

     public Tag getParent() {  

    

         return parent;  

     }  

    

     @Override 

     public void release() {  

         pc = null;  

         parent = null;  

     }  

  

    @Override 

    public void setPageContext(PageContext arg0) {  

         pc = arg0;  

     }  

    

     @Override 

     public void setParent(Tag arg0) {  

         parent = arg0;  

    }  

    

 } 





CustomTags.tld






<?xml version="1.0" encoding="UTF-8"?>  

 <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">  

 <taglib>  

    <tlibversion>1.0.0</tlibversion>  

    <jspversion>1.1</jspversion>  

     <shortname>CustomTag</shortname>  

    <uri>http://shivasoft.in/blog</uri>  

     <info>Custom tags</info>  

    <tag>  

         <name>ShowDate</name>  

        <tagclass>com.G2.CustomTag.DisplayDate</tagclass>  

         <bodycontent>empty</bodycontent>  

         <info>Custom tag used to display the current date</info>  

         <attribute>  

             <name>displayDate</name>  

            <required>true</required>  

         </attribute>  

     </tag>  

 </taglib> 

Open in new window


This example aslo gave similar error saying cannot find tag library descriptor as in attachment.
tldError.JPG
Avatar of gudii9

ASKER

I tried one other similar example

http://www.roseindia.net/jsp/custom-tag.shtml


This works fine not sure what is the difference



JSP File

<%@ taglib uri="/WEB-INF/taglib.tld" prefix="custom"%>
<html>
<head>
<title>Custom Tag with no attribute</title>
</head>
<body bgcolor="ffffcc">
      <H1>Welcome !</H1>
      Custom tag work starts...
      <custom:empty />
      Custom tag work ends... Body Content of JSP page
</body>
</html>





taglib.tld


<?xml version="1.0" encoding="UTF-8"?>  

 <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> 

 <taglib>  

 <tlibversion>1.0</tlibversion>
       <jspversion>1.1</jspversion>
       <shortname>custom</shortname>  

    <tag>  

        <name>empty</name>
        <tagclass>mytag.emptyTag</tagclass>
        <bodycontent>empty</bodycontent>
        <info>Tag having no attribute and no body</info>

     </tag>  

 </taglib>










emptyTag.java





package mytag;

import java.io.*;
import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;

public class emptyTag implements Tag {
   private PageContext pageContext;
   private Tag parent;
   
   public emptyTag() {
      super();
   }

   public int doStartTag() throws JspException {
      try {
         pageContext.getOut().print("This is an empty tag!");
      } catch (IOException ioe) {
         throw new JspException("Error:"+ioe.getMessage());
      }
      return SKIP_BODY;
   }

   public int doEndTag() throws JspException {
      return SKIP_PAGE;
   }
   public void release() {
   }

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

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

   public Tag getParent() {
      return parent;
   }

}
EmptyCustomTag.JPG
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial