java httpclient 发布
我有一个关于如何允许我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你确实走错了路。与任何“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.