RequestDispatcher exception?

Hello,

In the servlet below we are posting some data from a html form. And complete some validation checks, including a check to see if the email entered already exists.

When the email entered is found this if statement would be run. Though when the addcontact.jsp page is being called tomcat throws this exception. Could anyone possibly see a reason for the exception? Thank you

         if(found){
            req.setAttribute("bean",contact);
            req.setAttribute("errmsg", "Please enter new email");
            dispatch("addcontact.jsp",req,res);
         }

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.IllegalStateException: Cannot forward after response has been committed
      at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:368)
      at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:356)
      at com.servlet.Search.dispatch(Search.java:117)
      at com.servlet.Search.doPost(Search.java:91)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:534)



--------------------------------------------------------------------------------

Apache Tomcat/4.1.30



   public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException{

      String action = req.getParameter(ACTION_TOKEN);

      if(NEW_ACCOUNT.equals(action)){
         ContactRecord contact = setObject(req);
         boolean valid = vaildateForm(contact.getEmail());

         if(!valid){
            req.setAttribute("bean",contact);
            req.setAttribute("errmsg", "A valid email addess must be entered");
            dispatch("addcontact.jsp",req,res);
         }

         boolean found = emailSearch(contact.getEmail());

         if(found){
            req.setAttribute("bean",contact);
            req.setAttribute("errmsg", "Please enter new email");
            dispatch("addcontact.jsp",req,res);
         }
      }
}

   public void dispatch(String jsp, HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException{

      RequestDispatcher disp = req.getRequestDispatcher("/"+jsp);
      disp.forward(req, res);
   }
AntoniRyszard656Asked:
Who is Participating?
 
fargoCommented:
u should have to explicitly make a return for it. like follows

if(!valid){
            req.setAttribute("bean",contact);
            req.setAttribute("errmsg", "A valid email addess must be entered");
            dispatch("addcontact.jsp",req,res);
            return;
         }

0
 
fargoCommented:
actually both the conditions of !valid and found are getting executed. First !valid is executed, and then the dispatcher is forwarded to given jsp. But the loop doesn't end there and found condition returns true and then dispatcher forwards to addcontatct.jsp (here the response is already committed with !valid condition).

solution: keep the found condition inside the else of !valid. (but it all depends upon your requirement)
0
 
AntoniRyszard656Author Commented:
Thanks

In the servlet when we meet the conditions of !valid. I thought the servlet would call/display the addcontact.jsp and break/exist the servlet execution.

When we use the RequestDispatcher object does the servlet not end?

Thanks again
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AntoniRyszard656Author Commented:
Thanks

The examples in my text mostly used this method to forward javabeans/errormessages to the jsp.

Would you say its best to avoid the use of return, from a design/servlet correctness point of view. And use if/else statements to avoid my orginal error? Or is the use of return in this way also correct?

Thanks

   public void dispatch(String jsp, HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException{

      RequestDispatcher disp = req.getRequestDispatcher("/"+jsp);
      disp.forward(req, res);
   }
0
 
AntoniRyszard656Author Commented:
I just wondered if these was a disadvantage of using the return.
0
 
AntoniRyszard656Author Commented:
Sorry I meant

I just wondered if there was any disadvantage of using return.
0
 
fargoCommented:
IMO, return in any way is correct.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.