繁体   English   中英

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

[英]How to show text to textArea in *.jsp?

我通过request.setAttribute("path", textpath);传递(* .txt)的路径request.setAttribute("path", textpath); 在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);
}

因此,您可以将其称为String fileContents = getFileContents(textPath);

然后,在页面中,您会说<textarea><%= fileContents %></textarea>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM