java web實現(xiàn)簡易收費站
本文實例為大家分享了java web實現(xiàn)簡易收費站的具體代碼,供大家參考,具體內(nèi)容如下
一、目標
頁面內(nèi)輸入車的類型和行駛公里數(shù),可以得到該車的收費金額。
注:小汽車:每公里5角。大巴車:每公里1元,營運稅每次100元。
二、基礎知識
JavaBeans的使用
1、JavaWeb開發(fā)中常用JavaBeans來存放數(shù)據(jù)、封裝業(yè)務邏輯等。JavaBeans最大的優(yōu)點就是可以實現(xiàn)代碼的重用。
2、作為JavaBeans使用的Java類需遵循三個規(guī)范:
1).JavaBeans應該是public類,并且具有無參數(shù)的public構造方法
2).JavaBeans類的成員變量一般被稱為屬性,對每個屬性訪問權限一般定義為private
3).每個屬性通常定義兩個public方法,一個是訪問方法(getter),一個是修改方法(setter),使用它們訪問和修改JavaBeans的屬性值。
三、實現(xiàn)思路
1、輸入頁面:輸入汽車類型和行駛公里,提交給servlet
2、servlet:讀取提交的數(shù)據(jù),生成相應的汽車類類型(不能聲明小汽車類型或大巴車)的對象,調(diào)用對象的收費方法,跳轉到收費結果jsp。
3、結果顯示頁面:讀取數(shù)據(jù)(javabean)的收費金額,顯示結果(不能有任何腳本和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 id="v" type="charge.Vehicle" scope="request"/> <html> <head> <title>收費結果</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的子類Car class Car extends Vehicle{ private float mile; private float money; public Car(float mile) { super(mile); } //計算收費金額 public float count(float mile){ float price; price =(float) 0.5*this.getMile(); return price; } } //Vehicle的子類Bus class Bus extends Vehicle{ private float mile; private float money; public Bus(float mile) { super(mile); } //計算收費金額 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 ; //分情況計算收費金額 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); } //轉發(fā) RequestDispatcher dispatcher = request.getRequestDispatcher("/charge-result.jsp"); dispatcher.forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
上述僅部分代碼
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
mybatis插入數(shù)據(jù)后返回自增主鍵ID的兩種實現(xiàn)方式
這篇文章主要介紹了mybatis插入數(shù)據(jù)后返回自增主鍵ID的兩種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05spring?boot集成smart-doc自動生成接口文檔詳解
這篇文章主要介紹了spring?boot集成smart-doc自動生成接口文檔詳解,smart-doc是一款同時支持java?restful?api和Apache?Dubbo?rpc接口文檔生成的工具,smart-doc顛覆了傳統(tǒng)類似swagger這種大量采用注解侵入來生成文檔的實現(xiàn)方法2022-09-09java如何更改數(shù)據(jù)庫中的數(shù)據(jù)
這篇文章主要介紹了java如何更改數(shù)據(jù)庫中的數(shù)據(jù),修改數(shù)據(jù)庫是數(shù)據(jù)庫操作必不可少的一部分,使用Statement接口中的excuteUpdate()方法可以修改數(shù)據(jù)表中的數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧2021-11-11深入解析System.load 與 System.loadLibrary
以下是對System.load與System.loadLibrary進行了詳細的分析介紹。需要的朋友可以過來參考下2013-08-08在Eclipse IDE使用Gradle構建應用程序(圖文)
這篇文章主要介紹了在Eclipse IDE使用Gradle構建應用程序(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12