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


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

<%@ 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如下:

<%@ 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编程有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3