1

So this question suggested using a servlet to do the file check before doing the include: How can you check if a file exists before including/importing it in JSP?

So I wrote a servlet that does that. I call it using something like

<jsp:include page='<%= "/servlet/fileChecker?section=THX&file=1138" &>'></jsp:include>

but the output of that servlet call contains a jsp:include tag, to the file I was checking for. Unfortunately, the browser doesn't "include" it. I'm getting errors like "there is no attribute 'page'" and "element 'jsp:include' undefined" -- which suggests the servlet output is not being rendered as Java but as HTML.

Here is the output:

<p>Text before the servlet call</p>
    <p><h4>From THX1138</h4><jsp:include page='thx/results_1138.jsp'></jsp:include></p>
<p>Text after the servlet call</p>

Here is the main call of my servlet:

private String FileChecker(String section, String file) {
    String result = ""; // assume file does not exist

    String pathToCheck = section + "/results_" + file + ".jsp";
    // realPath is defined in the init() method as config.getServletContext().getRealPath("/");
    File fileToCheck = new File(realPath + pathToCheck);
    if (fileToCheck.exists()) {
        result = "<p><h4>" + section + "</h4><jsp:include page='" + pathToCheck + "'></jsp:include></p>";
    }

    return result;
}

I feel like the answer is close, but I'm not sure what curtain I should be looking behind. Any help?

Community
  • 1
  • 1
Robert
  • 11
  • 1
  • 3
  • `jsp:...` tags are not included by the browser, they are interpreted on the server. The browser does only understand HTML. How does your `fileChecker` look like? – home Sep 22 '11 at 18:57
  • It will not work that way. `filechecker`'s output will not get interpreted again. Can you please show the code of `filechecker` or its relevant parts? – home Sep 22 '11 at 19:04
  • Off topic: stop using raw servlets. Use something like struts, tiles, spring-mvc, jsf, or the like. – DwB Sep 22 '11 at 19:21
  • @DwB: Meh--if there's no reason for a big ol' framework, don't use one. Servlets are fine as long as they're not abused, just like anything else. This functionality could be wrapped up in a JSP-bsed custom tag anyway. – Dave Newton Sep 22 '11 at 19:32

1 Answers1

0

Do not write a string with a bunch of HTML and JSP tags to the response. This makes no sense. The webbrowser does not understand JSP tags.

Call RequestDispatcher#include() instead.

request.getRequestDispatcher(checkedJspPath).include(request, response);

And move that HTML back into the JSP.


Unrelated to the concrete question, I know that you're referring to an old answer of me, but I realize that it's actually better to check if ServletContext#getResource() doesn't return null instead of using File#exists(). This way it'll work as well whenever the WAR is not expanded.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for the response. Could I use RequestDispatcher but still have the additional HTML markup (the simple

    tag) in addition to the include?

    – Robert Sep 22 '11 at 19:22
  • Yes, you can write it to the response before and after the `include()` call. This is however not the best practice. HTML do not belong in servlet. Consider another wrapping include JSP file which contains that HTML. – BalusC Sep 22 '11 at 19:24
  • Thanks again. I'll read up on RequestDispatcher and try it out. (You can probably tell I've never used it before.) Many thanks! – Robert Sep 22 '11 at 19:28