(javaweb)用三种版本实现简易web版计算器(JSP,JSP+javaBean,JSP+javaBean+Servlet)

(javaweb)用三种版本实现简易web版计算器(JSP,JSP+javaBean,JSP+javaBean+Servlet),第1张

(javaweb)用三种版本实现简易web版计算器(JSP,JSP+javaBean,JSP+javaBean+Servlet)

第一种:在jsp中用语句块<%%>写java代码,最后用表达式<%=%>展示出来。这种方式耦合性太高了,不利于维护。

calculator.jsp:

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




计算器


<%
	String firstNum=request.getParameter("firstNum");
	String secondNum=request.getParameter("secondNum");
	String str=request.getParameter("operator"); 
	if(firstNum==null||secondNum==null)
		{firstNum="0";secondNum="0";
		str="+";
		}
	float first=Float.parseFloat(firstNum);	
	float second=Float.parseFloat(secondNum);
	
	
	char operator=str.charAt(0);	
	String result="";

	
	switch (operator) {
	case '+':{
		result=(first+second)+"";			
		break;
	}
	case '-':{
		result=(first-second)+"";	
		break;
	}
	case '*':{
		result=(first*second)+"";	
		break;
	}			
	case '/':{
		if(second==0){
			out.println("被除数不能为0;");//throw new RuntimeException("被除数不能为0;");
		}
		else
		result=(first/second)+"";	
		break;
	}
	default:
		throw new RuntimeException("运算符有误!");
	}
%>
	
*********************************
计算结果是: <%=first %><%=operator %><%=second %> = <%=result %>
*********************************

第二种方式:jsp+javajean    这种方式会进一步降低耦合性。

实体类:CalBeans:

package com.ag;


public class CalBeans {
	private String firstNum = "0";
	private char operator = '+';
	private String secondNum = "0";
	private String result = "0";

	public CalBeans() {
		super();
	}

	public CalBeans(String firstNum, char operator, String secondNum) {
		super();
		this.firstNum = firstNum;
		this.operator = operator;
		this.secondNum = secondNum;
		
	}

	public String getFirstNum() {
		return firstNum;
	}

	public void setFirstNum(String firstNum) {
		this.firstNum = firstNum;
	}

	public char getOperator() {
		return operator;
	}

	public void setOperator(char operator) {
		this.operator = operator;
	}

	public String getSecondNum() {
		return secondNum;
	}

	public void setSecondNum(String secondNum) {
		this.secondNum = secondNum;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	//
	public void calculate() {
		float first = Float.parseFloat(this.firstNum);
		float second = Float.parseFloat(this.secondNum);
		switch (this.operator) {
		case '+':
			this.result = (first + second) + "";
			break;
		case '-':
			this.result = (first - second) + "";
			break;
		case '*':
			this.result = (first * second) + "";
			break;
		case '/':
			if (second == 0) {
				throw new RuntimeException("被除数不能为0!");
			} else {
				this.result = (first / second) + "";
				break;
			}
			default:
				throw new RuntimeException("运算符有误!");
		}
	}
}

Cal.jsp:

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




Insert title here





<%
	try{
	calBean.calculate();
	}catch(Exception e)	{
		out.write(e.getMessage());
	}
%>

*********************************
计算结果是: =
*********************************

第三种:jsp+javaBean+Servlet  

即MVC模式,MVC模式(Model-view-controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)。这种方式jsp只是用于前端的页面的展示,后端实现计算的功能,很好地分离了前后端。

CalBeans和第二种是相同的:
代码如下:

package com.ag;


public class CalBeans {
	private String firstNum = "0";
	private char operator = '+';
	private String secondNum = "0";
	private String result = "0";

	public CalBeans() {
		super();
	}

	public CalBeans(String firstNum, char operator, String secondNum) {
		super();
		this.firstNum = firstNum;
		this.operator = operator;
		this.secondNum = secondNum;
		
	}

	public String getFirstNum() {
		return firstNum;
	}

	public void setFirstNum(String firstNum) {
		this.firstNum = firstNum;
	}

	public char getOperator() {
		return operator;
	}

	public void setOperator(char operator) {
		this.operator = operator;
	}

	public String getSecondNum() {
		return secondNum;
	}

	public void setSecondNum(String secondNum) {
		this.secondNum = secondNum;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	//
	public void calculate() {
		float first = Float.parseFloat(this.firstNum);
		float second = Float.parseFloat(this.secondNum);
		switch (this.operator) {
		case '+':
			this.result = (first + second) + "";
			break;
		case '-':
			this.result = (first - second) + "";
			break;
		case '*':
			this.result = (first * second) + "";
			break;
		case '/':
			if (second == 0) {
				throw new RuntimeException("被除数不能为0!");
			} else {
				this.result = (first / second) + "";
				break;
			}
			default:
				throw new RuntimeException("运算符有误!");
		}
	}
}

前端展示:cal1.jsp:

用el表达式展示:

代码如下:

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



    
    Insert title here



*********************************
计算结果是: ${calBeans_firstNum} ${calBeans_b} ${calBeans_secondNum}${calBeans_a} ${calBean_result}
*********************************

calServlet的编写:

package web;

import com.ag.CalBeans;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/calServlet")
public class calServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //接受数据
        String firstNum = request.getParameter("firstNum");
        String operator = request.getParameter("operator");
        String secondNum = request.getParameter("secondNum");
        char b=operator.charAt(0);
        //封装对象
        CalBeans calBeans = new CalBeans();
        calBeans.setFirstNum(firstNum);
        calBeans.setOperator(b);
        calBeans.setSecondNum(secondNum);
        //调用方法
         calBeans.calculate();
        String result = calBeans.getResult();
       
        request.setCharacterEncoding("utf-8");
        //把数据存到request域中
          request.setAttribute("calBeans_firstNum",firstNum);
          request.setAttribute("calBeans_b",b);
          request.setAttribute("calBeans_secondNum",secondNum);
          request.setAttribute("calBeans_a","=");
          request.setAttribute("calBean_result",result);
        //转发
        request.getRequestDispatcher("/cal1.jsp").forward(request,response);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

最后附上结果图:

需要源码的朋友,可以点击这个https://gitee.com/zhu-yiwen-667/Calculator,是一个maven项目,导入之后,可能会下载一些插件和jar包,里面已经配好了Tomcat7的插件,(现在tomcat插件最高只有7的版本),可以直接配置使用,我配置的80端口,如果需要修改可以在pom.xml中修改,如果觉得慢,提前在pom.xml中加入如下代码:


        
            nexus-aliyun
            nexus-aliyun
            http://maven.aliyun.com/nexus/content/groups/public/
            
                true
            
            
                false
            
        
    

配置图:idea上面偏右一点

最后,如有问题或者不懂的地方,希望大家评论指出和提问。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5684839.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存