Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JSP Custom Tags beginner questions...

Posted on 2004-10-29
6
Medium Priority
?
253 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
Comment
Question by:gen228
  • 3
  • 2
6 Comments
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 160 total points
ID: 12456507
I think the URI needs to be specified in the taglib.
0
 
LVL 2

Accepted Solution

by:
siliconeagle earned 240 total points
ID: 12461623
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
ID: 12464035
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:gen228
ID: 12478398
Thanks again for your comments and answers !

G.
0
 
LVL 2

Expert Comment

by:siliconeagle
ID: 12481466
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
ID: 12486522
Thank you siliconeagle, I found out that it sets it based upon the tag attribute name that I create.

Thanks again,
G.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

578 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