搭建一个mvc框架的小实例。
简易计算器
有一个名为inputNumber.jsp的页面提供一个表单,用户可以通过表单输入两个数和运算符号提交给Servlet控制器;由名为ComputerBean.java生成的JavaBean负责存储运算数、运算符号和运算结果,由名为handleComputer的servlet控制器负责运算,将结果存储到JavaBean中,并负责请求JSP页面lookResult.jsp显示JavaBean中的数据。
inputNumber.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<p>输入运算数、选择运算符</p>
<form action="handleComputer" method="post"><input type="text" name="numberOne" /><select name="operator"><option value="+">+</option><option value="-">-</option><option value="*">*</option><option value="/">/</option></select><input type="text" name="numberTwo" /><br><input type="submit" value="计算"/>
</form>
</body>
</html>
ComputerBean
package Beans;public class ComputerBean {private double num1, num2, result;private String opt;// 省略部分setter和getter方法public double getNum1() {return num1;}public void setNum1(double num1) {this.num1 = num1;}public double getNum2() {return num2;}public void setNum2(double num2) {this.num2 = num2;}public double getResult() {return result;}public void setResult(double result) {this.result = result;}public String getOpt() {return opt;}public void setOpt(String opt) {this.opt = opt;}public double optResult(String opt, double num1, double num2) {if (opt.equals("+")) {result = num1 + num2;}if (opt.equals("-")) {result = num1 - num2;}if (opt.equals("*")) {result = num1 * num2;}if (opt.equals("/")) {result = num1 / num2;}return result;}
}
handleComputer
import Beans.ComputerBean;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;@WebServlet("/handleComputer")
public class handleComputer extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {double num1 = Double.parseDouble(request.getParameter("numberOne"));double num2 = Double.parseDouble(request.getParameter("numberTwo"));String operator = request.getParameter("operator");ComputerBean computerBean = new ComputerBean();computerBean.setNum1(num1);computerBean.setNum2(num2);computerBean.setOpt(operator);double result = computerBean.optResult(operator, num1, num2);computerBean.setResult(result);request.setAttribute("computerBean", computerBean);RequestDispatcher dispatcher = request.getRequestDispatcher("lookResult.jsp");dispatcher.forward(request, response);}
}
lookResult.jsp
<%@ page import="Beans.ComputerBean" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<%--EL表达式--%>
<p>${computerBean.num1}${computerBean.opt}${computerBean.num2}=${computerBean.result}</p>
<%--JSP--%>
<%
// ComputerBean computerBean = (ComputerBean) request.getAttribute("computerBean");
%>
<%--<%= computerBean.getNum1() %><%= computerBean.getOpt() %><%= computerBean.getNum2() %> = <%= computerBean.getResult() %>--%>
</body>
</html>
实验心得
喜欢你的你看不上,你喜欢的看不上你,他/她的优秀你看得见,你也不能一直做后面的影子了。