2012-08-12 323 views
0

對於使用在JSP頁面之外定義的外部Java類的所有JSP頁面,我總是收到錯誤HTTP狀態500。下面是碼Tomcat/Windows 7上的HTTP狀態500

的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下包mypack

package mypack; 
public class sou { 
public int hi() 
{ 
    return 0; 
} 
} 

錯誤:

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


目錄結構

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

系統信息:的Win 7旗艦版X64,Apache Tomcat上7.0.29
Java版本 「1.7.0_02」
的Java (TM)SE運行時環境(內部版本1.7.0_02-b13)
Java HotSpot(TM)客戶端VM(內部版本22.0-b10,混合模式,共享)
Tomcat目錄具有完全權限!


我不得不轉移到GlassFish,其中相同的代碼工作!但問題仍然存在與Tomcat

+0

問題是該死的,我知道。但是這不應該被稱爲CODEREVIEW,因爲它不是與我相信的代碼相關的問題。 **在-1投票前寫下您的理由,以便我可以糾正自己從未來的類似錯誤**! – Sourav 2012-08-12 16:35:32

+0

您在瀏覽器上獲得的確切輸出是什麼? – 2012-08-12 17:21:15

+0

我在錯誤部分 – Sourav 2012-08-12 17:22:33

回答

0

嘗試添加「;」到您的進口聲明。

修改如下:

<%@page import="mypack.sou;" %> 
<!DOCTYPE html> //Remove content type 


可能這看起來像一個奇怪的答案,但看到這個參考 https://stackoverflow.com/a/1858635/586836

編輯:
否則:
嘗試在編譯的java文件一個類手動,然後把它放在classes目錄中並檢查。

+0

沒有工作!!! – Sourav 2012-08-12 17:40:03

+0

沒有工作,再次! – Sourav 2012-08-12 17:57:12

0

嘗試使用 mypack.sou而不是sou; 您必須在JSP內使用完全限定的類名稱,因爲Container將所有JSP轉換爲普通的舊Java Servlet代碼。 或者你可以改爲導入你的包裹:

<%@ page import=」mypack.*」 %> 
+0

試過了,但沒有效果。我認爲代碼沒有錯誤,問題是與Win 7或Tomcat! – Sourav 2012-08-13 12:34:50

相關問題