最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JSP實現(xiàn)計算器功能(網(wǎng)頁版)

 更新時間:2021年11月02日 09:40:13   作者:徐劉根  
這篇文章講述了JSP實現(xiàn)計算器功能的詳細代碼,網(wǎng)頁版的計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

jsp實現(xiàn)網(wǎng)頁計算器代碼如下:只有兩個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"> 
<!-- 
  //寫一個函數(shù)判斷是否兩個數(shù)都有 
  function checkNum() 
  { 
   //判斷num1 num2是否為空 
   if((form1.num1.value == "") || (form1.num2.value == "")) 
   { 
    window.alert("null,不能為空!"); 
    return false; 
   } 
   //判斷是否是數(shù)字 
   if(Math.round(form1.num1.value) != form1.num1.value && Math.round(form1.num2.value) != form1.num2.value) 
   { 
    window.alert("num1和num2不是一個數(shù)"); 
    return false; 
   } 
   if(Math.round(form1.num1.value) != form1.num1.value) 
   { 
    window.alert("num1不是一個數(shù)"); 
    return false; 
   } 
   if(Math.round(form1.num2.value) != form1.num2.value) 
   { 
    window.alert("num2不是一個數(shù)"); 
    return false; 
   } 
    
  } 
  
 --> 
 </script> 
</head> 
<body> 
 
 <form name="form1" action="calculator/myResult.jsp" method="post"> 
  請輸入第一個數(shù):<input type="text" name="num1"> 
  <select name="flag"> 
   <option value=+>+</option> 
   <option value=->-</option> 
   <option value=*>*</option> 
   <option value=/>/</option> 
  </select> 
  請輸入第二個數(shù):<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個數(shù) 
 String s_num1 = request.getParameter("num1"); 
 //第2步接收到 第2個數(shù) 
 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("結(jié)果是:"+result); 
 %> 
  
 </body> 
</html>

雖然過程很簡單但是有幾個值得學習的地方:
如何判斷輸入的數(shù)據(jù)是不是數(shù)字:使用Math.round(form1.num1.value) != form1.num1.value   來判斷;
如何獲取操作值:設(shè)置name屬性 flag實現(xiàn)。

希望本文所述對大家學習JSP編程有所幫助。

相關(guān)文章

  • JSP是什么?JSP是什么意思?

    JSP是什么?JSP是什么意思?

    JSP(Java Server Pages)是由Sun Microsystems公司倡導、許多公司參與一起建立的一種動態(tài)網(wǎng)頁技術(shù)標準,與asp、php、asp.net類似的后臺語言
    2013-05-05
  • JSP開發(fā)Servlet重寫init()方法實例詳解

    JSP開發(fā)Servlet重寫init()方法實例詳解

    這篇文章主要介紹了JSP開發(fā)Servlet重寫init()方法實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 純jsp實現(xiàn)的倒計時動態(tài)顯示效果完整代碼

    純jsp實現(xiàn)的倒計時動態(tài)顯示效果完整代碼

    這篇文章主要介紹了純jsp實現(xiàn)的倒計時動態(tài)顯示效果代碼,涉及JSP時間操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • 最新評論

    松江区| 徐水县| 绍兴市| 平陆县| 清丰县| 湄潭县| 平安县| 抚顺市| 大洼县| 晋中市| 清徐县| 五大连池市| 黄冈市| 台北市| 河津市| 河间市| 哈密市| 渭南市| 平顶山市| 邛崃市| 华亭县| 古浪县| 大姚县| 鄂伦春自治旗| 平凉市| 泊头市| 河曲县| 红河县| 武胜县| 秦皇岛市| 理塘县| 怀远县| 南部县| 晋中市| 洛浦县| 高州市| 舒兰市| 新平| 扎赉特旗| 正宁县| 滨海县|