错误信息:
严重: Servlet.service() for servlet ShowBaby threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:312)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at com.shyy.jb.epet.servlet.ShowBabyServlet.doGet(ShowBabyServlet.java:37)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.shyy.jb.epet.Filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:32)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
源代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id=request.getParameter("id");
if (null==id||id.equals("")) {
request.setAttribute("error", "没有指定宠物编号!");
// response.sendRedirect("/WEB-INF/jsp/error.jsp");//重定向,在浏览器端改变地址栏地址
request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request, response);//转发,在服务器端跳转,地址不变
}
Integer petId=Integer.valueOf(id);
PetInfoBiz biz=new PetInfoBizImpl();
PetInfo petInfo=biz.load(petId);//根据ID加载宠物信息
if (null==petInfo) {
request.setAttribute("error", "没有指定编号的宠物记录!");//没有这个ID的宠物则跳转到错误页面
request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request, response);
}
request.setAttribute("item", petInfo);
request.getRequestDispatcher("/WEB-INF/jsp/pet/petInfo.jsp").forward(request, response);
}
这是一个简单的Servlet,我在地址栏敲入的是:
http://localhost:8080/epet/ShowBaby.do?id=4
数据库里没有4这个ID,理论上来说应该跳转到错误页面的,可是没有。
错误原因:
if{……}else{……}这是逻辑判断语句,只有if{}而没有else{}有时候它还会继续执行外面的语句,所以导致错误
解决办法:
if (null==petInfo) {
request.setAttribute("error", "没有指定编号的宠物记录!");//没有这个ID的宠物则跳转到错误页面
request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request, response);
}else{
request.setAttribute("item", petInfo);
request.getRequestDispatcher("/WEB-INF/jsp/pet/petInfo.jsp").forward(request, response);
}
}
加else{}
if (null==petInfo) {
request.setAttribute("error", "没有指定编号的宠物记录!");//没有这个ID的宠物则跳转到错误页面
request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request, response);
return ;
}
request.setAttribute("item", petInfo);
request.getRequestDispatcher("/WEB-INF/jsp/pet/petInfo.jsp").forward(request, response);
}