package per.mjn._08_cal; 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("/cal") public class CalServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); // ----------------------------------- // 2. 接收表单中的数据 String sNum1 = req.getParameter("num1"); String op = req.getParameter("op"); String sNum2 = req.getParameter("num2"); String result = ""; if(hasLength(sNum1) && hasLength(sNum2)) { Integer num1 = Integer.valueOf(sNum1); Integer num2 = Integer.valueOf(sNum2); if("+".equals(op)) { result = num1 + num2 + ""; } else if("-".equals(op)) { result = num1 - num2 + ""; } else if("*".equals(op)) { result = num1 * num2 + ""; } else if("/".equals(op)) { result = num1 / num2 + ""; } } // System.out.println(sNum1); // System.out.println(op); // System.out.println(sNum2); // 1. 输出一个计算器的界面 out.print("<form action='/day3/cal' method='post'>"); out.print("<input type='number' name='num1' value='" + sNum1 + "'/>"); out.print("<select name='op'>"); out.print("<option>+</option>"); out.print("<option>-</option>"); out.print("<option>*</option>"); out.print("<option>/</option>"); out.print("</select>"); out.print("<input type='number' name='num2' value='" + sNum2 + "'/>"); out.print("<input type='submit' value=' = '/>"); out.print("<input type='text' value='" + result + "' disabled/>"); } private boolean hasLength(String str) { return str != null && !"".equals(str.trim()); } }
<!DOCTYPE html> <html> <head> <meta charset='UTF-8'> <title>Insert title here</title> </head> <body> <form action='' method='post'> <input type='number' name='num1'/> <select name='op'> <option>+</option> <option>-</option> <option>*</option> <option>/</option> </select> <input type='number' name='num2'/> <input type='submit' value=' = '/> <input type='text' value='' disabled/> </form> </body> </html>