The below method throw: Servlet.service() for servlet DetailCakeServlet threw exception  

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

**Code**

```
 try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        CakeDAO dao = new CakeDAO();
        String strID = request.getParameter("id");
        int id = Integer.parseInt(strID);
        request.setAttribute("cake", dao.getCakeById(id));
        request.getRequestDispatcher("/WEB-INF/detail.jsp").forward(request, response);
    }catch (Exception ex) {
        request.getRequestDispatcher("/WEB-INF/error.jsp").forward(request, response);
    }

```

But when I remove the resource out object, it works. What could be the reason ?