<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="/day_11/Aservlet" method="post"> 数据A:<input type="text" name="data1"/><br/> 数据B:<input type="text" name="data2"/><br/> <input type="submit" value="提交"/><hr/> </form> </body> </html>
<%@ page language="java" import="java.util.*"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% Integer c = (Integer)request.getAttribute("aaa"); %> <%=c %> </body> </html>
package cn.code.jia; 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 Aservlet */ @WebServlet("/Aservlet") public class Aservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Aservlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String s1 = request.getParameter("data1"); String s2 = request.getParameter("data2"); int a =Integer.parseInt(s1); int b =Integer.parseInt(s2); int sum=a+b; request.setAttribute("aaa", sum); request.getRequestDispatcher("/jsps/result.jsp").forward(request, response); } }