2011-04-01 112 views
0

Remote.jsp代碼:的<jsp:包括標籤不工作的<jsp:包括頁= 「Remote.jsp」/>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Remote Value</title> 
</head> 
<body> 
<% 
    File remote = new File("D:\\test.txt"); 
    FileReader fr = new FileReader(remote); 
    int ch; 
    while((ch = fr.read()) != -1) 
     out.println("<p>" + (char)ch + "</p>"); 
    fr.close(); 
    %> 
</body> 
</html> 

<jsp:include page="Remote.jsp"/>被嵌入到另一個jsp和路徑是正確的。 但每次我得到一個異常。

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 11 in the jsp file: /Remote.jsp File cannot be resolved to a type 

回答

0

你可以得到這個異常時Remote.jsp未能自行編譯。您可以通過直接打開文件而不將其包含在另一個頁面中來測試它。然後你會看到明顯的例外原因。據我所見,你至少缺少中的java.io.*導入。

<%@page import="java.io.*" %> 

無關到具體的問題,這是一個貧窮的方法。它不僅可能導致其他JSP中的語法無效HTML(不能嵌套<html>等)。考慮使用從磁盤讀取文件並將其傳輸到響應的servlet。那麼你可以做

<div style="white-space: pre;"> 
    <jsp:include page="fileservlet/test.txt" /> 
</div>