重定向到 jsp 时出现问题。我已将我的 servlet 代码放在下面。而且我也放了错误日志。任何人都可以帮我解决这个问题。如果您需要更多详细信息,请在下面评论。我在其他 servlet 中做了同样的事情它正在工作。
public class DoctorServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sDocId=request.getParameter("doctorId");
Integer dId=Integer.parseInt(sDocId);
String speciality=request.getParameter("speciality");
String experience=request.getParameter("experience");
String qualification=request.getParameter("qualification");
String sempId=request.getParameter("employeeId");
Integer empId=Integer.parseInt(sempId);
String action=request.getParameter("method");
Doctors d=new Doctors();
if(action.equalsIgnoreCase("add")){
d.setDocId(dId);
d.setEmpId(empId);
d.setExp(experience);
d.setSumOfQn(qualification);
d.setSpeciality(speciality);
try {
boolean result=new Doctors().insertDoctor(d);
if(result==true){
request.setAttribute("add","sucess");
}
request.getRequestDispatcher("doctor_result.jsp").forward(request, response);
response.sendRedirect("doctor_result.jsp");
} catch (SQLException ex) {
Logger.getLogger(DoctorServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Aug 23, 2012 12:29:50 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DoctorServlet] in context with path [/AsiaHospital] threw exception
java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:483)
at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:138)
at com.asiahospital.servlet.DoctorServlet.doPost(DoctorServlet.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)