0

当我尝试验证请求参数时,Tomcat 向我显示错误 500。

堆栈跟踪:

HTTP Status 500 - Cannot forward after response has been 
type Exception 
message Cannot forward after response has been 
description The server encountered an internal error that prevented it from 
fulfilling this request. exception

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

这是我的 servlet 的一部分(没有 PrintWriter.flash 或类似的东西)。

此代码位于 servlet 的开头。为什么我会收到IllegalArgumentException“已经提交”的回复?

String login = req.getParameter("login");
String password = req.getParameter("password");

if (login.length() == 0 || password.length() == 0) {
    req.setAttribute("wrong", "Your login or password is empty");

    RequestDispatcher rd = req.getRequestDispatcher("jsp/index.jsp");
    rd.forward(req, resp);
}
4

1 回答 1

1

return在调度页面之后添加。这将解决您的异常。

    RequestDispatcher rd = req.getRequestDispatcher("jsp/index.jsp");
    rd.forward(req, resp);
    return;

原因:

当您使用 ' forward' 或 ' include' 时,将相同的requestandresponse传递给该资源。如果关闭输出流,则不能使用它JSP来返回数据。一旦任何数据被写入client,响应已经committed

于 2013-02-19T14:41:45.400 回答