Home > Web Front-end > JS Tutorial > How to implement a simple calculator based on JSP

How to implement a simple calculator based on JSP

高洛峰
Release: 2017-01-20 17:26:01
Original
2720 people have browsed it

The example in this article describes the method of implementing a simple calculator based on JSP. Share it with everyone for your reference. The specific implementation method is as follows:

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
<html>  
  <head>  
  <!-- 用户提交验证 -->  
  <script type="text/javascript" language="javascript">  
  <!--   
  function checkNum(){  
    if(form1.num1.value==""){  
    window.alert("num1 值不能为空 !!哈哈");  
    return false;  
    }  
    //判断num1是不是一个数  
    if(Math.round(form1.num1.value)!=(form1.num1.value)){  
    window.alert("num1不是一个整数")  
    return false;  
    }  
    if(form1.num2.value==""){  
    window.alert("num2 值不能为空 !!哈哈");  
    return false;  
    }  
    //判断num2是不是一个数  
    if(Math.round(form1.num2.value)!=(form1.num2.value)){  
    window.alert("num2不是一个整数")  
    return false;  
      
  }  
  }  
  -->  
  </script>  
  </head>  
  <h1>我的计算器</h1>  
  <hr>  
  <body>  
    <form  name ="form1" action="result.jsp" method ="post">  
    <input type="text" name ="num1" ></input><br>  
     
    <select name="flag">  
    <option value=+>+</option>  
    <option value=->-</option>  
    <option value=*>*</option>   
    <option value=/>/</option>  
    </select><br>  
     <input type="text" name="num2"/></input><br>  
    <input type="submit" value="提交" onclick="return checkNum();"></input>  
    </form>  
    <hr>  
  </body>  
</html>
Copy after login

result.jsp is used to display results

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
  </head>  
    
  <body>  
  <%  
    String num1=request.getParameter("num1");  
    String num2 = request.getParameter("num2");  
   String flag = request.getParameter("flag");  
   int s_num1=Integer.parseInt(num1);  
   int s_num2=Integer.parseInt(num2);  
   int result=0;  
   
  if(flag.equals("+")){  
  //加  
  result=s_num1+s_num2;  
  }else if(flag.equals("-")){  
  //减  
  result=s_num1-s_num2;  
  }else if(flag.equals("/")){  
  result=s_num1/s_num2;  
  //除  
  }else{  
  //乘  
  result=s_num1*s_num2;  
  }  
  out.println("结果是:"+result);  
   %>  
  </body>  
</html>
Copy after login

I hope this article will be helpful to everyone’s jsp programming.

For more articles on how to implement a simple calculator based on JSP, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template