20170828_chr_MyCalculator 用servlet编写计算器程序

20170828_chr_MyCalculator 用servlet编写计算器程序


20170828_chr_MyCalculator

  • /20170828_chr_MyCalculator/src/nuc/sw/servlet/CalServlet.java
package nuc.sw.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nuc.sw.vo.Calculator;

public class CalServlet extends HttpServlet {

    /**
         * Destruction of the servlet. <br>
         */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
         * The doDelete method of the servlet. <br>
         *
         * This method is called when a HTTP delete request is received.
         * 
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
    public void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Put your code here
    }

    /**
         * The doGet method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to get.
         * 
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Calculator cal = new Calculator();
        cal.setNum1(Float.parseFloat(request.getParameter("num1")));
        cal.setNum2(Float.parseFloat(request.getParameter("num2")));
        cal.setOp(request.getParameter("op"));
        float result = 0;
        switch (cal.getOp().charAt(0)) {
            case '+':
                result = cal.getNum1() + cal.getNum2();break;
            case '-':
                result = cal.getNum1() - cal.getNum2();break;
            case '*':
                result = cal.getNum1() * cal.getNum2();break;
            case '/':
                result = cal.getNum1() / cal.getNum2();break;
        }
        request.setAttribute("cal", cal);
        request.setAttribute("result", result);
        request.getRequestDispatcher("cal.jsp").forward(request, response);
    }

    /**
         * The doPost method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to post.
         * 
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    /**
         * Initialization of the servlet. <br>
         *
         * @throws ServletException if an error occurs
         */
    public void init() throws ServletException {
        // Put your code here
    }

}
  • /20170828_chr_MyCalculator/src/nuc/sw/vo/Calculator.java
package nuc.sw.vo;

public class Calculator {
    private float num1;
    private String op;
    private float num2;

        public float getNum1() {
            return num1;
        }

        public void setNum1(float num1) {
            this.num1 = num1;
        }

        public String getOp() {
            return op;
        }

        public void setOp(String op) {
            this.op = op;
        }

        public float getNum2() {
            return num2;
        }

        public void setNum2(float num2) {
            this.num2 = num2;
        }
}
  • /20170828_chr_MyCalculator/WebRoot/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>20170828_chr_MyCalculator</display-name>
  <display-name>CalServlet</display-name>
  <description>This is the description of my J2EE component</description>
  <servlet>
    <servlet-name>CalServlet</servlet-name>
    <servlet-class>nuc.sw.servlet.CalServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>CalServlet</servlet-name>
    <url-pattern>/CalServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>
  • /20170828_chr_MyCalculator/WebRoot/cal.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>简易计算器</title>
</head>
<body>
<form action="CalServlet" method="get">
    操作数1:<input type="text" name="num1"value="${cal.num1}"><br>
    运算符:<input type="radio" name="op" value="+"${requestScope.cal.op=='+'?"checked":""}>+
    <input type="radio" name="op" value="-"${requestScope.cal.op=='-'?"checked":""}>-
    <input type="radio" name="op" value="*"${requestScope.cal.op=='*'?"checked":""}>*
    <input type="radio" name="op" value="/"${requestScope.cal.op=='/'?"checked":""}>/<br>
    操作数2:<input type="text" name="num2" value="${cal.num2}"><br>
    <input type="submit" value="计算"><br>
    结果:<input type="text" value=" ${requestScope.result}" >
    </form>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值