点击“求累加和”按钮,没有任何反应
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>求100以内累加和</title> </head> <body> <form action="http://localhost:8080/test02/s01"> <input type="text" placeholder="请输入100以内的数字"/> <button type="submit" form="test">求累加和</button> </form> </body> </html>
package sum; 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 Sum01 */ @WebServlet("/s01") public class Sum01 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Sum01() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String nn = request.getParameter("num"); int n=Integer.parseInt(nn); int sum=0; for(int i=0;i<=n;i++) { sum+=i; } request.setAttribute("sum", sum); request.getRequestDispatcher("/s02").forward(request,response); } }
package sum; 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 Sum02 */ @WebServlet("/s02") public class Sum02 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Sum02() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int sum = (int)request.getAttribute("sum"); response.getWriter().println(sum); } }
登录显示出来了
可是输入数字,比如20,点击按钮,没有任何反应
0
收起
正在回答
3回答
同学你好 ,在按钮处,应该使用input,如:<input type="submit" value="求累加和">,button标签不能提交数据,所以点击按钮没有反应。<input type="submit" >可以提交数据,建议同学修改一下。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
qq_凯哥哥_0
2019-02-27 01:14:22
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>求100以内累加和</title> </head> <body> <form action="http://localhost:8080/test02/s01"> <input name="num"type="text" placeholder="请输入100以内的数字"/> <button type="submit" form="test">求累加和</button> </form> </body> </html>
同学你好,根据你说的问题结合你的代码,是因为你在html中没有为input设置name属性导致你后面得String nn = request.getParameter("num");中num不知道是什么,没有反应就是没有获取到num,所以你添加一个name在<input name="num"type="text" placeholder="请输入100以内的数字"/>
。。我也是个学生代码我没有测试,问题是我肉眼看得,希望可以帮到你。
相似问题
登录后可查看更多问答,登录/注册
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星