在jsp页面中读取并显示本地文本文件有错误

时间:2014-12-20 20:11:38

标签: java file jsp

这是我的index.jsp页码[这个问题就像这个read and display a local text file in jsp page

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*"%>
<%@page import="java.net.URL"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!--When I use this line has conflict error -->
<!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=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
    <%
        String jspPath = "C:\\Users\\Farshid\\workspace\\STPT\\WebContent\\file\\title.txt";
        BufferedReader reader = new BufferedReader(new FileReader(jspPath));
        //BufferedReader br = new InputStreamReader(new FileInputStream(txtFilePath));
        StringBuilder sb = new StringBuilder();
        String line;

        line = reader.readLine();
        out.println(sb.toString());
        System.out.println(sb.toString());
    %>

</title>

注意:我的IDE是eclipse,我的Web服务器是Tomcat7.0。 但是这个代码没有任何输出,为什么?

1 个答案:

答案 0 :(得分:0)

您在第1行和第4行使用多个页面指令。使用其中一个。并查看下面的更新代码。

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"%>
<%@page import="java.io.*"%>
<%@page import="java.net.URL"%>
 <!-- @page contentType="text/html" pageEncoding="UTF-8" --> <!--When I use this line has conflict error -->
<!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=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
    <%
        String jspPath = "C:\\Users\\Farshid\\workspace\\STPT\\WebContent\\file\\title.txt";
        BufferedReader reader = new BufferedReader(new FileReader(jspPath));
        //BufferedReader br = new InputStreamReader(new FileInputStream(txtFilePath));
        StringBuilder sb = new StringBuilder();
        String line;

        line = reader.readLine();
        out.println(sb.toString());
        System.out.println("line" +line);
    %>
 <%=line %>

</title>

</head>
<body>
<p> <%=line %></p>
</body>
</html>