• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resetting/overwriting a committed HttpServletResponseresponse object

 
Fergus Red
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to overwrite a HttpServletResponse object but cannot seem to do so correctly.
What I am doing, is calling a servlet from different submit buttons in a form and depending on the submit button presser then I determine in the servlet where to send the user goes next.
Code in my servlet is as follows:
//check if user has come from button 2
if (requestURL.equals("2")) {
System.out.println("About to set redirect overwrite");
RequestDispatcher rd = null;
rd = getServletContext().getRequestDispatcher("/dreamset/registration1.jsp");
rd.forward(request, response);
}
However when I reach my next jsp page I get an error message as follows on the jsp page.

<PRE>
<B>/DispatcherServlet:</B>
java.lang.IllegalStateException: Response has already been committed
at allaire.jrun.servlet.JRunResponse.resetContent(../servlet/JRunResponse.java:172)
at allaire.jrun.servlet.JRunSE.runServlet(../servlet/JRunSE.java:1202)
at allaire.jrun.servlet.JRunRequestDispatcher.forward(../servlet/JRunRequestDispatcher.java:89)
at com.con..feServlet.DispatcherServlet.service(DispatcherServlet.java:102)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1417)
at allaire.jrun.session.JRunSessionService.service(../session/JRunSessionService.java:1082)
at allaire.jrun.servlet.JRunSE.runServlet(../servlet/JRunSE.java:1271)
at allaire.jrun.servlet.JRunRequestDispatcher.forward(../servlet/JRunRequestDispatcher.java:89)
at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1553)
at allaire.jrun.servlet.JRunSE.service(../servlet/JRunSE.java:1543)
at allaire.jrun.servlet.JvmContext.dispatch(../servlet/JvmContext.java:364)
at allaire.jrun.jrpp.ProxyEndpoint.run(../jrpp/ProxyEndpoint.java:388)
at allaire.jrun.ThreadPool.run(../ThreadPool.java:272)
at allaire.jrun.WorkerThread.run(../WorkerThread.java:75)
</PRE>

Do you kow if it is possible to uncommit a response object or somehow reset it to work as I require.
I tried the method response.reset() but this seemd to have no affect.
It actually functions as I require but I get the error in the page and obviously this is no use to me.
Any ideas or help on this would be great.
Thanks in advance.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having a response committed means that headers and possibly some body content has been transmitted - there is no way to get it back.
I think what you need is either:
1) reorganize your code so that the decision is made before anything is sent.
or
2) set your response buffer large enough that what is being written won't trigger a flush.
Look at the methods in ServletResponse.
Bill
 
Fergus Red
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know if there is a way of creating a new redirect within my servlet without using my existing HttpServletResponse object. I.E can I create a new objevct which allows me to redirect?
 
Fergus Red
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How big do you think I should set my buffersize. Not really sure what is a big or small buffersize
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since a redirect is a response that sends the client to another URL, you have to use the response in hand.
Obviously, the buffer size would have to exceed the size of what you are writing to the response if you want to be able to clear it.
Bill
 
Fergus Red
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers for all the help
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Fergus,
well, i guess your problem has been resolved already but just i saw this post today and i was having some thoughts.
1. can you use Servlet Filter? (look how to use it if your servlet deployment supports it)
2. if it is not critical for you that client can see where his/her form is getting submitted then you can have it all in the HTML you have, where you have Submit buttons. here is what i mean,
say you have <form name='f' action='' method=post>
and multiple submit buttons ( i don't know how this Multiple submit buttons work if you really meant type="Submit" in the html code for multiple buttons but thats okay..that is not a question here..i assume somehow you want to be able to submit the same form to different servlets).
you can write a javascript method for submission of the form and change the 'action' for the form depending upon which Submit button has been hit.
like,
function doB4Submit() {
if ( submitButton == "1" ) {
document.formName.action="/servlet/servlet1";
document.formName.submit();
} else if ( submitButton == "2" ) {
document.formName.action="servlet/servlet2";
document.formName.submit();
}
}

Moderators would probably redirect this discussion to JavaScript/HTML forum as my explanation involves use of JavaScript/HTML...
hope you understand what i am trying to put down here.
thank you.
maulin
 
Fergus Red
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Maulin.
As it turns out my client decided that she no longer wanted the back (submit) button on my form and therefore I do not need to worry about re-ditrecting based on which form button is submitted.
Cheers for the help though and I'll definately keep your suggestions in hands if I need to undertake a similiar task in the future
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic