码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-09-11 19:36:43      阅读:384      评论:0      收藏:0      [点我收藏+]

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

代码解释基本都在代码中给出
技术分享图片
 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>简易计算器</title>
 8 </head> 
 9 <body>
10 
11     <!-- 
12         总结一下今天学到的知识
13         1、学到了EL表达式----》贼强哇  以前都不知道(调用的时候直接写&{里边写下边4中的一种之后添加东西}
14             ①:PageScope
15             ②:RequestScope
16             ③:SessionScope
17             ④:ApplicationScope
18         2、说一下自己在写完代码与看完老师代码之后出现的一些复杂的地方
19             ①:我在处理radio的时候使用for循环写的   但是,看了老师的三元表达式以后,我承认,我的复杂了  (无懈可击)
20         3、今天还学到了一个新知识(关于服务器跳转和客户端跳转)
21             ①:服务器跳转(快):在服务器之间跳转是可以使用request的
22             ②:客户端跳转(相对于服务器 不快):在客户端跳转中是应该使用session的
23      -->
24     <form action="CalServlet" method="post"  name="From" >
25         第一个数:<input type="text" name="num1" value="${requestScope.cal.num1}"><br>
26         运算符:
27         <input type="radio" name="operator" value="+" ${requestScope.cal.op=="+"?"checked":""}>+
28         <input type="radio" name="operator" value="-" ${requestScope.cal.op=="-"?"checked":""}>-
29         <input type="radio" name="operator" value="*" ${requestScope.cal.op=="*"?"checked":""}>*
30         <input type="radio" name="operator" value="/" ${requestScope.cal.op=="/"?"checked":""}>/
31         <br>
32         第二个数:<input type="text" name="num2" value="${requestScope.cal.num2}"><br>
33         <input type="submit" name="smt" value="提交"><br>
34         计算结果:<input type="text" name="result" value="${result}">
35     </form>
36 
37 </body>
38 </html>
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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!