[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JSP custom tag issues

Posted on 2012-08-10
4
Medium Priority
?
916 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:gudii9
  • 2
4 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 38281523
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 38281570
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
0
 
LVL 36

Accepted Solution

by:
mccarl earned 1000 total points
ID: 38283021
Post your code for your CalculatorTag class. You post the code for some Calculator class but I don't see how that class is involved/used at all?
0
 
LVL 11

Assisted Solution

by:Manish
Manish earned 1000 total points
ID: 38283146
Post the error in console,
eclipse error not always correct,
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question