• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

JSP include

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!

I'm looking for a way to include another jsp page in a jsp page without passing in all this page's parameters.

I know you can include a page with <jsp:include page="nextpage.jsp"> or <%@ include file="nextpage.jsp" %>, but the problem is that in nextpage.jsp if you call request.getParameter(...), it will have all the parameters that the original page has.

Is there some way to not pass in all the parameters automatically?

Thanks for your ideas.
 
Sheriff
Posts: 67754
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
By definition an include happens within the scope of the same request, so, yes, the parameters will be available.

Why is this an issue?
 
Tim Koop
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK Let's say I want to display some information on a side bar of every page on my site, and this side bar is an include to a search page (called search.jsp) that returns the latest however many things you want. Let's say it returns a list of cars for sale.

I can override the parameters like this:

Now let's say this search.jsp can also take a parameter called "new". If "new" is not null, search.jsp only returns new cars. This would be usefull if search.jsp was being called from a form and the form contained a checkbox called "new". If it wasn't checked, there would be no "new" parameter being passed in to search.jsp.

But if any page on my web site contains a parameter "new", it will be passed in to searc.jsp and it will only return new cars.

So my question is now: Is there a way to null out or remove certain parameters from being passed into an include page?
 
Bear Bibeault
Sheriff
Posts: 67754
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
I tend to use a richer namespace such that parameter names within a web app are unique (in that a name like 'new' would never be used to mean different things in different places thus eliminating the possibility of such confusion).

Or, you could be explicit in your include params, always being sure to explicitly set the value of any params that could be confusing to the included page.

Under JSP 2.0, I tend not to use include files at all, but favor creating tag files where the inputs to the tags are not implicitly passed request parameters or scoped variables, but explicitly specified tag attributes. If you're on JSP 2.0, this is an avenue you could explore.
 
Tim Koop
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK So I've concluded that it's impossible to restrict the parameters that are going into an included page, at least not without a bit of programming. If you really need this and you're not afraid of a bit of programming, do this:

Make yourself a class called CustomRequest that implements HttpServletRequest. In the constructor, pass in your request object and the new parameters you want as a Hashtable. In all the many many methods that you have to implement, simply call the request object's method on that method, except for the getParameter... methods where you should return values from your own hashtable. Make one more public static method called "include" that takes as parameters: request, response, out, the filename to include, an array of new parameter names, and an array of parameter values.

Then in this "include" method, do this:
1. flush out
2. pack your parameters from the arrays into a hashtable called params.
3. construct the uri of the include file, based on request.getRequestURI() and the filename passed in. Call it uri.
4. run the following lines of code

HttpServletRequest customRequest = new CustomRequest(request, params);
request.getRequestDispatcher(uri).include(customRequest, response);


In your jsp, call it like this:


Please don't ask me for the code; it's copywrited out of my control. And I'm going to stop monitoring this thread, so have a nice day.
 
Bear Bibeault
Sheriff
Posts: 67754
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

And I'm going to stop monitoring this thread, so have a nice day.



You're welcome. I'm so glad that I spent my time on this.
[ June 23, 2005: Message edited by: Bear Bibeault ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic