Question

Good day, I'm a beginner learning Jave EE. I created a servlet (controller) VersionOverview.java that contains following code in the doGet method:

RequestDispatcher view = request.getRequestDispatcher("WEB-INF/jsp/versions.jsp");
view.forward(request, response);

File WEB-INF/jsp/versions.jsp contains these lines:

<div id="content">
  <jsp:include page="/WEB-INF/jspf/organization-filter.jspf" />
  ...
</div>

File /WEB-INF/jspf/organization-filter.jspf contains these lines:

<form name="organization-filter" action="versions">
  <input type="text" id="org_name" name="org_name" value="<%= request.getAttribute("org_name") %>">
  <br>
  <input type="hidden" id="org_id" name="org_id" value="<%= request.getAttribute("org_id") %>">
  <br>
  <input type="submit" name="submit" value="Submit"/>
</form>

When I open page /versions (mapped to the servlet above) it seems that organization-filter.jspf is not compiled because instead of blank input field named org_name I can see this text inside it:

<%= request.getAttribute(

If I change include directive in the versions.jsp like shown below it starts working:

<div id="content">
  <%@include file="/WEB-INF/jspf/organization-filter.jspf" %>
  ...
</div>

Please advise why it doesn't work when I use jsp:include. Thank you in advance. Vojtech

Was it helpful?

Solution

A jspf file is a file that is supposed to be statically included, using an include directive:

<%@ include file="someFile.jspf" %>

That's just what a JSP fragment segment is, and since it's supposed tobe statically included, there is no point in compiling it.

Here's what the spec says:

By default the extension .jsp means a top-level JSP file. We recommend, but do not mandate, to differentiate between top-level JSP files (invoked directly by the client or dynamically included by another page or servlet) and statically included segments so that:

  • The .jsp extension is used only for files corresponding to top level JSP files, forming a JSP page when processed.
  • Statically included segments use any other extension. As included segments were called ‘JSP fragments’ in past versions of this specification, the extension .jspf was offered as a suggestion. This extension is still suggested for consistency reasons, despite that they are now called ‘jsp segments’.

If you want to dynamically include it, then it must have the .jsp extension. But in this case, I don't see why you wouldn't use a static include.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top