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: