This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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?
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.
Joined: Jun 23, 2005
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.