老师这样写可以吗,还有如何使用响应重定向写这个作业呢
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/Servlet_JSP_2/direct/SC"> <input name="num" value="请输入100以内的数字" style="box-shadow: 1px 1px 1px #000 inset; color:gray" border=1px/> <input type="submit" value="求累加和"/> </form> </body> </html> 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 Servlet_Request1 */ @WebServlet("/direct/SC") public class Servlet_Count extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet_Count() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int num = Integer.parseInt(request.getParameter("num")); int sum=0; for(int i = 1;i <= num;i++) { sum+=i; } request.setAttribute("sum", sum); request.getRequestDispatcher("/direct/SS").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 Servlet_Request2 */ @WebServlet("/direct/SS") public class Servlet_Show extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet_Show() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println((int)request.getAttribute("sum")); } }
1
收起
正在回答 回答被采纳积分+1
2回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星