简单的JAVA MVC框架模式--Java-servlet-JavaBean

MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码

此框架模式是一个简单的解决个人所得税计算的业务逻辑

servlet

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class TaxServlet
 */
public class TaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String income = request.getParameter("income");

		int n = Integer.parseInt(income);

		// 控制器调用业务模型

		// 模型 JavaBean
		Tax tax = new Tax();
		int result = tax.getResult(n);

		// 结果存入作用域
		HttpSession session = request.getSession();
		session.setAttribute("ic", result);

		// 跳转到 show.jsp
		response.sendRedirect("show.jsp");

	}

}

JavaBean

实现对数据的处理:个人所得税的计算


public class Tax {

	// 怎么计算个人所得税

	public int getResult(int n) {
		// 计算个人所得税
		int x = n - 3500;
		if (x <= 0) {
			return 0;
		}
		if (x <= 1500) {
			return (int) (x * 0.03f);
		} else if (x <= 4500) {
			return (int) (x * 0.1f - 105f);
		} else if (x <= 9000) {
			return (int) (x * 0.2f - 555f);
		} else if (x <= 35000) {
			return (int) (x * 0.25f - 1005f);
		} else if (x <= 55000) {
			return (int) (x * 0.3f - 2755f);
		} else if (x <= 80000) {
			return (int) (x * 0.35f - 5505f);
		} else
			return (int) (x * 0.45f - 13505f);
	}

}
index.jsp

<%@ page language="java" 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>个人所得税查询</title>
</head>
<body>

	<h1>计算个人所得税</h1>
	<form action="tax" method="post">
		<input type="text" name="income"><input type="submit" value="提交">
	</form>

</body>
</html>

show.jsp

<%@ page language="java" 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>
<span style="white-space:pre">	</span><!-- 内置对象session接收数据   -->
<span style="white-space:pre">	</span><%
<span style="white-space:pre">	</span>int n =( (Integer)session.getAttribute("ic")).intValue();
<span style="white-space:pre">	</span>%>
<span style="white-space:pre">	</span><h1><%=n %></h1>
</body>
</html>



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。