Servlet简单计算器 2.0

Posted 唐枫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet简单计算器 2.0相关的知识,希望对你有一定的参考价值。

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" style="width: 40px;"></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)" style="width: 40px;"></td>
27        <td width="40"><input type="button" value="←" onclick="doselect(this)" style="width: 40px;"></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)" style="width: 40px;"></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)" style="width: 40px;"></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)" style="width: 40px;"></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)" style="width: 40px;"></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 时:

                                         

关键问题在点击各个按键的时候,同时在文本域中显示想要输入的内容,当点击“←”的时候 输入的内容会自动往前退一格。

还有不知道什么原因,在计算加减乘模的时候没有问题但是当运算除法的时候总是会报数据格式错误,所以把“÷” 改成“/”之后就莫名其妙的解决了

以上是关于Servlet简单计算器 2.0的主要内容,如果未能解决你的问题,请参考以下文章

java---servlet与filter的联系与区别

Servlet简单计算器(只有除法,其它一样)

Java基础——JSP

C++实现一个简单的计算器2.0 增强稳定版

Servlet 2.0 && Servlet 3.0 新特性

golang代码片段(摘抄)