无法实现预期,求助
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="myjsp/sum" method="get"> <input type="text" name="number" placeholder="请输入100以内的数字"> <input type="submit" value="求累加和"> </form> </body> </html>
package com.imooc.servlet; 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 { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String number = request.getParameter("number"); int n = Integer.parseInt(number); int sumall=0; for (int i = 0; i <= n; i++) { sumall=i+sumall; } request.setAttribute("sum", sumall); request.getRequestDispatcher("/display").forward(request, response); } }
package com.imooc.servlet; 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; /** * Servlet implementation class display */ @WebServlet("/display") public class display extends HttpServlet { public display() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub int s = (int)request.getAttribute("sum"); response.setContentType("text/html;charset=utf-8"); response.getWriter().println("结果是:"+s); } }
16
收起
正在回答 回答被采纳积分+1
2回答
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星