File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalStateException

 
sai prasanna
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am getting IllegalStateException when iam using response.sendRedirect.I have not
used response.flushBuffer() in that page. And i have caught the Exception also. I
have tried and got 2 solutions also
1) response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location","/sivashakthi/general/editError.jsp?path="+request.getRequestURI()+"&message="+message);
generally this is working fine but when iam using in place of sendRedirect
where iam getting illegal it is not working nothing is making difference
2) changed the buffer size=12
its working fine but my superior needs sol by coding it seems.
for coding i have to pass through url but sendRedirect is not working

any body plz help
[ September 03, 2005: Message edited by: sai prasanna ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither response.sendRedirect nor requestDispatcher.forward will stop the execution of your page on their own.

For this reason, you should either branch your code in such a way that no code is reached after calling either of them or put a return statement just afer makeing the call.


If that wasn't the problem, post your code so we can take a look.
[ September 03, 2005: Message edited by: Ben Souther ]
 
sai prasanna
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Neither response.sendRedirect nor requestDispatcher.forward will stop the execution of your page on their own.

For this reason, you should either branch your code in such a way that no code is reached after calling either of them or put a return statement just afer makeing the call.


If that wasn't the problem, post your code so we can take a look.

[ September 03, 2005: Message edited by: Ben Souther ]
 
sai prasanna
Ranch Hand
Posts: 167
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sai prasanna:

If that wasn't the problem, post your code so we can take a look.

[ September 03, 2005: Message edited by: Ben Souther ]<hr></blockquote>[/QB]



Thanks Ben
I even tried with this code and iam getting IllegalStateException. My code is

i used jsp aram

<% if("Update".equalsIgnoreCase(action)){
message = "Vehicle/Insurance Taxes Updation ";
if(vehicle!=null && insuranceTax!=null &&
vehicle.getVehicleNo()!=null &&
vb.upDateVehicle(vehicle) &&
itb.updateInsuranceTaxes(insuranceTax)){
flag=false;
try{
response.sendRedirect("/sivashakthi/transport/edit/vehicleMasterEdit.jsp?action="+action+"&count=1");
}catch(Exception ise){
ise.printStackTrace();
}
}else{
response.sendRedirect("/sivashakthi/general/updateError.jsp?path="+request.getRequestURI()+"&message="+message);
}
}else if("Delete".equalsIgnoreCase(action)){
message = "Vehicle/Insurance Taxes Deletion ";
if(vehicle!=null && insuranceTax!=null && vehicle.getVehicleNo()!=null && vb.deleteVehicle(vehicle) && itb.deleteInsurance(insuranceTax)){
flag=false;
try{
response.sendRedirect("/sivashakthi/transport/edit/vehicleMasterEdit.jsp?count=1");

}catch(IllegalStateException ise){
ise.printStackTrace();
}
}else{
try{
//if(!response.isCommitted())
//System.out.println("un committed");
//else
//System.out.println("committed");

//or

//response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
//response.setHeader("Location","/sivashakthi/general/editError.jsp?path="+request.getRequestURI()+"&message="+message");

if(flag){
response.sendRedirect("/sivashakthi/general/editError.jsp?path="+request.getRequestURI()+"&message="+message);
return;
}

}catch(Exception e){
e.printStackTrace();
}
}
}
}else{
response.sendRedirect("/sivashakthi/general/sorry.jsp");
}
%>
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's tricky to use redirect within a JSP, because the page generates output, after which a redirect is no loger possible. It would be a better design to determine which action/path to take somewhere in a servlet or Java class, and then to do a forward or redirect from there to the appropriate JSP page.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai,
Make it very simple for yourself.
After every call to response.sendRedirect, add a return statement.

If you must test the output stream after a redirect, make sure to add a return statement before closing your scriptlet tag. Otherwise the JSP compiler will may add println statements that will cause the IllegalStateException.

JSPs are best used for formatting your output.
Servlets are better for handling control flow.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic