Include file contents in JSP

I would like to include the contents of the file in a JSP page. Normally I would use something like:

<jsp:include page="<%= path_to_file %>" />

However, this will not work here, because the file I'm trying to include is outside of the web deployment.

The ugliest solution I've seen looks something like this:

<td>
<% BufferedReader  br =  new BufferedReader(new FileReader(new File(path_to_file)));
   String line = br.readLine();
   while (line != null) { %>
     <% out.println(line); %>
     <% line = br.readLine(); } %>
</td>
<% } catch (IOException e) { %>
<td>
  <%= e %>
</td>
<% } %>

But I really don't want to do this.

thanks

+3
source share
2 answers

You can include a symbolic path in your distribution to point to your external path.

0
source

To avoid the ugly script code, you can write a small tag (in Java, extending SimpleTagSupport) only to include arbitrary files from your external path.

Then you could write

<my:include page="<%= path_to_file %>" />
0
source

Source: https://habr.com/ru/post/1707613/


All Articles