java 将响应和请求从 JSP 发送到 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13032269/
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
Send response and request from JSP to servlet
提问by nimrod
How can I send my response and request object from a jsp file to a servlet by code? I don't want to submit a form or so.
如何通过代码将我的响应和请求对象从 jsp 文件发送到 servlet?我不想提交表格左右。
I tried it with:
我试过:
response.setRedirect("my page"):
But then it says:
但随后它说:
Exception in thread "main" org.apache.http.client.HttpResponseException: Moved Temporarily
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:68)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:945)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:919)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:910)
at com.xx.xx.client.Client.sendPOSTRequest(Client.java:185)
at com.xx.xx.client.Client.main(Client.java:46)
As clarification: I have a client that sends a post request to a JSP file. This JSP file parses a file and puts the needed information into the session. I want to call a servlet from this jsp file to add something into the database. I think this error code is thrown by this line String responseBody = httpclient.execute(httppost, responseHandler);
作为澄清:我有一个客户端将发布请求发送到 JSP 文件。这个 JSP 文件解析一个文件并将所需的信息放入会话中。我想从这个 jsp 文件调用一个 servlet 来向数据库中添加一些东西。我认为这个错误代码是由这一行抛出的String responseBody = httpclient.execute(httppost, responseHandler);
采纳答案by MaVRoSCy
you can use the RequestDispatcherforward(ServletRequest request, ServletResponse response)
您可以使用RequestDispatcherforward(ServletRequest request, ServletResponse response)
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
将来自 servlet 的请求转发到服务器上的另一个资源(servlet、JSP 文件或 HTML 文件)。
You can do it like this:
你可以这样做:
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/YourServlet");
rd.forward(request, response);
UPDATE
更新
Also note on your code that you have response.setRedirect
instead of response.sendRedirect(...)
but note that this method will not work as you want it to because it just asks the browser to make a new request to your servlet rather than forward your request
and response
object to that servlet . See RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()for more info
您的代码还请注意,你必须response.setRedirect
代替response.sendRedirect(...)
,但请注意,这种方法是行不通的,你想要它,因为它只是要求浏览器发送到你的servlet一个新的请求,而不是你的前进request
和response
对象来表示的servlet。有关更多信息,请参阅RequestDispatcher.forward() 与 HttpServletResponse.sendRedirect()
回答by BalusC
You can just use <jsp:include>
on a servlet URL.
您可以只<jsp:include>
在 servlet URL 上使用。
<jsp:include page="/servletURL" />
The servlet doXxx()
method will just be invoked with the current request/response. Note that the servlet cannot forward to another JSP afterwards. It has to write directly to the response, or to set some request/session attributes which the JSP can intercept on afterthe <jsp:include>
line.
servletdoXxx()
方法将只用当前的请求/响应调用。请注意,servlet 之后无法转发到另一个 JSP。它必须直接写入响应,或者设置一些请求/会话属性,JSP 可以在该<jsp:include>
行之后拦截这些属性。
Note that this is bad design. You're abusing a JSP as a front controller. It should be the other way round. The servlet should act as a front controller and the JSP should act as a view. The client should send the request to the servlet URL directly instead of to some JSP file. The servlet should perform the business job and finally forward to a JSP to let it present the results in HTML. See also our servlets tag wiki pagefor some Hello World examples.
请注意,这是糟糕的设计。您正在滥用 JSP 作为前端控制器。应该反过来。servlet 应充当前端控制器,而 JSP 应充当视图。客户端应该将请求直接发送到 servlet URL,而不是发送到某个 JSP 文件。servlet 应该执行业务工作,最后转发到 JSP 让它以 HTML 形式呈现结果。另请参阅我们的 servlets 标记 wiki 页面以获取一些 Hello World 示例。