jsp 輸入界面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Servlet計算器</title> 8 </head> 9 <body bgcolor="#afb4db"> 10 <% 11 String s1 = ""; 12 Object ob = session.getAttribute("jg"); 13 if(ob != null){ 14 s1 = ob.toString(); 15 } 16 session.invalidate(); 17 %> 18 <form action="TestJSQ" method="post"> 19 <table border="1" align="center"> 20 <tr align="center"><td colspan="4">簡易計算器</td></tr> 21 <tr> 22 <td colspan="4"><textarea name="text" id="tt" cols="22" rows="1"><%= s1 %></textarea></td></tr> 23 <tr align="center"> 24 <td width="40" align="center"><input type="reset" value="C" ></td> 25 <td width="40"><input type="button" value="off" onclick="doselect(this)"></td> 26 <td width="40"><input type="button" value="+" onclick="doselect(this)" ></td> 27 <td width="40"><input type="button" value="←" onclick="doselect(this)" ></td></tr> 28 <tr align="center"> 29 <td><input type="button" value="7" onclick="doselect(this)"></td> 30 <td><input type="button" value="8" onclick="doselect(this)"></td> 31 <td><input type="button" value="9" onclick="doselect(this)"></td> 32 <td><input type="button" value="-" onclick="doselect(this)" ></td></tr> 33 <tr align="center"> 34 <td><input type="button" value="4" onclick="doselect(this)"></td> 35 <td><input type="button" value="5" onclick="doselect(this)"></td> 36 <td><input type="button" value="6" onclick="doselect(this)"></td> 37 <td><input type="button" value="x" onclick="doselect(this)" ></td></tr> 38 <tr align="center"> 39 <td><input type="button" value="1" onclick="doselect(this)"></td> 40 <td><input type="button" value="2" onclick="doselect(this)"></td> 41 <td><input type="button" value="3" onclick="doselect(this)"></td> 42 <td><input type="button" value="/" onclick="doselect(this)" ></td></tr> 43 <tr align="center"> 44 <td><input type="button" value="." onclick="doselect(this)"></td> 45 <td><input type="button" value="0" onclick="doselect(this)"></td> 46 <td><input type="submit" value="=" onclick="doselect(this)"></td> 47 <td><input type="button" value="%" onclick="doselect(this)" ></td></tr> 49 </table> 50 </form> 54 </body> 55 </html> 56 <script language="javascript"> 57 58 function doselect(bt){ 59 var obj = document.getElementById('tt'); 60 var s = bt.value; 61 obj.innerHTML += s; 62 if(s == "←"){ 63 var a = obj.value; 64 s = a.substring(0, a.length-2); 65 obj.innerHTML = s; 66 } 67 if(s == "off"){ 68 window.close(); 69 } 70 } 71 </script>
Servlet後台處理:
1 package com.sp.web; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 10 public class Test_JSQ extends HttpServlet { 11 private static final long serialVersionUID = 1L; 12 13 public Test_JSQ() { 14 super(); 15 16 } 17 18 protected void doGet(HttpServletRequest request, HttpServletResponse response) 19 throws ServletException, IOException { 20 21 response.setCharacterEncoding("UTF-8"); 22 response.setContentType("text/html"); 23 String st = request.getParameter("text").trim(); 24 25 int a = 0; 26 if (st.contains("+")) { 27 a = st.indexOf("+"); 28 } else if (st.contains("-")) { 29 a = st.indexOf("-"); 30 } else if (st.contains("x")) { 31 a = st.indexOf("x"); 32 } else if (st.contains("/")) { 33 a = st.indexOf("/"); 34 } else if (st.contains("%")) { 35 a = st.indexOf("%"); 36 } 37 Double n1 = Double.parseDouble(st.substring(0, a)); 38 Double n2 = Double.parseDouble(st.substring(a + 1, st.length() - 1)); 39 String s = st.charAt(a) + ""; 40 41 String rs = ""; 42 if (s.equals("+")) { 43 rs = n1 + n2 + ""; 44 45 } else if (s.equals("-")) { 46 rs = n1 - n2 + ""; 47 48 } else if (s.equals("x")) { 49 rs = n1 * n2 + ""; 50 51 } else if (s.equals("/")) { 52 if (n2 != 0.0) { 53 rs = n1 / n2 +""; 54 55 } else { 56 rs = "error by 0"; 57 } 58 } else if (s.equals("%")) { 59 rs = n1 % n2 + ""; 60 61 } 62 request.getSession().setAttribute("jg", rs); 63 response.sendRedirect("jisuanqi.jsp"); 64 ; 65 } 66 67 protected void doPost(HttpServletRequest request, HttpServletResponse response) 68 throws ServletException, IOException { 69 70 doGet(request, response); 71 } 72 73 }
界面展示: 當除數=0 時:
關鍵問題在點擊各個按鍵的時候,同時在文本域中顯示想要輸入的內容,當點擊“←”的時候 輸入的內容會自動往前退一格。
還有不知道什麼原因,在計算加減乘模的時候沒有問題但是當運算除法的時候總是會報數據格式錯誤,所以把“÷” 改成“/”之後就莫名其妙的解決了