简体   繁体   English

如何从servlet到jsp获得响应?

[英]How to get response from servlet to jsp?

I am trying get response from servlet use 我正在尝试从servlet使用获得响应

request.setAttribute(error, error);
request.getRequestDispatcher("http://localhost:8080/redicted_test/Home.jsp").forward(request, response);

String redictedURL="http://localhost:8080/redicted_test/Home.jsp";
response.sendRedirect(redictedURL);

But get error 但得到错误

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

Im understand that im send response twice, but how i can do it else? 我明白我发送了两次响应,但我怎么能做到呢? Can u tell me simplest way? 你能告诉我最简单的方法吗?

You already have forwarded the request and now you are redirecting it 您已经转发了请求,现在您正在重定向它

request.getRequestDispatcher("http://localhost:8080/redicted_test/Home.jsp").forward(request, response);
..
response.sendRedirect(redictedURL);

If you want to set some attributes from servlet and need to display it on jsp, then just forward the request to jsp and display those attributes 如果你想从servlet设置一些属性并需要在jsp上显示它,那么只需将请求转发给jsp并显示这些属性

In your example you have executed the jsp and the rendered jsp was sent in the response buffer. 在您的示例中,您已执行了jsp,并且已在回复缓冲区中发送了呈现的jsp。 And since you have started to send the response, the response headers were sent. 由于您已开始发送响应,因此发送了响应标头。 But just after that you wanted to send redirection and a redirection is a kind of headers manipulation (changes http status code and location headers). 但就在此之后,您想要发送重定向,重定向是一种标头操作(更改http状态代码和位置标头)。

i think u should use include rather then forward .. 我想你应该使用include而不是forward ..

forward is used to forward a request, that is control is transfered to the
new servler/jsp. u shud take care to not put any our.println() statements 
in the servlet from where u plan to call forward method. in fact u cant even 
fire response.getWriter() method in this case. u can only do that in the servlet
to which the control is bein fwded 

Never write any thing to response if you are going to forward the request to another servlet or JSP. 如果要将请求转发给另一个servlet或JSP,切勿将任何内容写入响应。 For example, if Servlet1 forwards the request to Servlet2, Servlet1 should not write any thing to response, neither call the response.getOutputStream() method.This is not allowed and either whatever servlet1 wrote to the response will not output or you will get IllegalStateException. 例如,如果Servlet1将请求转发给Servlet2,Servlet1不应该将任何内容写入响应,也不应该调用response.getOutputStream()方法。这是不允许的,无论servlet1写入响应的是什么都不会输出,否则您将获得IllegalStateException 。

include - means that the new servlet/jsp will be procesed and any
out.println()/html stuff will be included and then control will come
back to the servlet/jsp that called include method. 

After a RequestDispatcher.forward() , the response will be committed and closed. RequestDispatcher.forward() ,响应将被提交并关闭。 You cannot write any header/content to the response. 您无法在响应中写入任何标头/内容。

That is why you cannot redirect or do operation which adds header or response content after doing RequestDispatcher.forward() . 这就是为什么在执行RequestDispatcher.forward()之后,您无法redirectdo operation which adds header or response content

The alternative you have is use include instead of forward. 你有的替代方案是使用include而不是forward。

But, i did not understand your use case. 但是,我不明白你的用例。 You are forwarding and redirecting to the same page. 您正在转发并重定向到同一页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM