ServletResponse not being committed on Tomcat 4.1
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Haig_Haig
Posted On:   Thursday, February 27, 2003 06:52 AM

Hello, I have the following application architecture: A button on a JSP is clicked, which sends the request to the Controller servlet. This servlet gets a RequestDispatcher and forwards to another specified JSP. The problem is where I check to see that the response is committed, it goes through my finally block and executes, which is not expected behaviour. This works on Tomcat 4.0. The code fragment for that is as follows: public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { RequestDispatcher rd getServletContext().getRequestDispatcher(JSP); rd.forward(req,resp); } catch    More>>

Hello, I have the following application architecture:

A button on a JSP is clicked, which sends the request to
the Controller servlet. This servlet gets a
RequestDispatcher and forwards to another specified
JSP. The problem is where I check to see that
the response is committed, it goes through my
finally block and executes, which is not expected
behaviour. This works on Tomcat 4.0.

The code fragment for that is as follows:


			
public void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
RequestDispatcher rd getServletContext().getRequestDispatcher(JSP);
rd.forward(req,resp);
}
catch (Exception e) {
System.err.println(e.getMessage());
}
finally {
try {
if (!(resp.isCommitted())) {
RequestDispatcher rdFinal = getServletContext().getRequestDispatcher(JSPERROR);
rdFinal.forward(req,resp);
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
finally {
try {
if (!(resp.isCommitted())){
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("ERROR");
}
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
}


As you can see, I have plenty of error handling, but there are no exceptions being thrown, only my response not being committed.

Isn't a response committed after a rd.forward()?

Much thanks,
Haig

   <<Less

Re: ServletResponse not being committed on Tomcat 4.1

Posted By:   James_Bayer  
Posted On:   Thursday, February 27, 2003 07:09 AM

finally{}
blocks are ALWAYS executed. what i'm confused about, is whether or not your application is bahaving correctly. for example, if you took out the code in the finally block to send output to the browser and/or redirect to an error jsp, does the JSP from your JSP constant get loaded correct?
RequestDispatcher rd getServletContext().getRequestDispatcher(JSP);
rd.forward(req,resp);



if so, you shouldn't have anything to worry about. if you want to follow the flow of execution better, debug it running in tomcat and step through the code using eclipse or some other ide. when you are in the finally block and start messing with the request/response objects again, it may be overriding your desired behavior in the
try{}
block.
About | Sitemap | Contact