0
votes

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."

Directory structure

  • webapps
    |app
    |
    _ index.jsp
    |_ WEB-INF
    ......|
    _ classes
    .............|_ mypack
    ....................|_sou.class, test.war, mypack.jar

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

2
The question is damn long, I know. But this should not be referred to CODEREVIEW as it's not a problem related to code I believe. Before -1 voting write your reasons so I can rectify myself from similar error in future ! - Sourav
What is the exact output you get on browser? - vivek_jonam
I've written output on the error section - Sourav
you getting that Hello World! part four times? - vivek_jonam
Nope, they are being shown in the error section while showing the code - Sourav

2 Answers

0
votes

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.

0
votes

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.*” %>