Servlet:响应提交后无法转发 [英] Servlet: Cannot forward after response has been committed

查看:1529
本文介绍了Servlet:响应提交后无法转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理基于 geo-location 的内容的servlet页面,并且我想同时使用 sendRedirect forward 在一起;例如;您可以从 France ; 来浏览 example.com/aPage.jsp 首先,我希望servlet将您重定向到 example.com/fr/aPage.jsp ,然后转发给资源 page。



这就是我在servlet中的内容:

  public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {
....
response.sendRedirect(REDIRECT_URL_BASED_ON_GEO);
//重定向转发资源页面后
RequestDispatcher view = request.getRequestDispatcher(RESOURCES_PAGE);
view.forward(request,response);
...
}

但是我得到:

  java.lang.IllegalStateException:在提交响应后无法转发

我知道错误出现,因为我不能同时使用 sendRedirect 转发一个接一个,但我不知道如何达到我想要的(如上所述)没有这个。



任何帮助?

解决方案

  response.sendRedirect(REDIRECT_URL_BASED_ON_GEO); 
//重定向后转发资源页面

在该行之后,您的响应开始写入clinet。



您正在尝试向其添加更多数据。


服务器的已经完成,编写响应标题并写入内容的主体,以及点你试图写更多的头 - 当然它不能倒带。


所以,在处理servlet的时候, / p>

重定向转发添加return语句之前完成逻辑。执行结束。


I'm working on servlet page that renders content based on geo-location, and I want to use both sendRedirect and forward together; e.g; you browse example.com/aPage.jsp from France; first I want the servlet to redirect you to example.com/fr/aPage.jsp and then forward you to the resources page.

This is what I have in my servlet:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  ....
  response.sendRedirect(REDIRECT_URL_BASED_ON_GEO);
  // after redirect forward the resources page
  RequestDispatcher view = request.getRequestDispatcher(RESOURCES_PAGE);
  view.forward(request, response);
  ...
}

But I get:

java.lang.IllegalStateException: Cannot forward after response has been committed

I know the error appears because I can't use both sendRedirect and forward one after another, but I don't know how to achieve what I want (as described above) without this.

any help?

解决方案

 response.sendRedirect(REDIRECT_URL_BASED_ON_GEO);
  // after redirect forward the resources page

After that line , Your response start writing to clinet.

And you are trying to add additional data to it.

The server has already finished writing the response header and is writing the body of the content, and which point you are trying to write more to the header - of course it cant rewind.

So,Thumb rule while dealing with servlet is

Finish your logic before redirect or forward add return statement.So execution ends there .

这篇关于Servlet:响应提交后无法转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆