老师帮忙看一下错哪了???
package com.addition; 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 CheckLoginServlet2 */ @WebServlet("/cs") public class CheckLoginServlet extends HttpServlet { /** * @see HttpServlet#HttpServlet() */ public CheckLoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String num1 = request.getParameter("num1"); int num2=0, sum=0; for(int i=0;i<num2;i++) { sum+=i; } request.setAttribute("sum", "sum");//属性名与值(object对象) request.getRequestDispatcher("/add").forward(request, response); } }
package com.addition; 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; /** * Servlet implementation class indexServlet2 */ @WebServlet("/add") public class indexServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public indexServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int sum=(Integer)request.getAttribute("sum"); String str="累加和为:"+sum; PrintWriter out = response.getWriter(); out.println(str); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>加法计算器</title> </head> <body> <form action="/Servlet_Addition/cs"> <input name="num1" placeholder="请输入100以内的数字"/> <input type="submit" value="求累加和"><br/> </form> </body> </html>
9
收起
正在回答 回答被采纳积分+1
3回答
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星