servlet實現簡單的計算器
阿新 • • 發佈:2019-02-11
從今天開始,我會將這學期陸續學習的一些知識,發到網上,也會不斷新增新的知識點。
今天,先用servlet編寫一個簡易的計算器。使用eclipse或myeclipse編寫(需要配置jdk,tomcat.可以去其他部落格下找教程)
首秀建立一個webproject工程,自己起名字。如果是用eclispe 編寫的,那麼要選擇(Dynamic web project),並且一步一步的建立,最後一部要選擇建立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>
執行結果:
如果有不懂的地方,歡迎評論。