This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes Including a file dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Including a file dynamically" Watch "Including a file dynamically" New topic
Author

Including a file dynamically

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I'm working on a servlet/JSP web site and I've run into a snag. My servlet forwards to a basic JSP page (which serves as a tamplate for my site). In the area where that template should show useful content, I'd like to import another JSP (so that I can separate my content from my display template). However, I want to include a file based upon a value in the request. Here is the code I am using:

This works fine, but I'd like to be able to include a page based upon an attribue of the request, instead of forwarding to a single page. Even though the following doesn't work, this is roughly what I'm trying to do:

Does anyone know how I might be able to do something like this?
Thanks,
Corey


SCJP Tipline, etc.
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
...I want to include a file based upon a value in the request....

No problem. The page attribute of jsp:include accepts request time expressions. Thus:
<% String address = "/blah/blah.jsp"; %>
<jsp:include page="<%= address %>" />
Just replace the hardcoded address above with a method that takes a request param as an argument and determines an address. (Don't put request.getParameter there directly, since that would fail when the parameter is null or an empty string, two cases you should always check for.)
Cheers-
- Marty


Java training and consulting
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Marty,
First of all, let me say thanks - your solution worked perfectly.
However, I have another question for you. I had tried something very similar to what you showed me and it failed to work. This is what I had tried:

For some reason, this doesn't work while this does:

It seems to me that both snippets should be equivalent, but one works while the other doesn't. Would you happen to know why?
By the way, I am checking for null or empty strings, I just took that piece out to make the code displayed here simpler. (Thanks for the tip, though )
Corey
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Corey McGlone:
I had tried something very similar to what you showed me and it failed to work. This is what I had tried:


This fails, primarily, because of the quotes inside the request.getAttribute call. Values of all attributes in XML syntax MUST be wrapped in quotes. But if you do that, you have quotes inside of quotes. Aha, so you can escape them, right? Wrong, now the request.getAttribute call won't work, because it doesn't know what its call parameter is.
Hence, your this snippet will not work. Your second one, however, has quotes in the appropriate places so everything works.


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Matthew Webster
Ranch Hand

Joined: May 10, 2001
Posts: 51
Going perhaps a little of the original point, but would you know the best way to provide a JSP name to a custom tag (via attribute or whatever) and then have the handler of the tag include (or even forward) to the named JSP.
Would this even be a good idea or is it better to simply stick with <jsp:include...> ?

Regards,
Matt.
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
Marty,
First of all, let me say thanks - your solution worked perfectly.
However, I have another question for you. I had tried something very similar to what you showed me and it failed to work. This is what I had tried:


In an XML attribute, quote marks always go with the most recent one of the same type, so your first double quote before "target" goes with the one after "page=". Just use single quotes on the outside:

Cheers-
- Marty
 
GeeCON Prague 2014
 
subject: Including a file dynamically