JSP實現網頁計算機,jsp實現計算機
(1)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>
(2)雖然過程很簡單但是有幾個值得學習的地方:
如何判斷輸入的資料是不是數字:使用Math.round(form1.num1.value) != form1.num1.value 來判斷;
如何擷取操作值:設定name屬性 flag實現;
編寫一JSP頁面實現簡單計算機的功可以(只可以用一個JSP頁面來實現)
比較簡單的東西。jsp加js就搞定了。只要會加減乘除和js就能寫出來。LZ自己多多敲敲吧
實現一個jsp頁面的計數器
要做計數器,必須要用資料庫或是文本做儲存才行。