File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes JSP include Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP include" Watch "JSP include" New topic
Author

JSP include

Tim Koop
Greenhorn

Joined: Jun 23, 2005
Posts: 8
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.


Tim Koop
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

By definition an include happens within the scope of the same request, so, yes, the parameters will be available.

Why is this an issue?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tim Koop
Greenhorn

Joined: Jun 23, 2005
Posts: 8
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

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

Joined: Jun 23, 2005
Posts: 8
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP include
 
Similar Threads
session issues with Ctrl-N
custom link tags
how to use usebean
Navigate from one JSP to another
How can add a company emblem in many pages?