my dog learned polymorphism
The moose likes Servlets and the fly likes IllegalStateException error come on servlet side Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "IllegalStateException error come on servlet side" Watch "IllegalStateException error come on servlet side" New topic

IllegalStateException error come on servlet side

pandya devarshi

Joined: Sep 04, 2008
Posts: 12
Hello firends,
when i write the code which refresh the jsp page automatically, at a time following error is come on servlet side..

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

And in the servlet error line is


And my servlet code is like this

public void execute(HttpServletRequest request,HttpServletResponse response){
System.out.println("Inside execute method of ListCameraAction");
ResultObject resultObject = new ResultObject(MReporterResponceCode.UNKNOWN_ERROR_CODE,null);
String pagesize = request.getParameter("cmbPageSize");
String start = request.getParameter("start");
log.debug("Start : " + start);

session = request.getSession(false);
activityLog logdb = (activityLog)session.getAttribute("ActivityLog");
if(logdb == null){
logdb = new activityLog("WEB");
UserDAO userDAO=null;
logdb.InsertActionDetails("INFO", "List", "List All Cameras");
userDAO = (UserDAO)session.getAttribute(MReporterConstant.USERDAO_OBJECT);
logID =Integer.toString(userDAO.getUserId());
resultObject = getUserCamera(request,response);
if(resultObject.getResponseCode()!=null && resultObject.getResponseCode().trim().equalsIgnoreCase(MReporterResponceCode.SUCCESS_RESPONSE_CODE)){
if(pagesize == null){
//pagesize = Integer.toString(((Collection)resultObject.getResponseObject()).size());
pagesize = Integer.toString(MReporterConstant.PAGE_SIZE);

log.debug("Page Size :: " + pagesize);
log.error("Not a success response code " + resultObject);
//String logID = (String)session.getAttribute("logID");
if(logID != null){
WebAccessLogDAO accessLog = WebAccessLogDAO.getWebAccessLog(Integer.parseInt(logID));
session.setAttribute("ActivityLog", logdb);
log.debug(" Out from try---------------------------------------------");
}catch(Exception ex){
log.error("Exception in ListUsersAction:execute() method " + ex);
}//end try-catch
log.debug("Out from execute method of ListUsersAction");

any one give me solution....

sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

The exception says: You have started writing into the response object and then you try to forward request to another JSP file.

Make sure there you don't write any thing to response object (out.write or out.println) before you forward.

One more thing, did you paste the code of same servlet? because I don't see the line request.getRequestDispatcher("/jspusermgmtListCamera.jsp").forward(request,response); in servlet.

[Servlet tutorial] [Servlet 3.0 Cook Book]
Nitin Pathak
Ranch Hand

Joined: Sep 25, 2006
Posts: 70
RequestDispatcher does not help your control by exiting from the servlet (it does NOT exit from the servlet and return the control back to the point of invocation). Since, you are not writing anything to the response object, the ideal solution is to 'return' after forward has been executed.
[ December 15, 2008: Message edited by: Nitin Pathak ]
Nitin Pathak
Ranch Hand

Joined: Sep 25, 2006
Posts: 70
I am not aware whether you managed to solve your problem, but here is JavaRanch faq for your beloved exception.

Hope this would make things clearer!
I agree. Here's the link:
subject: IllegalStateException error come on servlet side
It's not a secret anymore!