java httpclient 发布

发布于 2024-08-28 12:16:40 字数 736 浏览 6 评论 0原文

我有一个关于如何允许我的 jsp 页面向服务器发出 post 命令的问题,并且仍然让浏览器遵循发布页面的重定向。

以下是代码片段:

执行帖子的代码(位于 jsp 文件内):

HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("SUBMITTED", "submitted");
client.getParams().setParameter("xxxxxxxx", purchaser.getemail());
client.getParams().setParameter("xxxxxxxx", purchaser.getsuject());

HttpPost method = new HttpPost(url+"process.jsp");
client.execute(method); 

这是 process.jsp 的片段

if (person.getStatus() == person.ACTIVE)
  response.sendRedirect("Account.jsp);
else if (person.getStatus() == person.ERROR)
  response.sendRedirect("Error.jsp);

我希望浏览器从 process.jsp 转到休闲/转到重定向。有谁知道可以帮助我的教程,或者我是否以错误的方式进行此操作。

I have a question about how to allow my jsp page to issue a post command to the server, and still have the browser fallow the re direction of the posted page.

Here are the code snipets:

code that does the post (this is inside a jsp file):

HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("SUBMITTED", "submitted");
client.getParams().setParameter("xxxxxxxx", purchaser.getemail());
client.getParams().setParameter("xxxxxxxx", purchaser.getsuject());

HttpPost method = new HttpPost(url+"process.jsp");
client.execute(method); 

here is a snipet of process.jsp

if (person.getStatus() == person.ACTIVE)
  response.sendRedirect("Account.jsp);
else if (person.getStatus() == person.ERROR)
  response.sendRedirect("Error.jsp);

I would like the browser to the fallow/goto the redirect from the process.jsp. Does anyone know a tutorial that would help me or Am I going about this the wrong way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为一人 2024-09-04 12:16:40

你确实走错了路。与任何“JSP 内的原始 Java 代码”一样,此逻辑属于真正的 Java 类,而不是 JSP 文件。创建一个执行此操作的 Servlet,并让它依次将请求重定向/转发到感兴趣的 JSP 文件。

JSP是一种视图技术,实际上是响应体的一部分。如果您尝试在 JSP 中途更改响应,则最终只会得到 IllegalStateException:响应已提交

此外,从问题中还不清楚整个功能需求。我的印象是这两个 JSP 文件都在同一环境中运行,然后整个 HttpClient 方法是错误的。开始更多地了解 servlet。 这是一个很好的起点

You're indeed going the wrong way with this. As with any "raw Java code inside a JSP" this logic belongs in a real Java class, not in a JSP file. Create a Servlet which does this and let it in turn redirect/forward the request to the JSP file of interest.

JSP is a view technology and is actually part of the response body. If you're trying to change the response's halfway a JSP, you'll only end up with IllegalStateException: response already committed.

Further, the whole functional requirement is unclear from the question. I have the impression that the both JSP files are running in the same environment and then the whole HttpClient approach is wrong. Start learning a bit more about servlets. This is a good starting point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文