JSP通过servlet重定向

时间:2012-07-26 07:41:42

标签: jsp servlets redirect

我在jsp app中有重定向问题。

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

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语句,但它不起作用。

有人可以告诉我为什么吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

java.lang.IllegalStateException: Response has already been committed

如果已写入响应,则会出现此错误。

请参阅此帖子中的answer

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

尝试:

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

EDIT(DetailedExplanation):

request.getRequestDispatcher(“url”)表示调度与当前HTTP请求相关。

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

path参数不必以“/”

开头

getServletContext().getRequestDispatcher(“url”)表示发送相对于ServletContext的根。

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

path参数必须以“/”

开头