2014-12-20 34 views
0

这是我index.jsp页面代码[这个问题是这样read and display a local text file in jsp page读取并显示在jsp页面本地文本文件中有错误

<%@ 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。 但是这个代码没有任何输出,为什么?

+0

因为它是空的。 –

+0

不,我的文本文件已上线 – Farshid

回答

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> 
相关问题