1

I have 2 web-pages.

So, 1st page takes some POST parameters, and then process it. I want to redirect this query(with all POST params) to my own 2nd page, if parameter "appId" = "myApp";

In start of 1st page I make next:

    if (getParameter("id") == "myApp") 
    {
        request.setHttpHeader("")  - ??? WHAT MUST BE HERE? WHICH HEADERS?
    }

P.S. I need only HTTP solution, using native (java) methods (like forward and redirect) don't help me.

Thanks.

WelcomeTo
  • 19,843
  • 53
  • 170
  • 286

2 Answers2

1

You have to user RequestDispatcher.forward. Here is an example.

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ForwardServlet extends HttpServlet{

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name = request.getParameter("name");
        /*
         * You can do any processing here. 
         * We will simply output the value of name parameter on server console.
         * 
         */
        System.out.println(name);
        String destination = "/DestinationServlet";

        RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
        rd.forward(request, response);
    }

}
Ravindra Gullapalli
  • 9,049
  • 3
  • 48
  • 70
  • @MyTitle said "using native (java) methods (like forward and redirect) don't help me." in the last line of this question – punny Mar 22 '12 at 08:06
  • @Ravindra Gullapalli, yes, thanks. I know that exist this solution. But my page in another context (so I can't use method "forward"). Also I know that I can forward request from one context to another, but I dont want to make so more changes in 1st JSP page (because its written not by me). Also I can't use "sendRedirect" method (because I need POST request). – WelcomeTo Mar 22 '12 at 08:08
  • @MyTitle Then you can do one thing. Render HTML code using ServletOutputStream and create a invisible form with a simple message "Processing your request". Then submit that form using java script. – Ravindra Gullapalli Mar 22 '12 at 09:15
1

What you're asking for can't be done with pure HTTP. You can only redirect GETs with HTTP. See this answer to a similar question https://stackoverflow.com/a/1310897/116509

Community
  • 1
  • 1
artbristol
  • 32,010
  • 5
  • 70
  • 103