gpt4 book ai didi

java - 使用发送重定向到 JSP

转载 作者:行者123 更新时间:2023-12-01 16:33:20 25 4
gpt4 key购买 nike

重定向到 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)

最佳答案

您正在转发到其他一些 JSP,因此它会提交响应

request.getRequestDispatcher("doctor_result.jsp").forward(request, response);

现在您正在尝试发回已提交的响应

response.sendRedirect("doctor_result.jsp");

无效

您应该转发或重定向相同的响应实例

<小时/>

另请参阅

关于java - 使用发送重定向到 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12086420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com