4

I need to include content in a JSP without using the include directive.

It is easy to do a server-side include with with <%@include file="includeMe.htm" %> but the content of includeMe.htm gets added to the JSP before the JSP is converted into a Servlet by the container. This means that if includeMe.htm gets modified, the changes are not reflected in the generated .java Servlet file. I'm tired of going into Tomcats generated files directory to manually delete the generated java and class files or re-deploy my app every time the included file changes.

Do I need to write a code block to just read in data from the text file line by line and then write it like this?

 <%
    while( not done reading from file ) {
        String line = scanner.nextLine();
        response.getWriter().println(line);
    }  %>

Is there an easier or cleaner way?

Thorn
  • 4,015
  • 4
  • 23
  • 42
  • 1
    You can use `.tag` files for reusable content or do `` (*INCLUDE* dispatch). However Tomcat should be able to re-compile JSP with such dependencies AFAIK... maybe it is because you are using incorrect extension. Try to use `.jspf` (JSP fragment) instead. – Pavel Horal Nov 17 '13 at 12:47
  • Just as I thought, Tomcat should be able to handle changes in included fragments - http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html (check *Recompile JSP when included page changes*). – Pavel Horal Nov 17 '13 at 12:53
  • If you want to include content of includeMe.htm at runtime instead of compile team then go for jsp include instead of @include. Does it address your query? – M Sach Nov 17 '13 at 13:10
  • @Pavel For some reason I thought this easy solution didn't work because the enclosed jsp page was not modified. Please change your comment to an answer to make it easier for others to find when searching. – Thorn Nov 17 '13 at 13:17
  • Perhaps this is a new feature of Tomcat. For instance, the link below describes my assumption: http://javapapers.com/jsp/difference-between-jsp-include-directive-and-jsp-include-action/ Either way, I guess `` would have worked. – Thorn Nov 17 '13 at 13:20
  • @Thorn Created a proper answer. Btw. the feature is there at least from Tomcat 6... Also I know that Tomcat/Eclipse integration has a bit of issue when you try to access the changed page too soon (like in one second from its modification). – Pavel Horal Nov 17 '13 at 13:24

2 Answers2

7

Take a look at answers here. You can also use <c:import>:

2) The <jsp:include> standard action

<jsp:include page="header.jsp" />

Dynamic: adds the content from the value of the page attribute to the current page at request time. Was intended more for dynamic content coming from JSPs.

3) The <c:import> JSTL tag:

<c:import url=”http://www.example.com/foo/bar.html” />

Dynamic: adds the content from the value of the URL attribute to the current page, at request time. It works a lot like <jsp:include>, but it’s more powerful and flexible: unlike the other two includes, the <c:import> url can be from outside the web Container!

Regarding reading a file into string. If the file is small do it one line:

String content = new Scanner(new File("filename")).useDelimiter("\\Z").next();
Community
  • 1
  • 1
bancer
  • 7,475
  • 7
  • 39
  • 58
3

You can use .tag files for reusable content or do <jsp:include> (INCLUDE dispatch) to another JSP.

However Tomcat is able to re-compile JSPs when any of the included files got changed:

Recompile JSP when included page changes - Jasper 2 can now detect when a page included at compile time from a JSP has changed and then recompile the parent JSP.

Also note, that the common practice is to use .jspf (JSP fragment) extension for files included via <%@include %>.

Pavel Horal
  • 17,782
  • 3
  • 65
  • 89