File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "IllegalStateException" Watch "IllegalStateException" New topic
Author

IllegalStateException

sai prasanna
Ranch Hand

Joined: May 02, 2005
Posts: 167
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

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
sai prasanna
Ranch Hand

Joined: May 02, 2005
Posts: 167
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

Joined: May 02, 2005
Posts: 167
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
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.
 
Consider Paul's rocket mass heater.
 
subject: IllegalStateException
 
Similar Threads
problem with download manager in IE browser.
How can one say response is committed!
Message display during applet loading...
response.redirect issue
Writing the Response - Trying to generate an exception