簡體   English   中英

在 post 方法中發送重定向到 url

[英]Send redirect to url in post method

我正在嘗試添加參數並重定向到僅接受 post 方法中的請求的頁面。 我在我的 servlet 中使用此代碼,它沒有將我轉發到 url。

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String url = "http://www.thisone.com";
    InputStream in = null;
             try {
                    HttpClient client = new HttpClient();
                    PostMethod method = new PostMethod(url);

                    //Add any parameter if u want to send it with Post req.
                    method.addParameter("User", "xyz");
                    method.addParameter("Name", "abc");

                    int statusCode = client.executeMethod(method);
                    System.out.println(statusCode); 
                    if (statusCode != -1) {
                        response.sendRedirect(response.encodeRedirectURL(url));
                        in = method.getResponseBodyAsStream();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
} 

我認為使用這種方法不可能使用 post 方法進行重定向。 您可以做的是獲得對客戶端的響應,因此設置位置標頭作為響應。 下面給出了相同的實現:

一旦你的條件得到滿足:

response.setStatus(307);
response.addHeader("Location", "<url>");

還可以查看 307 狀態碼的意義。

這是 javascript 代碼,它從一個 ajax 請求開始並點擊一個 servlet 以獲取所需的 URL,一旦它接收到 URL,創建一個 HTML 表單對象,設置值並提交表單......

<script>
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            callURL(this.responseText);
        }
    };
    xhttp.open("GET", "TestServlet", true);
    xhttp.send();

    function callURL(url){
        var form = document.createElement("form");
        form.setAttribute('method', 'POST');
        form.setAttribute('action', url);
        form.setAttribute('id', 'frmProduct');
        form.style.display = 'none';

        var i = document.createElement('input');
        i.setAttribute('type', 'text');
        i.setAttribute('name', 'name');
        i.setAttribute('value', 'Neeraj');
        form.appendChild(i);
        document.getElementsByTagName('body')[0].appendChild(form);
        form.submit();
    }
</script>

下面是我的Testservlet的實現

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = "http://www.thisone.com";
        PrintWriter out = response.getWriter();
        out.print(url);
    }
}

您可以使用 POST 方法使用自動表單提交來做同樣的事情。

請在下面找到示例代碼:

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    sendPOSTRedirect(request, response);
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    sendPOSTRedirect(request, response);
}

private void sendPOSTRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html");
    String postURL = "url to send data";
    String value1 = "value for name1";
    String value2 = "value for name2";
    String content = "<html><body onload='document.forms[0].submit()'><form action=\"" + postURL + "\" method=\"POST\">"
            + "<INPUT TYPE=\"hidden\" NAME=\"name1\" VALUE=\"" + value1 + "\"/>"
            + "<INPUT TYPE=\"hidden\" NAME=\"name2\" VALUE=\"" + value2 + "\"/>"
            + "</form></body></html>";

    response.setStatus(HttpServletResponse.SC_OK);
    PrintWriter out = response.getWriter();
    out.write(content);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM