java web實現(xiàn)簡易收費(fèi)站
本文實例為大家分享了java web實現(xiàn)簡易收費(fèi)站的具體代碼,供大家參考,具體內(nèi)容如下
一、目標(biāo)
頁面內(nèi)輸入車的類型和行駛公里數(shù),可以得到該車的收費(fèi)金額。注:小汽車:每公里5角。大巴車:每公里1元,營運(yùn)稅每次100元。
二、基礎(chǔ)知識
JavaBeans的使用
1、JavaWeb開發(fā)中常用JavaBeans來存放數(shù)據(jù)、封裝業(yè)務(wù)邏輯等。JavaBeans最大的優(yōu)點(diǎn)就是可以實現(xiàn)代碼的重用。2、作為JavaBeans使用的Java類需遵循三個規(guī)范:1).JavaBeans應(yīng)該是public類,并且具有無參數(shù)的public構(gòu)造方法2).JavaBeans類的成員變量一般被稱為屬性,對每個屬性訪問權(quán)限一般定義為private3).每個屬性通常定義兩個public方法,一個是訪問方法(getter),一個是修改方法(setter),使用它們訪問和修改JavaBeans的屬性值。
三、實現(xiàn)思路
1、輸入頁面:輸入汽車類型和行駛公里,提交給servlet2、servlet:讀取提交的數(shù)據(jù),生成相應(yīng)的汽車類類型(不能聲明小汽車類型或大巴車)的對象,調(diào)用對象的收費(fèi)方法,跳轉(zhuǎn)到收費(fèi)結(jié)果jsp。3、結(jié)果顯示頁面:讀取數(shù)據(jù)(javabean)的收費(fèi)金額,顯示結(jié)果(不能有任何腳本和java代碼)
四、代碼
charge-select.jsp(輸入界面)
<form action='vehicle.do' method='post'> <table> <tr> <td> 汽車類型: </td> <td> <select name='type'> <option value='0'>--請選擇--</option> <option value='car'>小汽車</option> <option value='bus'>大卡車</option> </select> </td> </tr> <tr> <td> 行駛里程/公里: </td> <td> <input type='text' name='mile'/> </td> </tr> <tr> <td> <input type='submit'/> </td> <td> <input type='reset'/> </td> </tr> </table></form>
charge-result.jsp(顯示金額界面)
//聲明javabeans<jsp:useBean type='charge.Vehicle' scope='request'/><html><head> <title>收費(fèi)結(jié)果</title></head><body>//javabeans的使用 價格:<jsp:getProperty name='v' property='money'/>元</body></html>
Vehicle.java
package charge;//Vehicle類public abstract class Vehicle { private float mile; private float money; public abstract float count(float mile); public Vehicle(){}; public Vehicle(float mile){ this.mile = mile; } public float getMile() { return this.mile; } public float getMoney(){ return this.money; } public void setMoney(float money){ this.money = money; }}//Vehicle的子類Carclass Car extends Vehicle{ private float mile; private float money; public Car(float mile) { super(mile); } //計算收費(fèi)金額 public float count(float mile){ float price; price =(float) 0.5*this.getMile(); return price; }}//Vehicle的子類Busclass Bus extends Vehicle{ private float mile; private float money; public Bus(float mile) { super(mile); } //計算收費(fèi)金額 public float count(float mile){ return (float) (mile+100); }}
VehicleServlet.java(計算金額)
package charge;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;import java.io.PrintWriter;@WebServlet(name = 'VehicleServlet',urlPatterns = '/vehicle.do')public class VehicleServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType('text/html,charset=utf-8'); //獲取輸入的信息 String type = request.getParameter('type'); float mile =Float.parseFloat(request.getParameter('mile')); float price=0; Vehicle v ; //分情況計算收費(fèi)金額 if(type.equals('car')){ v = new Car(mile); price = v.count(mile); v.setMoney(price); request.setAttribute('v',v); } else if(type.equals('bus')){ v = new Bus(mile); price = v.count(mile); v.setMoney(price); request.setAttribute('v',v); } //轉(zhuǎn)發(fā) RequestDispatcher dispatcher = request.getRequestDispatcher('/charge-result.jsp'); dispatcher.forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }}
上述僅部分代碼
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP.NET MVC獲取多級類別組合下的產(chǎn)品2. ASP.NET MVC實現(xiàn)橫向展示購物車3. ThinkPHP5 通過ajax插入圖片并實時顯示(完整代碼)4. Docker 容器健康檢查機(jī)制5. CSS3實現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效6. python使用openpyxl庫讀寫Excel表格的方法(增刪改查操作)7. python中asyncio異步編程學(xué)習(xí)8. python os.listdir()亂碼解決方案9. Python使用socket_TCP實現(xiàn)小文件下載功能10. ASP實現(xiàn)文件上傳的方法
