簡易Servlet計算機,servlet計算機

來源:互聯網
上載者:User

簡易Servlet計算機,servlet計算機

編寫一個簡易的Servlet計算機,暫時僅能實現 + - * / % 五種運算

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>10 <form action="TestJSQ" method="post">11 <input type="text" name="shu1">12 <input type="text" name="yunsuan" style="font-size: 12px; width: 12px;">13 <input type="text" name="shu2">14 <input type="submit" value="=">15 16 </form>17 </body>18 </html>View Code

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 9 public class Test_JSQ extends HttpServlet {10 private static final long serialVersionUID = 1L;11 12 public Test_JSQ() {13 super();14 15 }16 17 protected void doGet(HttpServletRequest request, HttpServletResponse response)18 throws ServletException, IOException {19 20 response.setCharacterEncoding("UTF-8");21 response.setContentType("text/html");22 Double num1 = Double.parseDouble(request.getParameter("shu1"));23 Double num2 = Double.parseDouble(request.getParameter("shu2"));24 String ysf = request.getParameter("yunsuan");25 26 Double jg = 0.0;27 if (ysf.equals("+")) {28 jg = num1 + num2;29 response.getWriter().write(num1 + " + " + num2 + " = " + jg);30 } else if (ysf.equals("-")) {31 jg = num1 - num2;32 response.getWriter().write(num1 + " - " + num2 + " = " + jg);33 } else if (ysf.equals("*")) {34 jg = num1 * num2;35 response.getWriter().write(num1 + " × " + num2 + " = " + jg);36 } else if (ysf.equals("/")) {37 if (num2 != 0) {38 jg = num1 / num2;39 response.getWriter().write(num1 + " ÷ " + num2 + " = " + jg);40 } else {41 response.getWriter().write("除數不能為零");42 }43 } else if (ysf.equals("%")) {44 jg = num1 % num2;45 response.getWriter().write(num1 + " % " + num2 + " = " + jg);46 }47 48 49 }50 51 protected void doPost(HttpServletRequest request, HttpServletResponse response)52 throws ServletException, IOException {53 54 doGet(request, response);55 }56 57 }View Code

運行:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.