Home >Web Front-end >JS Tutorial >An example of using JSP to create a super simple web calculator

An example of using JSP to create a super simple web calculator

高洛峰
高洛峰Original
2017-03-30 10:15:223685browse

To implement a simple calculator program, requirements: use jsp+javabean mode.
The project source code is as follows:
File: calculator.jsp

<%@ page language="java" pageEncoding="UTF-8"%> 
<%@ page isErrorPage="true"%> 
<%@ page errorPage="calculator.jsp" %> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
  <title>简单的计算机</title> 
 </head> 
   
 <body> 
  <%--创建Calculator对象--%> 
  <jsp:useBean id="cal" class="cn.zq.domain.Calculator" /> 
  <%--设置值  通配符*表示设置所有属性--%> 
  <jsp:setProperty property="*" name="cal"/> 
    
    
  <%-- 
    进行计算 
   --%> 
   <c:if test="${empty pageContext.exception}"> 
    <% 
      cal.calculate(); 
    %> 
   </c:if> 
    
    
  <hr/> 
  <p>计算结果:${cal.firstNum } ${cal.operator } ${cal.secondNum } = ${cal.result }</p> 
  <hr/> 
    
  <%-- 
    构建url 
   --%> 
  <c:url var="formUrl" value="/calculator.jsp"/> 
  <form action="${formUrl }" method="post"> 
    <table border="1" cellpadding="2"> 
      <tr> 
        <td colspan="2" align="center">我的计算器</td> 
      </tr> 
      <tr> 
        <td>第一个参数:</td> 
        <td><input type="text" name="firstNum"/></td> 
      </tr> 
      <tr> 
        <td>运算符:</td> 
        <td> 
          <select name="operator"> 
            <option value="+">+</option> 
            <option value="-">-</option> 
            <option value="*">*</option> 
            <option value="/">/</option> 
          </select> 
        </td> 
      </tr> 
      <tr> 
        <td>第二个参数:</td> 
        <td> 
          <input type="text" name="secondNum"> 
        </td> 
      </tr> 
      <tr> 
        <td colspan="2"> 
          <input type="submit" value="计算" /> 
        </td> 
      </tr> 
    </table> 
  </form>   
 </body> 
</html>

The javabean code used in this article is as follows:

package cn.zq.domain; 
  
public class Calculator { 
  private String firstNum; 
  private String operator; 
  private String secondNum; 
  private String result; 
  public String getFirstNum() { 
    return firstNum; 
  } 
  public void setFirstNum(String firstNum) { 
    this.firstNum = firstNum; 
  } 
  public String getOperator() { 
    return operator; 
  } 
  public void setOperator(String 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 Calculator() {} 
  public Calculator(String firstNum, String operator, String secondNum, 
      String result) { 
    this.firstNum = firstNum; 
    this.operator = operator; 
    this.secondNum = secondNum; 
    this.result = result; 
  } 
    
  public void calculate(){ 
    if(operator != null && !operator.equals("")){ 
      double first = new Double(firstNum); 
      double second = new Double(secondNum); 
      char oper = operator.charAt(0); 
      switch (oper) { 
      case &#39;+&#39;: 
        result = first + second + ""; 
        break; 
      case &#39;-&#39;: 
        result = first - second + ""; 
        break; 
      case &#39;*&#39;: 
        result = first * second + ""; 
        break; 
      case &#39;/&#39;: 
        result = first / second + ""; 
        break; 
      default: 
        throw new RuntimeException("未知运算符!"); 
      } 
    } 
  } 
}

The final completed rendering is as follows:

An example of using JSP to create a super simple web calculator

For more examples of using JSP to create a super simple web calculator to share related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn