user1792335
user1792335

Reputation: 13

tomcat 7 shows compile error in a hello world jsp

Please have a look at the info

Objective: To run a JSP page which calls a java class, both residing on tomcat server

Environment
Server environment: Linux
Server: Tomcat7.0.27
IDE: none

JSP (all it does is display a string)

<%@page import="HelloWorld.HelloWorld" %>  
<HTML>  
<HEAD>  
<TITLE>Hello World/TITLE>  
</HEAD>  
<BODY>  
<H1>Hello World</H1>  
String is: <%= HelloWorld.HelloWorld.display() %>  
</BODY>  
</HTML>  

JSP location:TOMCAT/webapps/hello/hello.jsp

Java class(all it does is return a string)
package HelloWorld;

public class HelloWorld {  
public static String display() {  
 return "Hello World!"; // Display the string.  
   }  
}  

Java class location: TOMCAT/webapps/hello/WEB-INF/classes/HelloWorld/HelloWorld.class •Java class compile action: javac -classpath TOMCAT/lib/servlet-api.jar:. -d /home/user/ HelloWorld.java

•WEB.XML

<?xml version="1.0" encoding="Cp1252"?>  
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>  
<web-app>  
<display-name>hello</display-name>  
<description>no description</description>  
<servlet>  
<servlet-name>hello</servlet-name>  
<display-name>hello</display-name>  
<description>no description</description>  
<jsp-file>/hello.jsp</jsp-file>  
</servlet>  
<session-config>  
<session-timeout>30</session-timeout>  
</session-config>  
</web-app>  

Actions: o Copy java class to directory o Clean TOMCAT/work directory o Shutdown TOMCAT o Startup TOMCAT o Open server:8080/hello/hello.jsp

Issue: HTTP Status 500 org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 14 in the generated java file The import HelloWorld cannot be resolved An error occurred at line: 8 in the jsp file: /hello.jsp HelloWorld cannot be resolved 5: </HEAD> 6: <BODY> 7: <H1>Hello World</H1> 8: String is : <%= HelloWorld.HelloWorld.display() %> 9: </BODY> 10: </HTML> Stacktrace:org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)

There is no syntax error in calling PackageName.ClassName.StringReturningMethod() in JSP The class is in package folder.

What is causing it?

Thanks,

Upvotes: 1

Views: 1885

Answers (1)

Esteban Aliverti
Esteban Aliverti

Reputation: 6322

The problem seems to be related to the fact that the class name is equal to the package name. There is a name clash since you are importing the package and you are also using the fqn of the class when invoking the display() method.

So, you imported HelloWorld.HelloWorld and then you try to do HelloWorld.HelloWorld.display(). Java is taking the first part of HelloWorld.HelloWorld.display as the class that you have imported and then tries to look for a static member called HelloWorld in that class.

Long story short, either remove the import (since you are using the fqn) or do not use the fqn when calling display().

Best Regards,

Upvotes: 0

Related Questions