Solved

JSP Custom Tags beginner questions...

Posted on 2004-10-29
247 Views
Last Modified: 2013-11-24
I have just started learning about creating JSP Custom Tags, and I have a few questions:
(All questions refer to code below that I have been practicing with)

1. Who or which file calls the "setName" method?

2. Is setName a api method or could I also name it something like setTesting123?

3. Does the order of tags matter inside the tld file?

4. I am currently getting this error below with the provided files below, do you know why?
[Fri Oct 29 15:20:25 EDT 2004] The top-level uri tag is empty.
[Fri Oct 29 15:20:25 EDT 2004] JSP10Servlet: TLD attribute element must have a name element.
[Fri Oct 29 15:20:25 EDT 2004] JSP10Servlet: the problem occurred around line number 1:
[Fri Oct 29 15:20:25 EDT 2004] JSP10Servlet: [<%@ taglib uri="myTags" prefix="first" %>]


5. I am using Servlet Exec as my Application Server.
******* Code Below ********
Thank you for all your help !
G.

**** 2 jsp files, Hello and HelloTagParam.jsp *********

Hello.jsp

<%@ taglib uri="myTags" prefix="first"%>
<HTML>
<HEAD>
<TITLE>First Tag</TITLE>
</HEAD>

<BODY>

<B>My FFirst tag prints this </B>:

<first:hello/>

</BODY>
</HTML>


HelloTagParam.jsp
<%@ taglib uri="myTags" prefix="first" %>
<HTML>
<HEAD>
<TITLE>Hello Tag with Parameter</TITLE>
</HEAD>

<BODY>
<B>My parameterized tag prints</B>:

<P>

<first:helloparam name="JavaDuke"/>

</BODY>
</HTML>

************ TLD ***********************
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!-- a tag library descriptor -->

<taglib>
   <tlibversion>1.0</tlibversion>
   <jspversion>1.1</jspversion>
   <shortname>first</shortname>
   <uri></uri>
   <info>A simple tab library for the examples</info>

  <tag>
    <name>hello</name>
    <tagclass>tags.HelloTag</tagclass>
    <bodycontent>empty</bodycontent>
    <info>Say Hi</info>
  </tag>  
 
  <tag>
    <name>helloparam</name>
    <tagclass>tags.HelloTagParam</tagclass>
    <bodycontent>empty</bodycontent>
    <info>Tag with Parameter</info>
    <attribute>
      <testing>testing</testing>
      <required>false</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag>

</taglib>

****** Java classes: HelloTag.java and HelloTagParam.java ******

//HelloTag.java
package tags;

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

public class HelloTag extends TagSupport {

   public int doStartTag() throws JspException {
      try {
         pageContext.getOut().print("This is my first tag!");
      } catch (IOException ioe) {
         throw new JspException("Error:IOException while writing to client" + ioe.getMessage());
      }
      return SKIP_BODY;
   }

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


//HelloTagParam.java
package tags;

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

public class HelloTagParam extends TagSupport {
   private String name;

   
   public void setName(String name) {
      this.name = name;
   }
   

   public int doStartTag() throws JspException {
      try {
         pageContext.getOut().print("*** *** Welcome to JSP Tag Programming, " +name);
      } catch (IOException ioe) {
         throw new JspException("Error:IOException while writing to client");
      }
      return SKIP_BODY;
   }

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



0
Question by:gen228
    6 Comments
     
    LVL 21

    Assisted Solution

    by:MogalManic
    I think the URI needs to be specified in the taglib.
    0
     
    LVL 2

    Accepted Solution

    by:
    you need to define that taglib at the end of your web.xml
     <taglib>
      <taglib-uri>/WEB-INF/myTags</taglib-uri>
      <taglib-location>/WEB-INF/myTags.tld</taglib-location>
     </taglib>

    in this case you tag library descriptor (tld) file is called myTags.tld as is located in your WWEB-INF directory. to use your tag lib in a JSP use :-
    <%@ taglib uri="/WEB-INF/myTags" prefix="first" %>
    ...
    <first:hello/>
    ...
    0
     

    Author Comment

    by:gen228
    Thank you for your comments MogalManic and siliconeagle !  I found out that my problem had to do with the actual tld file itself, I created a new tld file and somehow it worked.  Can someone answer these questions:

    1. Who or which file calls the "setName" method?

    2. Is setName a api method or could I also name it something like setTesting123?

    3. Does the order of tags matter inside the tld file?

    Thank you so much !
    G.
    0
     

    Author Comment

    by:gen228
    Thanks again for your comments and answers !

    G.
    0
     
    LVL 2

    Expert Comment

    by:siliconeagle
    1. its called from the java class that is generated from the JSP the actual file will be in the work directory of the server.

    2. setName is declared in you Tag Class "HelloTagParam.java" it is intended that you use these metohds to get the data input for the name parameter of your tag.

    3. No
    0
     

    Author Comment

    by:gen228
    Thank you siliconeagle, I found out that it sets it based upon the tag attribute name that I create.

    Thanks again,
    G.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    fizzArray3  challenge 1 40
    squareUp  challenge 22 67
    mergeTwo  challenge 13 42
    Microsoft Edge 9 47
    Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now