• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass request parameters to getRequestDispatcher()

 
brad balmer
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All examples I've seen using the RequestDispatcher look like:
RequestDispatcher rd = getServletContext().getRequestDispatcher("/something.jsp");
And then to add paramters you use:
rd.forward(request, response);
My question is can we dynamically add request parameters to the rd by doing something like:
RequestDispatcher rd = getServletContext().getRequestDispatcher("/something.jsp?uid=1234");
We are using Struts as our framework and need to add a uniqe id to each request in an attempt to stop browsers from caching (we already use all the response headers but these never stop caching 100% of the time).
Therefore I thought about appending a uid to each request.
If this won't work, is there any other way that anybody else know that I can append a request Parameter AFTER a page has been submitted?
Thanks.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brad,
Welcome to JavaRanch. You can add a query string to the URL when you get the dispatcher. Here is what the Servlet Spec says:
SRV.8.1.1 Query Strings in Request Dispatcher Paths
The ServletContext and ServletRequest methods that create RequestDispatcher
objets using path information allow the optional attachment of query string
information to the path. For example, a Developer may obtain a RequestDispatcher
by using the following code:
String path = �/raisons.jsp?orderno=5�;
RequestDispatcher rd = context.getRequestDispatcher(path);
rd.include(request, response);
Parameters specified in the query string used to create the RequestDispatcher
take precedence over other parameters of the same name passed to the included
servlet. The parameters associated with a RequestDispatcher are scoped to apply
only for the duration of the include or forward call.
 
Scott Duncan
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally, using the struts framework, I would think that your action (e.g. the class that extends Action) would return a new ActionForward object which is simple enough:
ActionForward forward = new ActionForward("/something.jsp?uid=1234", true);
return forward;
 
brad balmer
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses.
Yes, I could place this when I create my ActionForward, but I wanted a central location where it would be placed on every request.
Thanks for the info. I wasn't sure that by attaching a parameter when I called the getRequestDispatcher() it would make it through and not be chopped off.
Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!