This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Including a file dynamically

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marty Hall
Author
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Webster
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic