我将servlet.java中的request.setAttribute("path", textpath);
的(* .txt)路径传递给* .jsp。并且* .txt在webserver地址中。如何向textArea显示内容?谢谢。
答案 0 :(得分:0)
既然你正在谈论JSP和阅读文件,我推断我们正在谈论Java。你想把文件的内容读成字符串,对吗?
这是一种Java方法。
/**
* Return the contents of file as a String.
*
* @param file
* The path to the file to be read
* @return The contents of file as a String, or null if the file couldn't be
* read.
*/
private static String getFileContents(String file) {
/*
* Yes. This really is the simplest way I could find to do this in Java.
*/
byte[] bytes;
FileInputStream stream;
try {
stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("File not found: `" + file + "`");
e.printStackTrace();
return null;
}
try {
bytes = new byte[stream.available()];
stream.read(bytes);
stream.close();
} catch (IOException e) {
System.out.println("IO Exception while getting contents of `"
+ file + "`");
e.printStackTrace();
return null;
}
return new String(bytes);
}
因此,您可以将其称为String fileContents = getFileContents(textPath);
。
然后,在您的页面中,您会说<textarea><%= fileContents %></textarea>
。