• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Including a file dynamically

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic