2009-05-06 45 views
5

Tôi đang sử dụng "jsp: include" để bao gồm một tệp tĩnh trong một trong các tệp jsp của tôi. Nó hoạt động tốt khi tệp html tĩnh nằm bên trong thư mục ứng dụng. Tuy nhiên, nếu nó được giữ bên ngoài thư mục ứng dụng, nó không được bao gồm trong tệp JSP.Cách bao gồm một tệp bên ngoài ứng dụng (chiến tranh) bằng cách sử dụng jsp bao gồm

Lưu ý: Tôi đã tạo ngữ cảnh cho thư mục chứa tệp tĩnh và tôi có thể xem tệp html bằng url trực tiếp.

Xin giúp ..

Trả lời

11

Tôi đã giải quyết vấn đề này bằng cách sử dụng thẻ c: nhập.

Để xác định URL động tôi đã sử dụng thẻ bean: define. Cảm ơn bạn cho những gợi ý và giúp đỡ.

+0

Sử dụng tiền tố giao thức: "file: /// $ {đường dẫn tệp của bạn}" –

+0

@EdgardLeal Tôi giả sử giao thức 'tệp' mong muốn tệp được đặt trong máy khách. 'c: import' đã giải quyết nó cho tôi. –

3

Bạn chỉ có thể sử dụng jsp:include cho các nguồn lực bên trong bối cảnh ứng dụng web của bạn. Bạn sẽ cần sử dụng java.io.File hoặc tương tự để tải từ đường dẫn hệ thống tệp hoặc ClassLoader.getResource để tải tài nguyên từ đường dẫn lớp.

+0

Hi Peter Hilton, cảm ơn bạn đã gợi ý. Tôi đã sử dụng c: nhập khẩu thẻ để giải quyết vấn đề này. –

0

Chỉ cần không quan tâm - lý do muốn làm điều này là gì? - có thể có một cách tiếp cận khác.

Tôi nghi ngờ rằng bạn muốn có một số cấu hình độc lập với tệp WAR và là duy nhất cho mỗi môi trường mà WAR được triển khai.

+1

Hi Belugabob, Tôi có tất cả các JSP trong một cuộc chiến và tôi muốn bao gồm một tệp html tĩnh (nói điều gì đó giống như thẻ trợ giúp) trong tệp JSP. Tôi có các tệp html này trong một ngữ cảnh khác bên ngoài tệp chiến tranh. Tôi đã giải quyết vấn đề này bằng cách sử dụng c: nhập –

2

gia tăng lợi ích này của phương pháp <c:import> là bạn có thể thiết lập mã hóa bằng cách sử dụng thuộc tính charEncoding. Bạn không thể thực hiện việc này với các câu hỏi <%@include%> hoặc <jsp:include>.

1

tôi sử dụng một lớp học mà có một phương pháp để có được nội dung từ URL: Ex: http://link.inet.vn/seo-website/inet.html

public class URLReader 
{ 
    public URLReader() 
    { 
     in = null; 
     out = null; 
     requestType = null; 
     headers = null; 
     content = null; 
     headers = new Hashtable(); 
    } 

    public void doGet(String server, String uri, int port) 
    { 
     try{ 
      Socket client = new Socket(server, port); 
      client.setKeepAlive(true); 
      in = new DataInputStream(client.getInputStream()); 
      out = new DataOutputStream(client.getOutputStream()); 
      out.writeBytes("GET " + uri + " HTTP/1.0\r\n"); 
      out.writeBytes("Host: " + server + "\r\n"); 
      out.writeBytes("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n"); 
      out.writeBytes("Accept-Language: en-us\r\n"); 
      out.writeBytes("Accept-Encoding: gzip, deflate\r\n"); 
      out.writeBytes("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)\r\n"); 
      out.writeBytes("Connection: Keep-Alive\r\n"); 
      out.writeBytes("Content-Length: 0\r\n\r\n"); 
      out.flush(); 
      parseRequest(); 
      out.close(); 
      in.close(); 
      client.close();   
     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
     } 

     return; 
    } 

    public byte[] getContent() 
    { 
     return content; 
    } 

    public String getHeader(String name) 
    { 
     String key = (String)headers.get(name); 
     return key; 
    } 

    public Hashtable getHeaders() 
    { 
     return headers; 
    } 

    public String getRequestType() 
    { 
     return requestType; 
    } 

    public static void main(String args[]) throws IOException 
    { 
     URLReader reader = new URLReader(); 

     reader.doGet("link.inet.vn", "/seo-website/inet.html", 80); 
     if(reader.getContent() != null) 
      System.out.println(new String(reader.getContent(),"UTF-8")); 

    } 

    private boolean parseRequest() 
    { 
     byte match[]; 
     int index; 
     String line; 
     match = (new byte[] { 
      13, 10, 13, 10 
     }); 
     index = 0; 
     line = ""; 
     int i; 
     try{ 
      while((i = in.read()) >= 0) 
      { 
       if(i == match[index] && index <= 3) 
        index++; 
       else 
        index = 0; 
       if(index == 4) 
       { 
        content = readHTTPContent(); 
        break; 
       } 
       line = line + (char)i; 
       if(line.length() > 2 && i == 10) 
       { 
        int pos = line.indexOf(':'); 
        if(pos != -1) 
        { 
         String name = line.substring(0, pos); 
         String value = line.substring(pos + 1, line.length()).trim(); 
         setHeader(name, value); 
        } else 
        { 
         setRequestType(line.substring(0, line.length()).trim()); 
        } 
        line = ""; 
       } 
      } 

      return true; 
     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
      return false; 
     }     
    } 

    private byte[] readHTTPContent() 
     throws IOException 
    { 
     ByteArrayOutputStream baosContent = new ByteArrayOutputStream(); 
     int contentLength = 0; 
     try { 
      contentLength = Integer.parseInt((String) headers.get("content-length")); 
     } catch (Exception ex) { 
      contentLength = 1024 * 1024; 
     } 
     int bytesToRead = 0; 
     int bytesRead = 0; 
     int totalBytesRead = 0; 
     int bufferSize = 1024; 
     byte[] buffer = new byte[bufferSize]; 

     if (contentLength < bufferSize) { 
      bytesToRead = contentLength; 
     } else { 
      bytesToRead = bufferSize; 
     } 
     do { 
      try { 
       bytesRead = in.read(buffer, 0, bytesToRead); 
      } catch (InterruptedIOException e) { 
       /* comms read timeout expired, no problem */ 
       System.out.println("Timeout reading from socket"); 
      } 
      if (bytesRead == -1) { 
       in.close(); 
       // throw new IOException("Connection was closed by client."); 
       break; 
      } else if (bytesRead > 0) { 
       ////////////////////////////////////// 
       baosContent.write(buffer, 0, bytesRead); 
       ////////////////////////////////////// 
       totalBytesRead += bytesRead; 
      } 
      // Left bytes to read 
      if (contentLength - totalBytesRead > bufferSize) { 
       bytesToRead = bufferSize; 
      } else { 
       bytesToRead = contentLength - totalBytesRead; 
      } 
     } while (totalBytesRead < contentLength); 

     return baosContent.toByteArray();   
    } 


    public void saveToFile(byte data[], String filename) 
    { 
     try{ 
      File f = new File(filename); 
      FileOutputStream fout = new FileOutputStream(f); 
      fout.write(data); 
      fout.close(); 
     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
     }   
     return; 
    } 


    private void setHeader(String key, String value) 
    { 
     headers.put(key.toLowerCase(), value); 
    } 

    private void setRequestType(String s) 
    { 
     requestType = new String(s); 
    } 

    private byte content[]; 
    private Hashtable headers; 
    private DataInputStream in; 
    private DataOutputStream out; 
    private String requestType; 
} 
Các vấn đề liên quan