当前位置: 移动技术网 > IT编程>开发语言>Java > JSP实现计算器功能(网页版)

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

2017年12月08日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网