tags:

views:

219

answers:

3

I want to intercept a request using the RequestDispatcher, and then I want to forward the request along to another servlet -- something like this:

RequestDispatcher dispatcher = request.getRequestDispatcher("/servlet/some.ThirdPartyServlet" + "?" + "param_name=" + "somevalue");
dispatcher.forward(request, response);

If the incoming request were a POST, will the request dispatcher take my new parameters, and include them in the message body, or does this forward now become a GET?

+3  A: 

It retains the original request, without changing it.

So, it will remain POST if it was POST.

Bozho
+1  A: 

If you use forward, then control stays within the servlet container, the request attributes are retained, and the request remains a POST. It's when you use redirect that it causes a response to be sent to the browser causing it to make a GET request, which is where it loses the request attributes for the original request because the GET is an entirely new request.

Nathan Hughes
+3  A: 

I think your concern is rather the availability of the passed-in request parameters (not attributes as others mentions). In this case it doesn't matter whether you use a forward or a redirect. But if you use a forward to another Servlet, then the appropriate method associated with the initial request as obtained by HttpServletRequest#getMethod() will be invoked. If it is POST, then doPost() will be invoked. You can still access the additional parameters in the query string the usual way by HttpServletRequest.getParameter().

So basically the following in servlet1:

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    request.getRequestDispatcher("servlet2?foo=bar").forward(request, response);
}

can basically be handled by servlet2 as follows:

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    String foo = request.getParameter("foo"); // Returns "bar".
}
BalusC