How to pass a request parameter from one servlet to a other while forwaring
When in doubt, always go to the specification. In this case, see chapter 9.1.1 Query Strings in Request Dispatcher Paths
The
ServletContext
andServletRequest
methods that createRequestDispatcher
objects using path information allow the optional attachment of query string information to the path. For example, a Developer may obtain aRequestDispatcher
by using the following code:String path = "/raisins.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 aRequestDispatcher
are scoped to apply only for the duration of the include or forward call.
So you can very well do
RequestDispatcher rd = getServletContext().getRequestDispatcher("/report?p="+value);
rd.forward(request, response);
And the parameter p
will be available only for HttpServletRequest
that is given to the resource mapped to handle the specified path, ie. /report
in this case. If that is a HttpServlet
, you can then access it with
request.getParameter("p");
where request
would be the HttpServletRequest
method parameter.
When the forward(..)
call terminates and execution comes back to your PreProcessingServlet
, the parameter will no longer be available in the local HttpServletRequest
object.
Gabriel Glenn
Updated on May 12, 2020Comments
-
Gabriel Glenn about 4 years
There is my goal that I can't achieve for now :
I have one servlet, say 'ReportServlet'. It takes a request parameter, say 'p'. I can obviously get the parameter by :
request.getParameter("p");
The query string in my JSP is :
<a href="<c:url value="/report"/>?p=value">report</a>
And everythings works fine.
Now : I have another servlet, say 'PreProcessingServlet'. I want to forward PreProcessingServlet to ReportServlet, passing a 'p' parameter which is computed in PreProcessingServlet. I tried :
RequestDispatcher rd = getServletContext().getRequestDispatcher("/report?p="+value); rd.forward(request, response);
But the parameter 'p' goes in the request's queryString member, not in the parameters.
How can I pass the 'p' parameter, using query parameter in the way that I can retrieve 'p' the same way from the JSP and from the forward.
I don't want to use a request attribute because I want a unique solution to get the parameter from both a JSP and a forward.
I guess I'm missing something, but I can't find what !
-
Gabriel Glenn about 10 yearsThank you very much for pointing out this part of the spec. That's what I was looking for. So it can work. I'm going to check this out. Thanks again.
-
Gabriel Glenn about 10 yearsOK, my bad : It was working just fine... I guess I must have been confused by eclipse debugger inspector or something. Thanks a lot : It helps me take my head out of the bucket :)
-
kerafill over 8 yearsIs it bad practice to pass parameters via the query string used to create the RequestDispatcher? Or is that acceptable?
-
Sotirios Delimanolis over 8 years@kerafill I don't think it's bad practice, but you're now passing around information as if it had come from the client. That might be confusing.
-
kerafill over 8 years@SotiriosDelimanolis Yes, I agree. I decided to pass them via setAttribute instead. My servlet checks for any get/post parameters, then pass the values via setAttribute and forward it to a .jsp view. The .jsp doesn't check anymore in the param[], but from requestScope[]. It's cleaner that way. I also don't need to escape any URL coz setAttribute handles nonordinary characters.
-
Andrew T Finnell over 5 yearsIt should be noted that the window.location.search does not get updated with the query parameters. They are only set on the request itself. So you can't just forward or include an HTML page that doesn't have access to the HttpServletRequest itself.