• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher - how to include a jspf file?

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jsp which uses...

<%@ include file="../WEB-INF/jspf/full-template_top.jspf" %>

...to include the top section of a web page in its content. The jspf file uses variables from the caller jsp to control its output.

Is it possible to include this jspf file into a *servlet* response where, again, the caller (servlet) can control the jspf output?

Cheers,
James
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a RequestDispatcher include, but that performs the same action as a <jsp:include> action, not the directive. Of course, any scripting variables that the fragment expects will not exist.

Sounds like a refactoring may be in order. Care to elaborate on your needs?

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply.

The situation is that we have some JSPs already set up in this way to include the content, and are now wanting to use the include on a servlet.

Sounds like I will have to go the <jsp:include> route...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there scriptlets variables that will be an issue? Scoped variables should be no problem -- the servlet can set those easily.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got round the problem by doing this is my JSP...

<jsp:include page="/jspf/full-template_top.jsp" flush="true">
<jsp:param name="title" value="register" />
</jsp:include>

...and this is my servlet...

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/jspf/full-template_top.jsp");
request.setAttribute("title", "product details");
requestDispatcher.include(request, response);

My jsp fragment file then searches for request.getParameter("title") and (if that was null) request.getAttribute("title")

Is that a good way of doing it?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just use a scoped variable for both scenarios? Why bother with the param?
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you be kind enough to provide me with code snippets for how I could do this from a JSP and from a servlet?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're already doing it in the servlet. The JSP equivalent would be:

Now, in your fragment you don't have to worry about which one was used -- it's the same for either path.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic