Java 使用 HTTP 重定向保存 POST 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9816614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:03:10  来源:igfitidea点击:

Redirect saving POST parameters using HTTP

javahttppostredirect

提问by MyTitle

I have 2 web-pages.

我有 2 个网页。

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";

因此,第一页需要一些 POST 参数,然后对其进行处理。如果参数“appId”=“myApp”,我想将此查询(带有所有 POST 参数)重定向到我自己的第二页;

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.

PS 我只需要 HTTP 解决方案,使用本机(java)方法(如转发和重定向)对我没有帮助。

Thanks.

谢谢。

回答by Ravindra Gullapalli

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

您必须使用 RequestDispatcher.forward。这是一个例子。

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);
    }

}

回答by artbristol

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

使用纯 HTTP 无法完成您的要求。您只能使用 HTTP 重定向 GET。请参阅此对类似问题的回答https://stackoverflow.com/a/1310897/116509