• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

RequestDispatcher - how to include a jspf file?

 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic