I'm constantly getting error HTTP Status 500 for all JSP pages which uses external Java class defined outside JSP pages. Here are the codes
index.jsp
<%@page import="mypack.sou" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
sou o=new sou();
int r=o.hi();
out.println(r);
%>
</body>
</html>
sou.java under package mypack
package mypack;
public class sou {
public int hi()
{
return 0;
}
}
The error:
type Exception report
"message Unable to compile class for JSP: An error occurred at line: 14 in the generated java file Only a type can be imported. mypack.sou resolves to a package An error occurred at line: 18 in the jsp file: /web/index.jsp sou cannot be resolved to a type 15:
Hello World!
16: 17: <% 18: sou o=new sou(); 19: int r=o.hi(); 20: out.println(r); 21: %> An error occurred at line: 18 in the jsp file: /web/index.jsp sou cannot be resolved to a type 15:Hello World!
16: 17: <% 18: sou o=new sou(); 19: int r=o.hi(); 20: out.println(r); 21: %> Stacktrace:description The server encountered an internal error (Unable to compile class for JSP: An error occurred at line: 14 in the generated java file Only a type can be imported. mypack.sou resolves to a package An error occurred at line: 18 in the jsp file: /web/index.jsp sou cannot be resolved to a type 15:
Hello World!
16: 17: <% 18: sou o=new sou(); 19: int r=o.hi(); 20: out.println(r); 21: %> An error occurred at line: 18 in the jsp file: /web/index.jsp sou cannot be resolved to a type 15:Hello World!
16: 17: <% 18: sou o=new sou(); 19: int r=o.hi(); 20: out.println(r); 21: %> Stacktrace:) that prevented it from fulfilling this request."
System Info: Win 7 Ultimate X64, Apache Tomcat 7.0.29
java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) Client VM (build 22.0-b10, mixed mode, sharing)
Tomcat directory has full permission !
I had to move to GlassFish, where the same code works ! But the problem still exist with Tomcat
Try adding a ";" to your import statement.
modify like this:
<%@page import="mypack.sou;" %>
<!DOCTYPE html> //Remove content type
May be this looks like a weird answer, but see this reference https://stackoverflow.com/a/1858635/586836
EDIT:
Otherwise:
Try compiling that java file to a class manually and then put that in the classes directory and check.
Try to use mypack.sou
instead of sou
; You have to use the fully-qualified class name inside JSPs, since all JSPs are turned into plain old Java servlet code by the Container. or you can import your package instead :
<%@ page import=”mypack.*” %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.