Question

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.

Was it helpful?

Solution

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

OTHER TIPS

You don't include by full path. The folder MySharedHTML will need to be under your application folder, and you include by relative path.

So say your webapp was at

 c:\Program Files\Apache Software Foundation\Tomcat\webapps\myapp\

You would put your MySharedHTML in there

 c:\Program Files\Apache Software Foundation\Tomcat\webapps\myapp\MySharedHTML

And then include by relative path:

 <jsp:include page="./MySharedHTML/test.html" />

Alternatively we can achieve with the help of symlink or shortlink or softlink, so that there will be no much coding. What I did in my case is created a softlink for MySharedHTML, which is under my application web content to some path in D drive.

As symlinks are disabled by default to enable them in your Tomcat server, you need add below configuration to context.xml, which is under conf folder of tomcat server.

<Context allowLinking="true">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top