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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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)
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
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;
         }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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);
   }
AntoniRyszard656Author Commented:
I just wondered if these was a disadvantage of using the return.
AntoniRyszard656Author Commented:
Sorry I meant

I just wondered if there was any disadvantage of using return.
fargoCommented:
IMO, return in any way is correct.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.