如何在* .jsp中向textArea显示文本?

时间:2010-08-11 16:49:46

标签: javascript web-services

我将servlet.java中的request.setAttribute("path", textpath);的(* .txt)路径传递给* .jsp。并且* .txt在webserver地址中。如何向textArea显示内容?谢谢。

1 个答案:

答案 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>