请问为什么用响应重定向是500错误?
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="http://localhost:8080/WebDemo1/sum"> <input type="text" name="number" placeholder="请输入100以内的数字"> <input type="submit" name="submit" value="求累加和"> </form> </body> </html>
package com.imooc.direct; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/sum") public class sum extends HttpServlet { private static final long serialVersionUID = 1L; public sum() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String num = request.getParameter("number"); int number = Integer.parseInt(num); int sum=0; for(int i=0;i<=number;i++) { sum=sum+i; } request.setAttribute("sum", sum); // request.getRequestDispatcher("/show").forward(request,response); response.sendRedirect("/WebDemo1/show"); } }
package com.imooc.direct; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/show") public class show extends HttpServlet { private static final long serialVersionUID = 1L; public show() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int result = (int)request.getAttribute("sum"); response.getWriter().println(result); } }
工程目录如下图:
1
收起
正在回答 回答被采纳积分+1
5回答
qq_我叫马俊杰_0
2019-01-12 17:20:28
你的html页面上的method
有点麋鹿
2019-01-06 11:46:46
重定向到show里面;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int result=(int)request.getAttribute("sum");
response.getWriter().println(result);
}
这里的sum是个字符串,无法强制转化成为一个int类型的数字,报错:500(服务器错误);可以把sum改成一个int数值..
跟着小鸟去抓琴梨4290628
2018-12-18 01:20:58
第一个servlet27行
response.sendRedirect("/WebDemo1/show");
去掉/WebDemo1
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星