0

我在 jsp 应用程序中的重定向存在一些问题。

我的重定向方法是这样的:

public static void redirectUrl(String url,HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
    request.getSession().getServletContext().getRequestDispatcher("/" + url).forward(request,response);
}

当我在 localhost 上启动应用程序时,一切正常,但是当我将它部署到服务器上时,它会因以下异常而崩溃:

Servlet error
java.lang.IllegalStateException: Response has already been committed
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.EvermindHttpServletResponse.resetBuffer(EvermindHttpServletResponse.java:1933)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.forward(ServletRequestDispatcher.java:221)
at app.framework.request.Controller.doPost(Controller.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:835)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:341)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.ServletRequestDispatcher.forward(ServletRequestDispatcher.java:230)
at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.3.0)].server.http.GetParametersRequestDispatcher.forward(GetParametersRequestDispatcher.java:257)

我在每次重定向调用之后都放了 return 语句,但它不起作用。

有人能告诉我为什么吗?

提前致谢。

4

1 回答 1

1
java.lang.IllegalStateException: Response has already been committed

当响应已经写入时会出现此错误。

请参阅这篇文章中的答案

您使用的术语“重定向”不是根据您的代码重定向。它称为“转发”。

尝试:

request.getRequestDispatcher("/" + url).forward(request, response);

编辑(详细解释):

request.getRequestDispatcher(“url”)表示调度是相对于当前的 HTTP 请求的。

RequestDispatcher reqDispObj = request.getRequestDispatcher("/home.jsp");

路径参数不必以“/”开头

getServletContext().getRequestDispatcher(“url”)表示调度是相对于ServletContext.

RequestDispatcher reqDispObj = getServletContext().getRequestDispatcher("/ContextRoot/home.jsp");

路径参数必须以“/”开头

于 2012-07-26T07:59:03.023 回答