简体   繁体   English

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

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

I transfer a (*.txt)'s path by request.setAttribute("path", textpath); 我通过request.setAttribute("path", textpath);传递(* .txt)的路径request.setAttribute("path", textpath); in servlet.java to a *.jsp. 在servlet.java中将其* .jsp And the *.txt is in webserver address.How to show the content to textArea?Thank you. * .txt在Web服务器地址中。如何将内容显示到textArea?谢谢。

Since you're talking JSP and reading files, I infer we're talking Java. 由于您正在谈论JSP和读取文件,所以我推断我们正在谈论Java。 You want to read the contents of a file into a string, right? 您想将文件的内容读取为字符串,对吗?

Here's a Java method for doing that. 这是用于执行此操作的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);
}

So you would call this like String fileContents = getFileContents(textPath); 因此,您可以将其称为String fileContents = getFileContents(textPath); .

Then, in your page, you would say, <textarea><%= fileContents %></textarea> . 然后,在页面中,您会说<textarea><%= fileContents %></textarea>

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

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