ib.salesforce
ib.salesforce

Reputation: 11

How to redirect request from a filter to desired servlet with post DataParameter?

I am using following code to redirect a request from filter to servlet and jsp by using following snippet of code :-

   String redirectedServlet = "servletLcation"+"?parameter=abc";
   response.sendRedirect(redirectedServlet.trim());

Here parameter is passing as get method. I want to pass this parameter as a post method.

Is any way to do this ? My finding is as of now there is no way to send any parameter to any servlet by using response.sendRedirect() . response.sendRedirect() only support get method to pass parameter from one servlet to another servlet.

Thanks,

Upvotes: 1

Views: 5012

Answers (2)

rapadura
rapadura

Reputation: 5300

To pass a parameter from one servlet to another servlet, put it in the request as an attribute, and read the attribute on the recieving server.

So, before you do rd.forward you have to do request.setAttribute("hey", requestedPage.trim()) then in the receiving servlet, pick it up, request.getAttribute("hey"); and do something with it.

And by the way, if you are using a filter to forward to servlets or jsp, you are doing it horribly wrong. A filter should only mangle data around, not select which servlet/jsp to invoke. A filter is not meant to act as a controller, the servlet is.

Upvotes: 0

Bozho
Bozho

Reputation: 597036

That's correct - you can't send redirect with POST.

If you need it, you can use forwarding (server-side redirect), which will preserve the http method request.getRequestDispatcher("/targetUri").forward(req, resp)

But use redirection in filters carefully - normally you do that if you want to restrict some access.

Upvotes: 1

Related Questions