How to load Html page which is outside application context using JSP include or c:import or symlink

Ganesh K picture Ganesh K · Oct 3, 2013 · Viewed 11.1k times · Source

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.

Answer

BalusC picture BalusC · Oct 4, 2013

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}" />