如何使用JSP include或c:import或symlink加载应用程序上下文之外的HTML页面 [英] How to load Html page which is outside application context using JSP include or c:import or symlink

查看:117
本文介绍了如何使用JSP include或c:import或symlink加载应用程序上下文之外的HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用jsp include标签<jsp:include page="${Htmlpath}" /> OR <jsp:include page="D:\MySharedHTML\test.html" />将某些其他应用程序动态创建的html页面加载到我的应用程序jsp页面中.我的想法是在服务器上有一个共享文件夹,例如"MySharedHTML",并让其他应用程序在那里创建html文件,我的应用程序将通过提供完整路径进行访问.但是jsp include表示请求的资源D:\ MySharedHTML \ test.html不可用".任何输入该怎么做.提前谢谢.

解决方案

该URL必须可用. D:\MySharedHTML\test.html绝对不是有效的URL.一个有效的URL如下http://localhost:8080/MySharedHTML/test.html所示.

是否使用<jsp:include><c:import>取决于URL是内部URL还是外部URL. <jsp:include>仅适用于内部URL(因此,同一Web应用程序中的资源,也包含在/WEB-INF中私下隐藏的资源). <c:import>还可以在外部URL(因此,完全不同的Web应用程序中的资源,但是必须公开访问这些资源);即,在将URL复制到浏览器的地址栏中时,必须已经看到所需的包含内容). /p>

在您的特定情况下,您似乎在服务器的本地磁盘文件系统中的其他位置拥有该文件,但真正的URL根本无法使用它.在这种情况下,您基本上有2个选择:

  1. 将该路径的根文件夹作为虚拟主机添加到服务器配置中.如何做到这一点取决于服务器的制造商/版本,您对此一无所知.以Tomcat为例,可以在/conf/server.xml中添加以下条目:

    <Context docBase="D:\MySharedHTML" path="/MySharedHTML" />
    

    这样,http://localhost:8080/MySharedHTML/*(包括test.html)都可以使用该文件夹的所有内容.这样,您可以在其上使用<c:import>(注意:<jsp:include>不适用,因为它不在同一webapp中).

    <c:import url="/MySharedHTML/test.html" />
    


  2. 创建一个servlet,该servlet充当本地磁盘文件系统的代理.假设您使用的是Servlet 3.0/Java 7,并且可以更改${Htmlpath}变量,使其仅返回test.html,那么应该这样做:

    @WebServlet("/MySharedHTML/*")
    public class PdfServlet extends HttpServlet {
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String filename = request.getPathInfo().substring(1);
            File file = new File("D:\\MySharedHTML", filename);
            response.setHeader("Content-Type", getServletContext().getMimeType(filename));
            response.setHeader("Content-Length", String.valueOf(file.length()));
            response.setHeader("Content-Disposition", "inline; filename=\"" + URLEncoder.encode(filename, "UTF-8") + "\"");
            Files.copy(file.toPath(), response.getOutputStream());
        }
    
    }
    

    (当尚未使用Servlet 3.0/Java 7时,只需回到明显的web.xml注册和InputStream/OutputStream循环样板)

    由于servlet在同一webapp中运行,因此<jsp:include>应该可以正常工作:

    <jsp:include page="/MySharedHTML/${HtmlFilename}" />
    

I need to load some html pages created dynamically by some other application into my application jsp page using jsp include tag <jsp:include page="${Htmlpath}" /> OR <jsp:include page="D:\MySharedHTML\test.html" />. My idea is to have a shared folder on server like "MySharedHTML" and let other application create html files there and my app will access by giving full path. But jsp include is saying "requested resource D:\MySharedHTML\test.html is not available". Any inputs how to do it. Thanks In Advance.

解决方案

It has to be available by an URL. The D:\MySharedHTML\test.html is very definitely not a valid URL. A valid URL look like this http://localhost:8080/MySharedHTML/test.html.

Whether to use <jsp:include> or <c:import> depends on whether the URL is an internal or an external URL. The <jsp:include> works only on internal URLs (thus, resources in the same webapp, also the ones privately hidden in /WEB-INF). The <c:import> works additionally also on external URLs (thus, resources in a completely different webapp, but those have to be publicly accessible; i.e. you have got to see the desired include content already when copypasting the URL in browser's address bar).

In your particular case, you seem to have it elsewhere in the server's local disk file system which is not available by a true URL at all. In that case you've basically 2 options:

  1. Add the root folder of that path as a virtual host to the server configuration. How to do that depends on the server make/version which you didn't tell anything about. To take Tomcat as an example, that would be a matter of adding the following entry to its /conf/server.xml:

    <Context docBase="D:\MySharedHTML" path="/MySharedHTML" />
    

    This way all of the folder's contents is available by http://localhost:8080/MySharedHTML/*, including the test.html. This way you can use <c:import> on it (note: <jsp:include> is inapplicable as this is not in the same webapp).

    <c:import url="/MySharedHTML/test.html" />
    


  2. Create a servlet which acts as a proxy to the local disk file system. Let's assume that you're using Servlet 3.0 / Java 7 and that you can change ${Htmlpath} variable in such way that it merely returns test.html, then this should do:

    @WebServlet("/MySharedHTML/*")
    public class PdfServlet extends HttpServlet {
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String filename = request.getPathInfo().substring(1);
            File file = new File("D:\\MySharedHTML", filename);
            response.setHeader("Content-Type", getServletContext().getMimeType(filename));
            response.setHeader("Content-Length", String.valueOf(file.length()));
            response.setHeader("Content-Disposition", "inline; filename=\"" + URLEncoder.encode(filename, "UTF-8") + "\"");
            Files.copy(file.toPath(), response.getOutputStream());
        }
    
    }
    

    (when not using Servlet 3.0 / Java 7 yet, just fall back to the obvious web.xml regisration and InputStream/OutputStream loop boilerplate)

    As the servlet runs in the same webapp, <jsp:include> should work just fine:

    <jsp:include page="/MySharedHTML/${HtmlFilename}" />
    

这篇关于如何使用JSP include或c:import或symlink加载应用程序上下文之外的HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆