0

I am using a custom Tree Tag library to display hierarchical data by using the JSP taglib mechanism. My Tomcat setup has successfully deployed other webs apps including JSP & servlet samples, but it's giving me issues with this tag library. I have found this very same problem in a number of places but without responses. Any help is appreciated. Thanks in advance.

The tag library is declared as follows:


 1:     &lt%@ taglib uri="WEB-INF/treetag.tld" prefix="tree" %&gt

I've reduced my usage of this library to a single line for debugging purposes and that's the code at line 20. Here's the error message:


org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 20 in the jsp file: /hello.jsp
ITree cannot be resolved to a type
20:     &lt% ITree tree = new Tree(); %&gt

From the above error, it looks like the server cannot find the library (jenkov-prizetags-3.4.0.jar) that I placed at webapp/WEB-INF/lib. I have unpacked the jar file and found the very same classes that the server cannot find, so I am inclined to think that it is just not looking in the webapp/WEB-INF/lib. Am I missing something here?

Programming Environment:

  • Tomcat 6&7 - same problem.
  • Java SDK 6
Mugabo
  • 753
  • 7
  • 13

1 Answers1

1

Taglibs and scriptlets doesn't work together. You normally use the one or the other, not both.

As to the compilation error in your scriptlet, you just need to import the ITree class like as you would do in a normal Java class.

<%@page import="com.example.ITree" %>

Or, better yet, don't use scriptlets at all. That Java code belongs in a Java class, not a JSP file.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the tip. I just started playing around with Tomcat and JSPs and, on top of that, I'm using a custom taglib. I'd figured the tag lib descriptor file (*.tld), which ties the tags to their classes, would do the importing. But if what you say is true, then it would explain the unexplainable errors I have been getting. – Mugabo Feb 03 '11 at 17:32
  • I warmly recommend to read tutorials/books which are not written before 2001, when JSP 2.0 was introduced. Start here: http://stackoverflow.com/tags/jsp/info – BalusC Feb 03 '11 at 17:35
  • Thanks, I imported the necessary libraries and the problem was solved. I need to read more on why scriptlets are discouraged, but they'll do for my dev phase. If I follow correctly, all the code would go into a Java class which I would then import? Thanks. – Mugabo Feb 03 '11 at 18:49
  • That kind of job should be done by a servlet, in the `doGet()` method. To learn more about servlets, check our servlets wiki page: [stackoverflow.com/tags/servlets/info](http://stackoverflow.com/tags/servlets/info). To learn about discouragement of scriptlets, read [this answer](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202), it comes with examples of normal approaches as well. – BalusC Feb 03 '11 at 18:51
  • @BalusC So I completely got rid of scriplets in my jsp file storing the Tree Model written in pure Java code in a servlet. To construct the tree model, however, I have to invoke the servlet and then access my jsp page. How can I get the jsp to invoke the servlet? Without using scriplets, of course. I felt this was too much of a follow up to be a stand-alone question. – Mugabo Feb 09 '11 at 17:25
  • @BalusC Nevermind, looks like you've actually answered the same exact [question](http://stackoverflow.com/q/3590961/558732) before. – Mugabo Feb 09 '11 at 17:32
  • Yes, it's covered by the two links as well which I posted in my previous comment. – BalusC Feb 09 '11 at 17:47