zoukankan      html  css  js  c++  java
  • 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=" 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=" 40px;"></td>
    27        <td width="40"><input type="button" value="←" onclick="doselect(this)" style=" 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=" 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=" 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=" 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=" 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 时:

                                             

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

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

  • 相关阅读:
    pywinauto首页、文档和下载
    Android UI自动化测试最佳实践
    5个最佳的Android测试框架(带示例) – 码农网
    (转)使用graphviz绘制流程图
    使用 Graphviz 生成自动化系统图
    程序员的绘图利器 — Graphviz
    绘图工具graphviz学习使用
    Git Book 中文版
    [git] 更新到某个指定版本
    LindDotNetCore~Aspect面向方面编程
  • 原文地址:https://www.cnblogs.com/ouyangtangfeng99/p/5642294.html
Copyright © 2011-2022 走看看