Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

JSP Custom Tags beginner questions...

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
gen228
Asked:
gen228
  • 3
  • 2
2 Solutions
 
MogalManicCommented:
I think the URI needs to be specified in the taglib.
0
 
siliconeagleCommented:
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
 
gen228Author Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gen228Author Commented:
Thanks again for your comments and answers !

G.
0
 
siliconeagleCommented:
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
 
gen228Author Commented:
Thank you siliconeagle, I found out that it sets it based upon the tag attribute name that I create.

Thanks again,
G.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now