Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I add custom tag libraries in a JSP page?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Do I need to add it in the configuration - libraries from the Tools part of IDE or the location at the top of the JSP page?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You need a taglib directive at the top of each JSP page that will use the taglib. This directive identifies the URI of the taglib (which must be stored somewhere the container can find it) and associates it with a tag prefix.
Under some circumstances, you may need to define the taglib in web.xml.
[ March 09, 2004: Message edited by: Bear Bibeault ]
 
Author
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Bear Bibeault:
You need a taglib directive at the top of each JSP page that will use the taglib. This directive identifies the URI of the taglib (which must be stored somewhere the container can find it) and associates it with a tag prefix.
Under some circumstances, you may need to define the taglib in web.xml.
[ March 09, 2004: Message edited by: Bear Bibeault ]


Adding a bit to this, "stored somewhere the container can find it" should be "in the WEB-INF/lib directory if the tag library descriptor (TLD) is bundled in a JAR file along with all the class files". During development of a tag library, it may make sense to keep the TLD file directly in a subdirectory of the WEB-INF directory and the tag handler class files in WEB-INF/classes. But if you're asking about an existing tag library, just dump the library's JAR files in WEB-INF/lib and include taglib directives with the appropriate "uri" attribute value (see the docs for the library) in the JSP pages that use the library.
Unless you're using a old pre-JSP 1.2 container, you should never declare the tag library in the web.xml file; at best it's just redundant, at worst it may confuse the container and lead to "This absolute uri cannot be resolved" errors.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks for filling in some of the blanks, Hans. I had to pen that fairly quickly.
With regards to .tld files under WEB-INF: I've had some trouble getting Tomcat 4 to "hook up" to such files without an entry in the deployment descriptor; though it had no problems hooking up to jarred tlds. Tomcat 5 seems to have no such problems.
(Just in case someone bumps up against the same issue).
 
Al Kaiser
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks for the information. Is taglib.tld file is different for each custom JSP files containg tags?
Also, Is the follwing code legal. I'm getting error on this even if I register the taglib containg in the JAR file:
(...)
</title>
</head>
<body>
<c:setFeed url="http://www2.theserverside.com/rss/theserverside-1.0.rdf" id="A"/>
<p>Title:<c:getChannelTitle feedId="A"/>
<p>Description:<c:getChannelDescription feedId="A"/>
<c:forEachItem feedId="A">
Link:<%=itemLink%>
(..)
the error in the output:
x Error(12): c:setFeed is not a registered tag in that namespace.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You should also *import* the JSTL tag library into the JSP with the taglib directive.

Nick.
[ March 09, 2004: Message edited by: Nicholas Cheung ]
 
Hans Bergsten
Author
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Al Kaiser:
Thanks for the information. Is taglib.tld file is different for each custom JSP files containg tags?
Also, Is the follwing code legal. I'm getting error on this even if I register the taglib containg in the JAR file:
(...)
</title>
</head>
<body>
<c:setFeed url="http://www2.theserverside.com/rss/theserverside-1.0.rdf" id="A"/>
<p>Title:<c:getChannelTitle feedId="A"/>
<p>Description:<c:getChannelDescription feedId="A"/>
<c:forEachItem feedId="A">
Link:<%=itemLink%>
(..)
the error in the output:
x Error(12): c:setFeed is not a registered tag in that namespace.


One TLD (i.e., the content of a file with a .tld) describes the content of one tag library, which may contain one or many custom tags (or "custom actions", which is the formal name). You must use a taglib directive in each page that uses the taglibrary to map a namespace prefix to a TLD through the "uri" attribute value, e.g.,

where "mytaglib" is the default URI for the library declared by the <uri> element in the TLD and "my" is the namespace prefix you use for tags from this library in the page.
 
Hans Bergsten
Author
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Nicholas Cheung:
You should also *import* the JSTL tag library into the JSP with the taglib directive.

Nick.
[ March 09, 2004: Message edited by: Nicholas Cheung ]


Actually, this taglib directive only works if you have the TLD in a file name WEB-INF/c.tld, and there's no reason to put it there because it's already included in the standard.jar file (in you use the reference implementation) in WEB-INF/lib.
You should always use the default URI for the JSTL libraries, e.g.:

for JSTL 1.0 or

for JSTL 1.1.
Not only is it easier to install (just put the JAR files in WEB-INF/lib), but it's also the only way for the container to realize that you're using JSTL so it can optimize the code it generates for the page. Some containers can, for instance, generate a simple if statement for the <c:if> action if it knows that it's a JSTL action instead of the more complex code for creating a tag handler instance, configuring it with attribute values and invoking its doXXX() methods. This type of optimization is only enabled when you use the standard URIs for the JSTL libraries.
 
Al Kaiser
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Might be related to my previous questions, I'm getting error in the following page. Should I place the JAR's and .class files in the same basket - which is ..\web-inf\lib. Please let me know?
---
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<br><b>Quotes page</b>
<%@ page language="java" import="Request" %>
<jsp:useBean id="quote" class="Request" scope="page" />
<%
String s="";
java.util.Vector v;
(....)
Error(5): Unable to find class for bean: quote defined by tag with class: Request
 
Al Kaiser
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hans,
When I add the ...\jstl\core lib tag, I get the following error:
x Error(2): "http://java.sun.com/jsp/jstl/core" is not a registered TLD namespace.
x Error(6): Unable to find class for bean: quote defined by tag with class: Request
Does that mean I need to download those core libraries? From where though?
Al

Originally posted by Hans Bergsten:

Nick.
[ March 09, 2004: Message edited by: Nicholas Cheung ]<hr></blockquote>
Actually, this taglib directive only works if you have the TLD in a file name WEB-INF/c.tld, and there's no reason to put it there because it's already included in the standard.jar file (in you use the reference implementation) in WEB-INF/lib.
You should always use the default URI for the JSTL libraries, e.g.:

for JSTL 1.0 or

for JSTL 1.1.
Not only is it easier to install (just put the JAR files in WEB-INF/lib), but it's also the only way for the container to realize that you're using JSTL so it can optimize the code it generates for the page. Some containers can, for instance, generate a simple if statement for the <c:if> action if it knows that it's a JSTL action instead of the more complex code for creating a tag handler instance, configuring it with attribute values and invoking its doXXX() methods. This type of optimization is only enabled when you use the standard URIs for the JSTL libraries.[/QB]

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

When I add the ...\jstl\core lib tag, I get the following error:
x Error(2): "http://java.sun.com/jsp/jstl/core" is not a registered TLD namespace.
x Error(6): Unable to find class for bean: quote defined by tag with class: Request
Does that mean I need to download those core libraries? From where though?


JSTL 1.1 is included in the Java Web Services Developer Pack and is available here.
JSTL 1.0 used to be here but the link does not seem to function right now.
 
Hans Bergsten
Author
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Al Kaiser:
Might be related to my previous questions, I'm getting error in the following page. Should I place the JAR's and .class files in the same basket - which is ..\web-inf\lib. Please let me know?
---
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
<br><b>Quotes page</b>
<%@ page language="java" import="Request" %>
<jsp:useBean id="quote" class="Request" scope="page" />
<%
String s="";
java.util.Vector v;
(....)
Error(5): Unable to find class for bean: quote defined by tag with class: Request



No, this has nothing to do with tag library declarations. Most likely you're using a Java 1.4.x SDK (or later) and you must therefore use packages for all your beans to not confuse the Java compiler (details about this in my book). Earlier versions of the SDK incorrectly allowed beans in the default (unnamed) package to be used in JSP pages, but from 1.4 and on, it's no longer possible.
So, add a "package" statement to your bean class (e.g., "package com.mycompany.beans;"), place the class file in a matching WEB-INF/classes subdirectory (e.g., WEB-INF/classes/com/mycompany/beans), and use the fully qualified class name in the <jsp:useBean> action's "class" attribute (e.g., "com.mycompany.beans.Request"). If you refer to this class in scriptlet code, you must also fix the import; otherwise you can drop it.
As for where to put things, JAR files go in WEB-INF/lib and class files that are not in JAR files go in WEB-INF/classes.
 
Hans Bergsten
Author
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Al Kaiser:
[QB]Hans,
When I add the ...\jstl\core lib tag, I get the following error:
x Error(2): "http://java.sun.com/jsp/jstl/core" is not a registered TLD namespace.
x Error(6): Unable to find class for bean: quote defined by tag with class: Request
Does that mean I need to download those core libraries? From where though?
Al


It depends on which JSP container you use, but for most you have to install the JSTL JAR files in WEB-INF/lib for your application.
In addition to the locations where you can get the JSTL reference implementation posted by Zach Lee, you can also get it from the Apache Taglibs project that creates both the JSTL 1.0 and 1.1 versions. They go under the names "Standard 1.0" and "Standard 1.1":
http://jakarta.apache.org/taglibs/
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Hans,


Earlier versions of the SDK incorrectly allowed beans in the default (unnamed) package to be used in JSP pages, but from 1.4 and on, it's no longer possible.


I remember so posts before, saying that classes in default package do not work, while they work when they are in non-default packages.
That is to say, if I use JDK 1.3.x together with Tomcat 4 or 5, it will still work, but if I use JDK 1.4.x, together with Tomcat 4, it will not work?
Nick.
 
Hans Bergsten
Author
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Nicholas Cheung:
Hi Hans,

I remember so posts before, saying that classes in default package do not work, while they work when they are in non-default packages.
That is to say, if I use JDK 1.3.x together with Tomcat 4 or 5, it will still work, but if I use JDK 1.4.x, together with Tomcat 4, it will not work?
Nick.


Unless my memory regarding the JDK version where this was changed fails me, you're assumption is correct, at least for Tomcat 4 (Tomcat 5 implements JSP 2.0, and JSP 2.0 explicitly requires beans to be in a declared package).
 
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Oh! I guess people out here forget about the sample chapter available, which is exacty about the issue we are discussing here... Adding custom tag libraries in a JSP page is covered in details in later sections of the chapter...
Al Kaiser, I hope if you do step by step as specified in the sample chapter, I think you will expert in it soon... I can say that that chapter is really good in explanation about the issue here as well as the location to place the TLD files and tag files as well...
Hope this helps...
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hans
I am getting the following error
OracleJSP: oracle.jsp.parse.JspParseException: /Simple.jsp: Line # 3, <%@ taglib prefix="cc" uri="mytaglib" %>
Error: java.io.FileNotFoundException: /opt/ias/product/10.1.3/j2ee/home/applications/CustomComponent/CustomComponent/mytaglib (No such file or directory)

I am developing JSF Custom Component. The tld file is in the directory example\taglib. I cant put it in WEB-INF directory

Regards
Bansi
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Bansilal, this topic is over 2 1/2 years old. Please start a new topic describing your problem, and be sure to let us know what versions of JSP and such that you are using as per forum instructions,
 
    Bookmark Topic Watch Topic
  • New Topic