使用基于Servlet的MVC模式设计一个Web应用

使用基于Servlet的MVC模式设计一个Web应用

要求:用户通过JSP页面inputNumber.jsp输入2个操作数,并选择一种运算符,单击“提交”按钮后,调用HandleComputer.java这个Servlet。在HandleComputer中首先获取用户输入的数字和运算符,并将这些内容存入到实体模型(由Computer.java创建)中,然后调用业务模型(由CalculateBean.java创建)进行计算,并把结果存入到实体模型中,在showResult.jsp中调用JavaBean显示计算的结果。如果输入格式有误却提交则重置页面。

创建项目与文件:首先新建一个Dynamic Web Project
各文件创建如下图:
使用基于Servlet的MVC模式设计一个Web应用_第1张图片
代码部分如下:
Computer.java:

package dto;

public class Computer {
     
	private String number1;
	private String number2;
	private String sign;
	private double result;
	public String getNumber1(){
     
		return number1;
		}
	public void setNumber1(String number1){
     
		this.number1=number1;
	}
	public String getNumber2(){
     
		return number2;
		}
	public void setNumber2(String number2){
     
		this.number2=number2;
	}
	public String getSign(){
     
		return sign;
		}
	public void setSign(String sign){
     
		this.sign=sign;
	}
	public double getResult(){
     
		return result;
		}
	public void setResult(double result){
     
		this.result=result;
	}
}

CalculateBean.java:

package service;

import dto.Computer;
public class CalculateBean{
     
	public boolean test(Computer user) {
     
		if(user!=null&&user.getSign().equals("+")||user.getSign().equals("-")||user.getSign().equals("*")||user.getSign().equals("/")&&user.getNumber1()!=null&&user.getNumber2()!=null&&user.getSign()!=null) {
     
			return true;
		}
		else {
     
			return false;
		}
	}
	public void validate(Computer user){
     
		if(user.getNumber1()!=null&&user.getNumber2()!=null) {
     
			if(user.getSign().equals("+")) {
     
				user.setResult(Double.parseDouble(user.getNumber1())+Double.parseDouble(user.getNumber2()));
				}
			else if(user.getSign().equals("-")) {
     
				user.setResult(Double.parseDouble(user.getNumber1())-Double.parseDouble(user.getNumber2()));
				}
			else if(user.getSign().equals("*")) {
     
				user.setResult(Double.parseDouble(user.getNumber1())*Double.parseDouble(user.getNumber2()));
				}
			else {
     
				user.setResult(Double.parseDouble(user.getNumber1())/Double.parseDouble(user.getNumber2()));
				}
			}
		}
	}

HandleComputer.java:

注意1:这里uc.validate(user)必须在确定getNumber1()与getNumber2()均不为空时调用,否则若不输入数字一或二提交均会抛出空指针的异常
2.这里Double.parseDouble(user.getNumber1())是将字符串转化为Double类型数的语句

package servlet;

import java.io.IOException;
import service.CalculateBean;

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 dto.Computer ;
@WebServlet(name="HandleComputer",urlPatterns={
     "/HandleComputer"})
public class HandleComputer extends HttpServlet{
     
	private static final long serialVersionUID = -5323681476108301191L;
	protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     
		doPost(request,response);
	}

	protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
     
		request.setCharacterEncoding("GBK");
		String number1=request.getParameter("number1");
		String number2=request.getParameter("number2");
		String sign=request.getParameter("sign");
		Computer user=new Computer();
		user.setNumber1(number1);
		user.setNumber2(number2);
		user.setSign(sign);
		CalculateBean uc=new CalculateBean();
		
		if (uc.test(user)){
     
			uc.validate(user);
			request.setAttribute("user",user);
			RequestDispatcher dis=request.getRequestDispatcher("showResult.jsp");
			dis.forward(request,response);
} 
		else {
     
			response.sendRedirect("inputNumber.jsp");
			} 
		}
}

inputNumber.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>inputNumber</title>
</head>
<body>
<form action="HandleComputer" method="post">
<table>
<tr>
<td>数字一:</td>
<td><input type="text" name="number1"/></td>
</tr>
<tr>
<td>数字二:</td>
<td><input type="text" name="number2"/></td>
</tr>
<tr>
<tr>
<td>运算符:</td>
<td><input type="text" name="sign"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>

showResult.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>showResult</title>
</head>
<body>
<jsp:useBean id="user" type="dto.Computer" scope="request"/>
<jsp:getProperty property="number1" name="user"/>
<jsp:getProperty property="sign" name="user"/>
<jsp:getProperty property="number2" name="user"/>
=<jsp:getProperty property="result" name="user"/>
</body>
</html>

你可能感兴趣的:(jsp,java)