2010-08-11 6 views
0

servlet.java에서 (* .txt)의 경로를 request.setAttribute("path", textpath);으로 * .jsp로 전송합니다. * .txt는 웹 서버 주소에 있습니다. 텍스트 영역에 내용을 표시하는 방법은? 감사합니다.* .jsp에서 textArea에 텍스트를 표시하는 방법?

+0

이것은 무엇입니까? 자바 또는 자바 스크립트? – leppie

+0

@ leppie의 질문에 textPath는 서버 또는 클라이언트의 파일 경로입니까? – Mark

답변

0

JSP로 읽고 파일을 읽으므로 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>이 표시됩니다.

관련 문제