如何在 Java servlet 中以编程方式提交表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1691296/
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-10-29 17:37:35  来源:igfitidea点击:

How to submit a form programmatically in Java servlet?

javaformsservlets

提问by Sapience

I have a java servlet which get the form request from one webpage in domain A, and it would deal with the form, and would send the result in another form as a request to another webpage in domain B.

我有一个 java servlet,它从域 A 中的一个网页获取表单请求,它将处理表单,并将结果以另一种形式作为请求发送到域 B 中的另一个网页。

I'm wondering how to submit the form programmatically in Java servlet? I tried to use

我想知道如何在 Java servlet 中以编程方式提交表单?我试着用

javax.servlet.RequestDispatcher.forward(request, response)

but it doesn't work because it can only forward to a resource in the same domain.

但它不起作用,因为它只能转发到同一域中的资源。

回答by OscarRyz

Try using Apache HttpClientfor that

尝试使用Apache的HttpClient的

From the tutorialthe code looks like:

教程中,代码如下所示:

HttpClient client = new HttpClient();
GetMethod method = new PostMethod(url);
int statusCode = client.executeMethod(method);
... etc 

There are ton's of options to customize it.

有很多选项可以自定义它。

回答by lsiu

Try a javascript auto submit form returned by Servlet on domain A.

尝试由域 A 上的 Servlet 返回的 javascript 自动提交表单。

Servlet on domain A:

域 A 上的 Servlet:

public void doPost(HttpServletRequest req, HttpServletResponse resp) {
  PrintWriter p = resp.getPrintWriter();
  p.print("<form id='f' action=[URL on domain B to login]><input type='secret' name='username' value='" + username+ "'/><input type='secret' name='password' value='" + password + "'/></form>");
  p.print("<script type='text/javascript'>document.getElementById('f').submit()");
}

This will not be the most elegant solution, but if you are looking for something more enterprisy, try SSO solution such as OpenSSO or CAS.

这不是最优雅的解决方案,但如果您正在寻找更具企业精神的解决方案,请尝试 SSO 解决方案,例如 OpenSSO 或 CAS。

回答by ZZ Coder

You need to do an auto-post to the new domain. Just forward the request to a JSP like this,

您需要自动发布到新域。只需将请求转发到这样的 JSP,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<body onload="document.forms[0].submit()">
<noscript>
<p>
<strong>Note:</strong> Since your browser does not support JavaScript,
you must press the Continue button once to proceed.
</p>
</noscript>

<jsp:useBean id="myBean" scope="request" class="example.myBean" />

<form action="<jsp:getProperty name="myBean" property="url"/>" method="post">
<div>
<input type="hidden" name="field1" value="<jsp:getProperty name="myBean" property="field1"/>"/>
...
</div>
<noscript>
<div>
<input type="submit" value="Continue"/>
</div>
</noscript>
</form>
</body>
</html>

The "myBean" contains the redirect URL and the field value needs to be posted to the other domain.

“myBean”包含重定向 URL,字段值需要发布到其他域。

回答by BalusC

Big question is here: do you want to hand over the request to the other site and not worry about the response further? Or do you still want to have full control over the response so that you can present as if it is done under your own domain? This wasn't made clear in your topicstart.

大问题就在这里:您是否想将请求移交给另一个站点而不用担心进一步的响应?或者您是否仍然希望完全控制响应,以便您可以像在您自己的域下完成一样呈现?这在您的 topicstart 中没有明确说明。

If the first, then use the aforesuggested Javascript auto-submit-onload approach. If the second, then use the aforesuggested HttpClientsuggestion (or if you know HTTP well enough, you could also use java.net.URLConnection).

如果是第一种,则使用前面建议的 Javascript 自动提交加载方法。如果是第二个,则使用上述HttpClient建议(或者,如果您对 HTTP 足够了解,也可以使用java.net.URLConnection)。