JAVA(利用jsp+javabean+servlet)实现简易计算器

2021-07-02 20:04

阅读:814

YPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

标签:isp   app   ring   title   ssi   patch   img   none   setattr   

代码解释基本都在代码中给出
技术分享图片技术分享图片
 1  2     pageEncoding="UTF-8"%>
 3 
 4 
 5 6
 7 简易计算器 8  
 9 
10 
11     
24     
25 第一个数:
26 运算符: 27 + 28 - 29 * 30 / 31
32 第二个数:
33
34 计算结果:35
36 37 38
Jsp页面
技术分享图片技术分享图片
package Calculation;

public class Cal {
    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;
    }
    
}
JvaBean代码
技术分享图片技术分享图片
 1 package CalculationServlet;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 import Calculation.Cal;
11 
12 /**
13  * Servlet implementation class CalServlet
14  */
15 @WebServlet("/CalServlet")
16 public class CalServlet extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18        
19     /**
20      * @see HttpServlet#HttpServlet()
21      */
22     public CalServlet() {
23         super();
24         // TODO Auto-generated constructor stub
25     }
26 
27     /**
28      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
29      */
30     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
31         // TODO Auto-generated method stub
32         response.getWriter().append("Served at: ").append(request.getContextPath());
33     }
34 
35     /**
36      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
37      */
38     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
39         // TODO Auto-generated method stub
40         //doGet(request, response);
41         response.setCharacterEncoding("UTF-8");
42         Cal cal = new Cal();
43         cal.setNum1(Float.parseFloat(request.getParameter("num1")));
44         cal.setOp(request.getParameter("operator"));
45         cal.setNum2(Float.parseFloat(request.getParameter("num2")));
46         float result = 0;
47         switch(cal.getOp().charAt(0)) {
48             case ‘+‘: result = cal.getNum1() + cal.getNum2();break;
49             case ‘-‘: result = cal.getNum1() - cal.getNum2();break;
50             case ‘*‘: result = cal.getNum1() * cal.getNum2();break;
51             case ‘/‘: result = cal.getNum1() / cal.getNum2();break;
52             //可以加入一个default
53         }
54         //System.out.println(result);
55         request.setAttribute("result", result);
56         request.setAttribute("cal", cal);  //----  传值不过去???
57         //request.getSession().setAttribute("cal", cal);
58         //新知识
59         //下边的这个函数就是服务器之间跳转  注意需要先调用getRequestDispatcher来获取这个对象  我是这么理解的
60         request.getRequestDispatcher("Cal.jsp").forward(request, response);
61     }
62 
63 }
Servlet代码

 

JAVA(利用jsp+javabean+servlet)实现简易计算器

标签:isp   app   ring   title   ssi   patch   img   none   setattr   

原文地址:https://www.cnblogs.com/Yinchen-One/p/9629583.html


评论


亲,登录后才可以留言!