java web實(shí)現(xiàn)簡(jiǎn)易收費(fèi)站
本文實(shí)例為大家分享了java web實(shí)現(xiàn)簡(jiǎn)易收費(fèi)站的具體代碼,供大家參考,具體內(nèi)容如下
一、目標(biāo)
頁面內(nèi)輸入車的類型和行駛公里數(shù),可以得到該車的收費(fèi)金額。注:小汽車:每公里5角。大巴車:每公里1元,營(yíng)運(yùn)稅每次100元。
二、基礎(chǔ)知識(shí)
JavaBeans的使用
1、JavaWeb開發(fā)中常用JavaBeans來存放數(shù)據(jù)、封裝業(yè)務(wù)邏輯等。JavaBeans最大的優(yōu)點(diǎn)就是可以實(shí)現(xiàn)代碼的重用。2、作為JavaBeans使用的Java類需遵循三個(gè)規(guī)范:1).JavaBeans應(yīng)該是public類,并且具有無參數(shù)的public構(gòu)造方法2).JavaBeans類的成員變量一般被稱為屬性,對(duì)每個(gè)屬性訪問權(quán)限一般定義為private3).每個(gè)屬性通常定義兩個(gè)public方法,一個(gè)是訪問方法(getter),一個(gè)是修改方法(setter),使用它們?cè)L問和修改JavaBeans的屬性值。
三、實(shí)現(xiàn)思路
1、輸入頁面:輸入汽車類型和行駛公里,提交給servlet2、servlet:讀取提交的數(shù)據(jù),生成相應(yīng)的汽車類類型(不能聲明小汽車類型或大巴車)的對(duì)象,調(diào)用對(duì)象的收費(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'>--請(qǐng)選擇--</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的使用 價(jià)格:<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); } //計(jì)算收費(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); } //計(jì)算收費(fèi)金額 public float count(float mile){ return (float) (mile+100); }}
VehicleServlet.java(計(jì)算金額)
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 ; //分情況計(jì)算收費(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 { }}
上述僅部分代碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 一個(gè) 2 年 Android 開發(fā)者的 18 條忠告2. Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼3. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼4. vue-drag-chart 拖動(dòng)/縮放圖表組件的實(shí)例代碼5. 什么是Python變量作用域6. Spring的異常重試框架Spring Retry簡(jiǎn)單配置操作7. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程8. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析9. Android studio 解決logcat無過濾工具欄的操作10. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
