It's not a secret anymore!*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "IllegalStateException error come on servlet side" Watch "IllegalStateException error come on servlet side" New topic
Author

IllegalStateException error come on servlet side

pandya devarshi
Greenhorn

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

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

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!
 
wood burning stoves
 
subject: IllegalStateException error come on servlet side
 
Similar Threads
detect the session attributes after the session is TimeOut?
IllegalStateException: Cannot Forward(Servlet 2.3, SRV 8.4)
Hibernate MVC Can't create new Client
execute method of Struts 1.3.9 Action
Get values from a servlet