aspose file tools*
The moose likes Servlets and the fly likes RequestDispatcher - how to include a jspf file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher - how to include a jspf file?" Watch "RequestDispatcher - how to include a jspf file?" New topic
Author

RequestDispatcher - how to include a jspf file?

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
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

Joined: Jan 10, 2002
Posts: 61233
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
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

Joined: Jan 10, 2002
Posts: 61233
    
  66

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

Joined: Jan 22, 2004
Posts: 401
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

Joined: Jan 10, 2002
Posts: 61233
    
  66

Why not just use a scoped variable for both scenarios? Why bother with the param?
James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
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

Joined: Jan 10, 2002
Posts: 61233
    
  66

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher - how to include a jspf file?