前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >servlet实现简单的计算器

servlet实现简单的计算器

作者头像
曼路
发布2018-10-18 15:32:47
2.3K0
发布2018-10-18 15:32:47
举报
文章被收录于专栏:浪淘沙浪淘沙

从今天开始,我会将这学期陆续学习的一些知识,发到网上,也会不断添加新的知识点。 今天,先用servlet编写一个简易的计算器。使用eclipse或myeclipse编写(需要配置jdk,tomcat.可以去其他博客下找教程) 首秀创建一个webproject工程,自己起名字。如果是用eclispe 编写的,那么要选择(Dynamic web project),并且一步一步的创建,最后一部要选择创建web.xml文件。

记得勾选Generate web.xml
记得勾选Generate web.xml

接下来进入正文:

本次共需要写4个部分: Cal.java :计算器的基本文件。即构成 CalServlet.java:逻辑文件,前台提交数据,进行处理,返回结果。 Cal.jsp:前台界面 ,用户输入信息并可得到结果。 web.xml:配置文件

Cal.jsp:

package Calculator;

public class Cal {
   private float num1;
   private String op;
   private float num2;
public float getNum1() {
    return num1;
}
public void setNum1(float num) {
    this.num1 = num;
}
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;
}

}

CalServlet.java:

package CalServlet;

import java.io.IOException;

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

import Calculator.Cal;

public class CalServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public CalServlet() {
        super();
    }

    /**
     * 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 {
        Cal cal=new Cal();
        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();

        }
        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 {

    }

}

Cal.jsp

<body>
      <form action="servlet/CalServlet"  method="get">
         操作数1:<input type="text" name="num1" value="${requestScope.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=" ${requestScope.cal.num2 }"><br>
         <input type="submit" value="计算"><br>
         结果:   <input type="text" value="${requestScope.result} ">
      </form>
  </body>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>CalServlet</servlet-name>
    <servlet-class>CalServlet.CalServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>CalServlet</servlet-name>
    <url-pattern>/servlet/CalServlet</url-pattern>
  </servlet-mapping>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

运行结果:

这里写图片描述
这里写图片描述

如果有不懂的地方,欢迎评论。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年01月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档