久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

java web實現(xiàn)簡易收費(fèi)站

瀏覽:6日期:2022-08-20 15:56:03

本文實例為大家分享了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)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 洛阳市| 渝中区| 衡山县| 武清区| 杨浦区| 原平市| 临洮县| 灵川县| 满城县| 弥勒县| 武鸣县| 翁源县| 高要市| 项城市| 丹棱县| 大埔县| 吴桥县| 镇远县| 濉溪县| 花莲市| 上思县| 观塘区| 台南县| 伊春市| 大关县| 姚安县| 招远市| 洛浦县| 陇西县| 武冈市| 榆中县| 富裕县| 拜城县| 仲巴县| 阳新县| 罗源县| 衡阳市| 宜川县| 鹤庆县| 离岛区| 长沙县|