<jsp:include tag =“”doesnt =“”work =“”<jsp:include =“”page =“Remote.jsp”> </jsp:include>

时间:2011-04-01 13:15:45

标签: jsp tags include

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

1 个答案:

答案 0 :(得分:0)

Remote.jsp无法自行编译时,您可以获得此异常。您可以直接打开文件进行测试,而不必将其包含在另一页中。然后,您会看到有关原因的明确例外情况。据我所知,您至少缺少java.io.*中的Remote.jsp导入。

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

对具体问题

无关,这是一种糟糕的做法。它不仅可能导致其他JSP中语法无效的HTML(您无法嵌套<html>等)。考虑使用从磁盘读取文件并将其传输到响应的servlet。那你就可以做到

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