user46953
user46953

Reputation: 9

Can't import simple class/package into a simple JSP page using Apache Tomcat

Can someone please walk me through the process of loading a class or package in JSP with Tomcat?

I think it might just be a Tomcat setup issue :S my JSP file runs fine without importing or using dbpool or dbpooljar. I've tried many suggestions to other peoples similar issues without any luck. Any help would be apreciated!

My class/package placed in web-inf/classes (among other places)

package dbpooljar;
public class DBPool
{   
   public DBPool()
   {
     System.out.println("dbpool evidence!");
   }
 }

My compile commands

javac "C:\website\apache-tomcat-6.0.18\webapps\ROOT\WEB-INF\classes\dbpool.java"
jar cf "C:\website\apache-tomcat-6.0.18\webapps\ROOT\WEB-INF\classes\dbpooljar.jar" DBPool.java

My index.jsp

<%@ page import="java.sql.*,java.util.List,java.util.ArrayList,DBPool" %>

<html>
  <body>
    Getting Length of a String
    <%
        String s1 = "Length of a String!";
        out.println("\"" + s1 + "\"" + " is  of   "  +  s1.length() +   " characters ");
        DBPool test=new DBPool();
    %>
  </body>
</html>

And lastly my horrible error (among others when I've tried different things)

An error occurred at line: 9 in the generated java file
The import DBPool cannot be resolved

Upvotes: 0

Views: 12748

Answers (3)

Ken Bellows
Ken Bellows

Reputation: 6942

I know this question is old, but I figure I'll give my answer anyway.

The way Tomcat works now, at least, there should be two folders in your WEB-INF directory, one called classes and one called lib. From what I understand, .class files go in WEB-INF/classes, and .jar files go in WEB-INF/lib. Additionally, if you're going to declare your .class file to be in a package, it needs to be in an appropriate directory; in your case, this means it should be located at WEB-INF/classes/dbpooljar/DBPool.class.

Hope that helps someone.

Upvotes: 0

Milhous
Milhous

Reputation: 14653

I dont know about windows, but in linux there is a the file(/usr/share/tomcat5/conf/jkconfig.manifest) that you can edit to add specific jars to the tomcat instance that is running.

Upvotes: 0

Olaf Kock
Olaf Kock

Reputation: 48087

You've typed

[%@ page import="java.sql.*,java.util.List,java.util.ArrayList,DBPool" %]

but

package dbpooljar;
public class DBPool { ...

Therefor, it should be

[%@ page import="java.sql.*,java.util.List,java.util.ArrayList,dbpooljar.DBPool" %]

plus your java file should be located in a directory named WEB-INF/classes/dbpooljar or if you insist on packaging a jar file, place the jar file in WEB-INF/lib

Of course the angled brackets "[" and "]" are meant to be proper xml brackets "<" and ">" - I've preserved them here in order to be able to use bold typeface.

Upvotes: 1

Related Questions