JSP实现计算器功能(网页版)

内容摘要
jsp实现网页计算器代码如下:只有两个jsp页面

myCal.jsp如下:




<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getC
文章正文

jsp实现网页计算器代码如下:只有两个jsp页面
myCal.jsp如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
<base href="<%=basePath%>">
  
<title>My JSP 'myCal.jsp' starting page</title>
  
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
  
  
<!-- jsp页面中不可以直接使用script -->
<script language="javascript">
<!--
    //写一个函数判断是否两个数都有
    function checkNum()
    {
      //判断num1 num2是否为空
      if((form1.num1.value == "") || (form1.num2.value == ""))
      {
        window.alert("null,不能为空!");
        return false;
      }
      //判断是否是数字
      if(Math.round(form1.num1.value) != form1.num1.value  && Math.round(form1.num2.value) != form1.num2.value)
      {
        window.alert("num1和num2不是一个数");
        return false;
      }
      if(Math.round(form1.num1.value) != form1.num1.value)
      {
        window.alert("num1不是一个数");
        return false;
      }
      if(Math.round(form1.num2.value) != form1.num2.value)
      {
        window.alert("num2不是一个数");
        return false;
      }
        
    }
    
  -->
  </script>
</head>
<body>
  
  <form name="form1" action="calculator/myResult.jsp" method="post">
    请输入第一个数:<input type="text" name="num1"
    <select name="flag">
      <option value=+>+</option>
      <option value=->-</option>
      <option value=*>*</option>
      <option value=/>/</option>
    </select> 
    请输入第二个数:<input type="text" name="num2"
    <input type="submit" value="计算" onclick="return checkNum();">
  </form>
  
</body>
</html>

myResult.jsp如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
  <base href="<%=basePath%>">
    
  <title>My JSP 'myResult.jsp' starting page</title>
    
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">  
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
  
 </head>
   
 <body>
 <%
  //第1步接收到 第1个数
  String s_num1 = request.getParameter("num1");
  //第2步接收到 第2个数
  String s_num2 = request.getParameter("num2");
  //第3步接收到 运算符
  String flag = request.getParameter("flag");
  //第4步 计算
    
  int num1 = Integer.parseInt(s_num1);
  int num2 = Integer.parseInt(s_num2);
  int result = 0;
  if(flag.equals("+"))
  {
    result = num1+num2;
  }
  else if(flag.equals("-"))
  {
    result = num1-num2;
  }
  else if(flag.equals("*"))
  {
    result = num1*num2;
  }
  else if(flag.equals("/"))
  {
    result = num1/num2;
  }
  //第5步
  out.println("结果是:"+result);
  %> 
    
 </body>
</html>

虽然过程很简单但是有几个值得学习的地方:
如何判断输入的数据是不是数字:使用Math.round(form1.num1.value) != form1.num1.value   来判断;
如何获取操作值:设置name属性 flag实现。

希望本文所述对大家学习JSP编程有所帮助。


代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!