• Post Reply 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

import directives for taglib classes?

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been working with a few taglib examples, and have come upon a point of confusion.
Whenever I run the examples, as given, my jsp page will not compile unless I import the classes that import the tags I use on the page. This is not exactly a surprise since the generated servlet code won't magically import those classes.
However, all the example code fails to do this. I am wondering if I am missing something here.
The taglib is declared in web.xml, its tld is located in WEB-INF and supporting classes are in WEB-INF/classes. The webapp has access to the classes (when I put the <%@ page import="SomeTagClass"> directives in the jsp page, it works).
Could it be that the examples just failed to mention that the imports are required in the .jsp pages?
 
Author
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Pelletier:
I've been working with a few taglib examples, and have come upon a point of confusion.
Whenever I run the examples, as given, my jsp page will not compile unless I import the classes that import the tags I use on the page. This is not exactly a surprise since the generated servlet code won't magically import those classes.
However, all the example code fails to do this. I am wondering if I am missing something here.
The taglib is declared in web.xml, its tld is located in WEB-INF and supporting classes are in WEB-INF/classes. The webapp has access to the classes (when I put the <%@ page import="SomeTagClass"> directives in the jsp page, it works).
Could it be that the examples just failed to mention that the imports are required in the .jsp pages?


The <%@ page import %> directives are not required on a properly functioning JSP container. You simply need to import the tag library with a <%@ taglib %> directive.
If it fails for you, please respond with (a) the name and version of the container you're using; (b) a sample JSP page that fails; and (c) the error message it gives you when it fails. That'd help us help you track down the problem.
Best,
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, so the container *should* locate the classes from the .tld and make the proper imports in the gen'd servlet for the .jsp page.
I'm using Tomcat 4.0.1, same result on RH7 Linux as on Mac OS X 10.1.5.
Here's the error message:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 10 in the jsp file: /javascript.jsp
Generated servlet error:
/usr/local/downloads/jakarta/jakarta-tomcat-4.0.1/work/localhost/TaglibTest/javascript$jsp.java:63: Class org.apache.jsp.JavaScriptExampleTag not found.
JavaScriptExampleTag _jspx_th_js_message_0 = new JavaScriptExampleTag();
^
This is based on a simple example from Wrox's Pro JSP book.
The JavaScriptExampleTag.class is indeed inside WEB-INF/classes, and the .tld is in WEB-INF.
Without an import directive, the above error results. With an import directive, the error is avoided, tag works as expected.
The jsp code is (import commented out here):
<%-- <%@ page import="JavaScriptExampleTag" %> --%>
<%@ taglib uri="http://www.mydomain.com/jspkit/javascript" prefix="js" %>
<html>
<head>
</head>
<body>
<p>This is a simple javascript tag test page</p>
<js:message>
This is the body of the js:message tag
</js:message>
</body>
</html>
 
Shawn Bayern
Author
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. If you add packages to the classes (both actually and in the TLD), it'll work fine.
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply.
I'd somehow never written a tag support class that wasn't in a package before, but admit I never knew it was a requirement that they be packaged.
I did notice that the container was trying to instantiate org.apache.jsp.MyTagClass, which puzzled me a bit.
I presume the container uses root level (unpackaged) tag support classes as a special case and considers them to be part of the org.apache.jsp package (?)
Any enlightening explanation would be appreciated.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It appears that when classes not in packages are encountered, the servlet JVM inevitably looks in the wrong place. Just bite the bullet and always use packages when working with servlets.
Bill
 
Ken Pelletier
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, William.
I normally would organize tag support classes into packages, but just stumbled on this when I coded a quick-and-dirty test w/o packages.
Thanks for the replies. Sorted.
reply
    Bookmark Topic Watch Topic
  • New Topic