AntoniRyszard656
asked on
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",co ntact);
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.IllegalStateExce ption: Cannot forward after response has been committed
at org.apache.catalina.core.A pplication Dispatcher .doForward (Applicati onDispatch er.java:36 8)
at org.apache.catalina.core.A pplication Dispatcher .forward(A pplication Dispatcher .java:356)
at com.servlet.Search.dispatc h(Search.j ava:117)
at com.servlet.Search.doPost( Search.jav a:91)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 760)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
at org.apache.catalina.core.A pplication FilterChai n.internal DoFilter(A pplication FilterChai n.java:247 )
at org.apache.catalina.core.A pplication FilterChai n.doFilter (Applicati onFilterCh ain.java:1 93)
at org.apache.catalina.core.S tandardWra pperValve. invoke(Sta ndardWrapp erValve.ja va:256)
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 3)
at org.apache.catalina.core.S tandardPip eline.invo ke(Standar dPipeline. java:480)
at org.apache.catalina.core.C ontainerBa se.invoke( ContainerB ase.java:9 95)
at org.apache.catalina.core.S tandardCon textValve. invoke(Sta ndardConte xtValve.ja va:191)
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 3)
at org.apache.catalina.core.S tandardPip eline.invo ke(Standar dPipeline. java:480)
at org.apache.catalina.core.C ontainerBa se.invoke( ContainerB ase.java:9 95)
at org.apache.catalina.core.S tandardCon text.invok e(Standard Context.ja va:2422)
at org.apache.catalina.core.S tandardHos tValve.inv oke(Standa rdHostValv e.java:180 )
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 3)
at org.apache.catalina.valves .ErrorDisp atcherValv e.invoke(E rrorDispat cherValve. java:171)
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 1)
at org.apache.catalina.valves .ErrorRepo rtValve.in voke(Error ReportValv e.java:163 )
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 1)
at org.apache.catalina.core.S tandardPip eline.invo ke(Standar dPipeline. java:480)
at org.apache.catalina.core.C ontainerBa se.invoke( ContainerB ase.java:9 95)
at org.apache.catalina.core.S tandardEng ineValve.i nvoke(Stan dardEngine Valve.java :174)
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 3)
at org.apache.catalina.core.S tandardPip eline.invo ke(Standar dPipeline. java:480)
at org.apache.catalina.core.C ontainerBa se.invoke( ContainerB ase.java:9 95)
at org.apache.coyote.tomcat4. CoyoteAdap ter.servic e(CoyoteAd apter.java :199)
at org.apache.coyote.http11.H ttp11Proce ssor.proce ss(Http11P rocessor.j ava:828)
at org.apache.coyote.http11.H ttp11Proto col$Http11 Connection Handler.pr ocessConne ction(Http 11Protocol .java:700)
at org.apache.tomcat.util.net .TcpWorker Thread.run It(PoolTcp Endpoint.j ava:584)
at org.apache.tomcat.util.thr eads.Threa dPool$Cont rolRunnabl e.run(Thre adPool.jav a:683)
at java.lang.Thread.run(Threa d.java:534 )
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Apache Tomcat/4.1.30
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
String action = req.getParameter(ACTION_TO KEN);
if(NEW_ACCOUNT.equals(acti on)){
ContactRecord contact = setObject(req);
boolean valid = vaildateForm(contact.getEm ail());
if(!valid){
req.setAttribute("bean",co ntact);
req.setAttribute("errmsg", "A valid email addess must be entered");
dispatch("addcontact.jsp", req,res);
}
boolean found = emailSearch(contact.getEma il());
if(found){
req.setAttribute("bean",co ntact);
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);
}
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",co
req.setAttribute("errmsg",
dispatch("addcontact.jsp",
}
HTTP Status 500 -
--------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.IllegalStateExce
at org.apache.catalina.core.A
at org.apache.catalina.core.A
at com.servlet.Search.dispatc
at com.servlet.Search.doPost(
at javax.servlet.http.HttpSer
at javax.servlet.http.HttpSer
at org.apache.catalina.core.A
at org.apache.catalina.core.A
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.valves
at org.apache.catalina.core.S
at org.apache.catalina.valves
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.coyote.tomcat4.
at org.apache.coyote.http11.H
at org.apache.coyote.http11.H
at org.apache.tomcat.util.net
at org.apache.tomcat.util.thr
at java.lang.Thread.run(Threa
--------------------------
Apache Tomcat/4.1.30
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
String action = req.getParameter(ACTION_TO
if(NEW_ACCOUNT.equals(acti
ContactRecord contact = setObject(req);
boolean valid = vaildateForm(contact.getEm
if(!valid){
req.setAttribute("bean",co
req.setAttribute("errmsg",
dispatch("addcontact.jsp",
}
boolean found = emailSearch(contact.getEma
if(found){
req.setAttribute("bean",co
req.setAttribute("errmsg",
dispatch("addcontact.jsp",
}
}
}
public void dispatch(String jsp, HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
RequestDispatcher disp = req.getRequestDispatcher("
disp.forward(req, res);
}
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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);
}
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("
disp.forward(req, res);
}
ASKER
I just wondered if these was a disadvantage of using the return.
ASKER
Sorry I meant
I just wondered if there was any disadvantage of using return.
I just wondered if there was any disadvantage of using return.
IMO, return in any way is correct.
solution: keep the found condition inside the else of !valid. (but it all depends upon your requirement)