2011-04-01 137 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>