File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalStateException error come on servlet side

 
pandya devarshi
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

request.getRequestDispatcher("/jspusermgmtListCamera.jsp").forward(request,response);

And my servlet code is like this

public void execute(HttpServletRequest request,HttpServletResponse response){
System.out.println("Inside execute method of ListCameraAction");
try{
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");
}
logdb.InsertAction("ListCameras");
UserDAO userDAO=null;
logdb.InsertActionDetails("INFO", "List", "List All Cameras");
userDAO = (UserDAO)session.getAttribute(MReporterConstant.USERDAO_OBJECT);
if(userDAO!=null){
logID =Integer.toString(userDAO.getUserId());
}
request.setAttribute("USERID",logID);
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);
request.setAttribute("cameraList",(Collection)resultObject.getResponseObject());
request.setAttribute("pageSize",pagesize);
request.setAttribute("start",start);
request.getRequestDispatcher("/jsp/usermgmt/ListCamera.jsp").forward(request,response);
}else{
log.error("Not a success response code " + resultObject);
}
//String logID = (String)session.getAttribute("logID");
if(logID != null){
WebAccessLogDAO accessLog = WebAccessLogDAO.getWebAccessLog(Integer.parseInt(logID));
accessLog.setTranscript(logdb.getLog());
accessLog.upDate();
}
session.setAttribute("ActivityLog", logdb);
log.debug(" Out from try---------------------------------------------");
}catch(Exception ex){
log.error("Exception in ListUsersAction:execute() method " + ex);
ex.printStackTrace();
}//end try-catch
log.debug("Out from execute method of ListUsersAction");
}

any one give me solution....

Regards
Devarshi
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitin Pathak
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic