2009-05-06 14 views
5

"jsp:include"を使用して、jspファイルの1つに静的ファイルを含める。静的なhtmlファイルがアプリケーションフォルダ内にある場合はうまく動作します。ただし、アプリケーションフォルダの外に置かれている場合は、JSPファイルには含まれません。jspを使用してアプリケーションの外部にファイルを含める方法(war)

注:静的ファイルが保存されているフォルダのコンテキストを作成しました。直接URLでhtmlファイルを表示できます。

助けてください..

答えて

11

c:importタグを使用してこの問題を解決しました。

動的URLを定義するために、私はbean:defineタグを使用しました。友人に提案と助けをありがとうございます。

+0

を使用して解決しました。プロトコル接頭辞を使用してください: "file:/// $ {あなたのファイルパス}" –

+0

@EdgardLeal私は 'ファイル'プロトコルを使用するとファイルがクライアントコンピュータにあると想定します。 'c:import'は私のためにそれを解決しました。 –

3

あなたは、あなたのWebアプリケーションのコンテキスト内でのリソースのjsp:includeを使用することができます。ファイルシステムのパスからロードする場合は、java.io.Fileなどを使用するか、クラスパスからリソースをロードする場合は、ClassLoader.getResourceを使用する必要があります。

+0

こんにちはピーター・ヒルトンが提案していただきありがとうございます。私はこの問題を解決するためにc:importタグを使用しました。 –

0

ちょうど興味がありません - これをしたい理由は何ですか? - 別の方法があるかもしれません。

私は、WARファイルとは無関係で、WARがデプロイされている環境ごとに固有の構成をいくつか持たせたいと思っています。

+1

こんにちはBelugabob、 私はすべてのJSPを戦争の対象としており、JSPファイル内に静的なHTMLファイル(ヘルプカードのようなもの)を入れたいと思っています。私はこれらのhtmlファイルをwarファイル以外の別のコンテキストの下に置いています。 私はこれをc:import –

2

追加の利点は、あなたがcharEncoding属性を使用してエンコーディングを設定することができるということです。を参照してください。 <%@include%>または<jsp:include>ステートメントでは、これを行うことはできません。

1

私はURLからコンテンツを取得するためのメソッドを持っているクラス使用: 例:、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; 
} 
関連する問題