0

Tomcat shows me an Error 500 when I'm trying to validate request parameters.

Stacktrace:

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

Here is piece of my servlet (there is no PrintWriter.flash or anything like that).

This code is in the beginning of the servlet. Why do I get an IllegalArgumentException on an "already commited" response?

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);
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
gabriel angelos
  • 349
  • 1
  • 9
  • 23

1 Answers1

1

Add return after Dispatch the page.That will resolve your exception.

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

REASON:

When you 'forward' or 'include', the same request and response is passed to that resource. If you close the output stream, it cannot be used by the JSP to return the data. As soon as any data gets written to the client, the response is already committed

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307