使用基于Servlet的MVC模式设计四则运算

1、创建用户输入JSP页面inputNumber.jsp,页面效果如下图所示,该页面的主要功能是获取和显示用户的的输入,提交的方式为post,提交的url为“calculate”。

使用基于Servlet的MVC模式设计四则运算_第1张图片

 

inputNumber.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>









Insert title here





请输入两个数


2、 在该项目的src目录下创建一个servlets的包,包中的类用于处理前端不同类型的请求,并对相应的请求作出响应。在该包内创建一个HandleComputerServlet.java,该类用于接收用的请求以及数据,然后调用ComputerBean类中的cacuResult()函数计算结果,最后将计算结果使用session对象响应给前端。

HandleComputerServlet.java类

package servlets;



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;

import javax.servlet.http.HttpSession;



import com.beans.ComputerBean;



/**

 * Servlet implementation class HandleComputerServlet

 */

@WebServlet({ "/HandleComputerServlet", "/calculate" })

public class HandleComputerServlet extends HttpServlet {

public static final long serialVersionUID = 1L;



    /**

     * Default constructor. 

     */

    public HandleComputerServlet() {

        // TODO Auto-generated constructor stub

    }



/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//response.getWriter().append("Served at: ").append(request.getContextPath());

double op1 = Double.parseDouble(request.getParameter("op1"));

double op2 = Double.parseDouble(request.getParameter("op2"));

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


HttpSession session = request.getSession();

ComputerBean computerBean = new ComputerBean();


computerBean.setOp1(op1);

computerBean.setOp2(op2);

computerBean.setStr(str);

session.setAttribute("computerBean", computerBean);

request.getRequestDispatcher("showResult.jsp").forward(request, response);

}



/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}



}

3、 在该项目的src目录下创建一个com.beans包,用于存放封装的model类。然后在bean目录下创建一个ComputerBean类来定义数据模型以及相应的操作,该类设计了四个类成员变量,分别为操作数一、操作数二、操作符以及输出结果。除此之外,该类还实现了计算结果的方法cacuResult()。

ComputerBean.java

package com.beans;



public class ComputerBean {

private double op1;

private double op2;

private String str;

private double result;

//double cacuResult;

public double getResult() {

return result;

}

public void setResult(double result) {

this.result = result;

}

public double getOp1() {

return op1;

}

public void setOp1(double op1) {

this.op1 = op1;

}

public double getOp2() {

return op2;

}

public void setOp2(double op2) {

this.op2 = op2;

}

public String getStr() {

return str;

}

public void setStr(String str) {

this.str = str;

}

public void cacuResult() {

switch(str) {

case "+": this.result=this.op1+this.op2;break;

case "-": this.result=this.op1-this.op2;break;

case "*": this.result=this.op1*this.op2;break;

case "/": this.result=this.op1/this.op2;break;

default:break;

}


}

}

4、创建结果输出页面showResult.jsp,页面效果如下所示。该页面的主要功能就是显示结果。

showResult.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>









Insert title here







  

  

  = 

   





你可能感兴趣的:(Java,web,java,前端,javascript,mvc,servlet)