ITted
自己不懂那就去麻烦一下百度大哥了,得到一个不是很明确的答案:这个是错误是由于response多次提交引起的。
既然如此,那就看看自己什么地方多次使用跳转了吧。
错误代码如下:
package com.neeke.calc.control; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import com.neeke.calc.bean.LogicBean; import com.neeke.calc.bean.EntITyBean; public class CalcServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; //InITialize global variables public void inIT() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); request.setCharacterEncoding("GBK"); String money = request.getParameter("earning").trim(); String local = request.getParameter("localTax").trim(); String temp = ""; double earning = 0; double localTax = 0; if (money != null && local != null) { if (money.equals("") || local.equals("")) { temp = "未输入收入或当地税额!"; request.setAttribute("result", temp); request.setAttribute("result", temp); request.getRequestDispatcher("/error.jsp").forward(request, response); } else { try { earning = Double.parseDouble(money); localTax = Double.parseDouble(local); } catch (NumberFormatException ex) { temp = "输入内容有误!"; request.setAttribute("result", temp); request.getRequestDispatcher("/error.jsp").forward(request, response); } EntITyBean ebean = new EntITyBean(); LogicBean lbean = new LogicBean(); ebean.setYourTax(lbean.calculateTax(earning, localTax)); request.setAttribute("bean", ebean); request.getRequestDispatcher("/result.jsp").forward(request, response); } } } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //Clean up resources public void destroy() { } } package com.neeke.calc.control;<br/><br/>import javax.servlet.*;<br/>import javax.servlet.http.*;<br/>import java.io.*;<br/>import java.util.*;<br/>import com.neeke.calc.bean.LogicBean;<br/>import com.neeke.calc.bean.Ent<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>yBean;<br/><br/>public class CalcServlet extends HttpServlet {<br/> private static final String CONTENT_TYPE = "text/html; charset=GBK";<br/><br/> //In<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>ialize global variables<br/> public void in<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>() throws ServletException {<br/> }<br/><br/> //Process the HTTP Get request<br/> public void doGet(HttpServletRequest request, HttpServletResponse response) throws<br/> ServletException, IOException {<br/> response.setContentType(CONTENT_TYPE);<br/> request.setCharacterEncoding("GBK");<br/> String money = request.getParameter("earning").trim();<br/> String local = request.getParameter("localTax").trim();<br/> String temp = "";<br/> double earning = 0;<br/> double localTax = 0;<br/> if (money != null && local != null) {<br/> if (money.equals("") || local.equals("")) {<br/> temp = "未输入收入或当地税额!";<br/> request.setAttribute("result", temp);<br/> request.setAttribute("result", temp);<br/> request.getRequestDispatcher("/error.jsp").forward(request,<br/> response);<br/><br/> } else {<br/> try {<br/> earning = Double.parseDouble(money);<br/> localTax = Double.parseDouble(local);<br/> } catch (NumberFormatException ex) {<br/> temp = "输入内容有误!";<br/> request.setAttribute("result", temp);<br/> request.getRequestDispatcher("/error.jsp").forward(request,<br/> response);<br/><br/> }<br/> Ent<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>yBean ebean = new Ent<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>yBean();<br/> LogicBean lbean = new LogicBean();<br/> ebean.setYourTax(lbean.calculateTax(earning, localTax));<br/> request.setAttribute("bean", ebean);<br/> request.getRequestDispatcher("/result.jsp").forward(request,<br/> response);<br/><br/> }<br/> }<br/> }<br/><br/> //Process the HTTP Post request<br/> public void doPost(HttpServletRequest request, HttpServletResponse response) throws<br/> ServletException, IOException {<br/> doGet(request, response);<br/> }<br/><br/> //Clean up resources<br/> public void destroy() {<br/> }<br/>}<br/>
修改后的代码:
package com.neeke.calc.control; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import com.neeke.calc.bean.LogicBean; import com.neeke.calc.bean.EntITyBean; public class CalcServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; //InITialize global variables public void inIT() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); request.setCharacterEncoding("GBK"); String money = request.getParameter("earning").trim(); String local = request.getParameter("localTax").trim(); String temp = ""; double earning = 0; double localTax = 0; if (money != null && local != null) { boolean y = false; if (money.equals("") || local.equals("")) { temp = "未输入收入或当地税额!"; request.setAttribute("result", temp); y = true; } else { try { earning = Double.parseDouble(money); localTax = Double.parseDouble(local); } catch (NumberFormatException ex) { temp = "输入内容有误!"; y = true; } } if (y) { request.setAttribute("result", temp); request.getRequestDispatcher("/error.jsp").forward(request, response); } else { EntITyBean ebean = new EntITyBean(); LogicBean lbean = new LogicBean(); ebean.setYourTax(lbean.calculateTax(earning, localTax)); request.setAttribute("bean", ebean); request.getRequestDispatcher("/result.jsp").forward(request, response); } } } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //Clean up resources public void destroy() { } } package com.neeke.calc.control;<br/><br/>import javax.servlet.*;<br/>import javax.servlet.http.*;<br/>import java.io.*;<br/>import java.util.*;<br/>import com.neeke.calc.bean.LogicBean;<br/>import com.neeke.calc.bean.Ent<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>yBean;<br/><br/>public class CalcServlet extends HttpServlet {<br/> private static final String CONTENT_TYPE = "text/html; charset=GBK";<br/><br/> //In<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>ialize global variables<br/> public void in<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>() throws ServletException {<br/> }<br/><br/> //Process the HTTP Get request<br/> public void doGet(HttpServletRequest request, HttpServletResponse response) throws<br/> ServletException, IOException {<br/> response.setContentType(CONTENT_TYPE);<br/> request.setCharacterEncoding("GBK");<br/> String money = request.getParameter("earning").trim();<br/> String local = request.getParameter("localTax").trim();<br/> String temp = "";<br/> double earning = 0;<br/> double localTax = 0;<br/> if (money != null && local != null) {<br/> boolean y = false;<br/> if (money.equals("") || local.equals("")) {<br/> temp = "未输入收入或当地税额!";<br/> request.setAttribute("result", temp);<br/> y = true;<br/> } else {<br/> try {<br/> earning = Double.parseDouble(money);<br/> localTax = Double.parseDouble(local);<br/> } catch (NumberFormatException ex) {<br/> temp = "输入内容有误!";<br/> y = true;<br/> }<br/> }<br/> if (y) {<br/> request.setAttribute("result", temp);<br/> request.getRequestDispatcher("/error.jsp").forward(request,<br/> response);<br/><br/> } else {<br/> Ent<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>yBean ebean = new Ent<a href=" [url]http://www.ineeke.cn[/url]" Class="keylink" title="IT" Target="IT" >IT</a>yBean();<br/> LogicBean lbean = new LogicBean();<br/> ebean.setYourTax(lbean.calculateTax(earning, localTax));<br/> request.setAttribute("bean", ebean);<br/> request.getRequestDispatcher("/result.jsp").forward(request,<br/> response);<br/><br/> }<br/><br/> }<br/><br/> }<br/><br/> //Process the HTTP Post request<br/> public void doPost(HttpServletRequest request, HttpServletResponse response) throws<br/> ServletException, IOException {<br/> doGet(request, response);<br/> }<br/><br/> //Clean up resources<br/> public void destroy() {<br/> }<br/>}<br/>
经过对比可以看出问题出在
if (money.equals("") || local.equals("")) { temp = "未输入收入或当地税额!"; request.setAttribute("result", temp); request.setAttribute("result", temp); request.getRequestDispatcher("/error.jsp").forward(request, response); } else { try { earning = Double.parseDouble(money); localTax = Double.parseDouble(local); } catch (NumberFormatException ex) { temp = "输入内容有误!"; request.setAttribute("result", temp); request.getRequestDispatcher("/error.jsp").forward(request, response); } if (money.equals("") || local.equals("")) {<br/> temp = "未输入收入或当地税额!";<br/> request.setAttribute("result", temp);<br/> request.setAttribute("result", temp);<br/> request.getRequestDispatcher("/error.jsp").forward(request,<br/> response);<br/><br/> } else {<br/> try {<br/> earning = Double.parseDouble(money);<br/> localTax = Double.parseDouble(local);<br/> } catch (NumberFormatException ex) {<br/> temp = "输入内容有误!";<br/> request.setAttribute("result", temp);<br/> request.getRequestDispatcher("/error.jsp").forward(request,<br/> response);<br/><br/> }<br/>
不过究竟为什么这样就会出错就不清楚了,以前写的好像也有过多次跳转的这个,但是都没有错的啊!郁闷了~~~不过知道问题出在哪里了,以后尽量避免这样写就是了。o(∩_∩)o...
本文来源于:尼克技术博客 [url]http://www.ineeke.cn/[/url] , 原文地址: [url]http://www.ineeke.cn/archives/Cannot-forward-after-response-has-been-committed/[/url]