簡體   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