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

Javascript 如何在*.jsp中向textArea显示文本?,javascript,web-services,Javascript,Web Services,我通过request.setAttributepath、textpath传输*.txt的路径;在servlet.java中转换为*.jsp。并且*.txt在Web服务器地址中。如何将内容显示到textArea?谢谢 既然你说的是JSP和读取文件,我推断我们说的是Java。您想将文件内容读入字符串,对吗 下面是一个Java方法 /** * Return the contents of file as a String. * * @param file * The p

我通过request.setAttributepath、textpath传输*.txt的路径;在servlet.java中转换为*.jsp。并且*.txt在Web服务器地址中。如何将内容显示到textArea?谢谢

既然你说的是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);
}
所以您可以像字符串fileContents=getFileContentstextPath;那样调用它


然后,在你的页面中,你会说,。

这是什么?Java还是javascript?除了@leppie的问题,textPath是服务器或客户端上文件的路径吗?