【问题标题】:How to Include a file outside the application (war) using jsp include如何使用 jsp include 在应用程序(战争)之外包含文件
【发布时间】:2009-05-06 10:24:25
【问题描述】:

我正在使用“jsp:include”在我的一个 jsp 文件中包含一个静态文件。当静态 html 文件位于应用程序文件夹中时,它可以正常工作。但是,如果它保存在应用程序文件夹之外,则它不会包含在 JSP 文件中。

注意:我已经为保存静态文件的文件夹创建了一个上下文,我可以使用直接 url 查看 html 文件。

请帮忙..

【问题讨论】:

    标签: jsp include


    【解决方案1】:

    我已经使用 c:import 标签解决了这个问题。

    为了定义动态 URL,我使用了 bean:define 标记。感谢朋友的建议和帮助。

    【讨论】:

    • 使用协议前缀:“file:///${你的文件路径}”
    • @EdgardLeal 我假设使用“文件”协议期望文件位于客户端计算机中。 'c:import' 为我解决了这个问题。
    【解决方案2】:

    您只能将 jsp:include 用于 Web 应用程序上下文中的资源。您将需要使用java.io.File 或类似名称从文件系统路径加载,或使用ClassLoader.getResource 从类路径加载资源。

    【讨论】:

    • 嗨彼得希尔顿,谢谢你的建议。我已经使用 c:import 标签来解决这个问题。
    【解决方案3】:

    <c:import> 方法的另一个好处是您可以使用charEncoding 属性设置编码。 您不能使用 <%@include%><jsp:include> 语句来执行此操作。

    【讨论】:

      【解决方案4】:

      【讨论】:

        【解决方案5】:

        我使用了一个类,该类具有从 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;
        }
        

        【讨论】:

          【解决方案6】:

          只是出于兴趣 - 想要这样做的原因是什么? - 可能有另一种方法。

          我怀疑您想要一些独立于 WAR 文件的配置,并且对于部署 WAR 的每个环境都是唯一的。

          【讨论】:

          • 您好 Belugabob,我有所有 JSP 处于战争状态,我想在 JSP 文件中包含一个静态 html 文件(比如帮助卡)。我在war文件之外的另一个不同上下文中拥有这些html文件。我已经使用 c:import 解决了这个问题
          • 你是怎么解决的?可以分享一下代码吗?我有类似的问题,即使使用 c:import 也找不到解决方案
          猜你喜欢
          • 2015-12-04
          • 1970-01-01
          • 1970-01-01
          • 2011-08-18
          • 1970-01-01
          • 2016-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多