Servlet simple calculator 2.0, servlet calculator 2.0

Source: Internet
Author: User

Servlet simple calculator 2.0, servlet calculator 2.0

Jsp input interface:

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 

Servlet background processing:

 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 }

Interface display: When the divisor is 0:

The key issue is that when you click each button, the content you want to enter is displayed in the text field. When you click "Submit", the entered content is automatically moved forward.

I still don't know why. There is no problem when I calculate the addition, subtraction, and Multiplication Modulo. However, when I perform Division operations, I always report a data format error, so after changing "handler" to "/", it will be inexplicably solved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.