Tomcat cannot find my Java Class

Hi, this problem has plagued me for a number of hours. I’ve googled looking for an answer, and still cannot figure this out.

This is a very simple JSP and Java class. I am just trying to access a java object from inside a jsp.

myjsp.jsp

<HTML>
<BODY BGCOLOR=“FDF5E6” >
<%!
MyClass data = new MyClass();
data.setUsername( “beagle” ); %>
<p>
Name is: <%= data.getUsername() %><br>
</BODY>
</HTML>

MyClass.java

public class MyClass {

String username;

public MyClass() {}

public void setUsername( String value )
{
    username = value;
}

public String getUsername() { return username; }

}

I have compiled MyClass to MyClass.class and placed it in ${CATALINA_HOME}/webapps/ROOT/WEB-INF/classes

I have stopped and started the tomcat server.

I have edited context.xml and added <Context reloadable=“true” >, to reload classes with a newer timestamp.

This all works if I put a package statement in. ie: in MyClass.java

package MyTest;

and in myjsp.jsp

MyTest.MyClass data = new MyTest.MyClass();

(and then store the class in ${CATALINA_HOME}/webapps/ROOT/WEB-INF/classes/MyTest

I would very much like to understand why the tomcat server cannot find my classes without having to access a packaged java class.

Thanks if anyone knows the secret to this!

When I do not have the package statement, and store my class in the classes directory, the error I get is:

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

An error occurred at line: 3 in the jsp file: /myjsp.jsp
Generated servlet error:
MyClass cannot be resolved to a type

An error occurred at line: 3 in the jsp file: /myjsp.jsp
Generated servlet error:
MyClass cannot be resolved to a type

An error occurred at line: 3 in the jsp file: /myjsp.jsp
Generated servlet error:
Syntax error on token(s), misplaced construct(s)

An error occurred at line: 3 in the jsp file: /myjsp.jsp
Generated servlet error:
Syntax error on token ““beagle””, delete this token

An error occurred at line: 7 in the jsp file: /myjsp.jsp
Generated servlet error:
data cannot be resolved
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

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

An error occurred at line: 3 in the jsp file: /myjsp.jsp
Generated servlet error:
MyClass cannot be resolved to a type

An error occurred at line: 3 in the jsp file: /myjsp.jsp
Generated servlet error:
MyClass cannot be resolved to a type

An error occurred at line: 3 in the jsp file: /myjsp.jsp
Generated servlet error:
Syntax error on token(s), misplaced construct(s)

An error occurred at line: 3 in the jsp file: /myjsp.jsp
Generated servlet error:
Syntax error on token ““beagle””, delete this token

An error occurred at line: 7 in the jsp file: /myjsp.jsp
Generated servlet error:

As far as I know, Tomcat no longer recognizes non-packaged classes in order to discourage sloppy programming.

I’m sure there’s some other, more technical, reason…

Do you have an import statement in your JSP? Your first line should look something like this:


<%@page import="com.me.MyClass"%>

If you don’t do that, your JSP doesn’t know to load that class for use on the page.

Okay, this is good to know. Since I don’t have a lot of experience working with Tomcat, it makes me think I’m missing something when I can’t get it to work. Thanks!

If you don’t do that, your JSP doesn’t know to load that class for use on the page.

I have used the import directive, but for my example, I was trying to get it to work without a package path. Importing com.me.MyClass would be a packaged class.