Creating the Back-end.
Create data transporter and controller.
To collect data for each registration we need a java class
which we can use to create new objects for each car registration.
-
Create a Java class in Netbeans, Car, with the package lab.
private String regNo;
private String cartype;
private int model;
private double dayPrice;
This is all the member variables of a Car object, but we need get/set accessors for all the member variables.
-
Place the cursor after the last member variable and inside the class
and right click to get the menu for inserting get/set accessors
for all the member variables.
-
Select Insert code... from the menu and then Getter and Setter ... from the
new menu.
-
Select all fields and click the Generate button. You should now have
get/set accessors for all the member variables in the class.
We like to have an object that contain all the registered cars
, with some kind of functionality and can be stored as an object
on the Http Session object.
-
Create a Java class in Netbeans, CarPark, with the package lab.
private Vector<Car> cars;
public CarPark() {
cars= new Vector<Car>();
}
public Vector<Car> getCars() {
return cars;
}
public void addCar(Car car) {
cars.add(car);
}
-
Right click inside the class and select Fix Imports
The car's Vector variable member is meant to hold all registered cars.
The addCar() method is to add new car to the Vector, cars, and the getCars()
method will return the Vector of all cars.
We have now the tools to hold the data and then we need the Servlet
to control flow of data and in this case handling the
presentation of registered cars.
Create the Servlet.
-
Create a Servlet in Netbeans
with the name CarParkServlet and package name show.
With help from Netbeans we have now created a Servlet that have a doGet() method
and a doPost() method, which both call the processRequest() method.
In this way, we have a common handling for both GET and POST type requests.
It is now for us to put some code inside the try braces in the processRequest() method.
HttpSession session = request.getSession();
CarPark carPark = (CarPark) session.getAttribute("carPark");
if (carPark == null) {
carPark = new CarPark();
session.setAttribute("carPark", carPark);
}
Enumeration enumr = request.getParameterNames();
if (enumr.hasMoreElements()) {
if (!request.getParameter("regNo").equals("")) {
Car car = new Car();
car.setRegNo(request.getParameter("regNo"));
car.setCartype(request.getParameter("cartype"));
car.setModel(Integer.parseInt(request.getParameter("model")));
car.setDayPrice(Double.parseDouble(request.getParameter("dayPrice")));
carPark.addCar(car);
}
}
Vector<Car> cars = carPark.getCars();
out.println("<html><body>");
out.println(" <h2>Registered Cars</h2>");
out.println(" <table border=\"1px\" >");
out.println(" <tr>");
out.println(" <td align=\"left\" style=\"width:120px\" >RegNo:</td>");
out.println(" <td align=\"left\" style=\"width:120px\">Cartype:</td>");
out.println(" <td align=\"left\" style=\"width:120px\">Model:</td>");
out.println(" <td align=\"left\" style=\"width:120px\">DayPrice:</td>");
out.println(" </tr>");
if (!cars.isEmpty()) {
for (int i = 0; i < cars.size(); i++) {
Car car = (Car) cars.get(i);
out.println(" <tr>");
out.println(" <td align=\"left\">" + car.getRegNo() + "</td>");
out.println(" <td align=\"left\">" + car.getCartype() + "</td>");
out.println(" <td align=\"left\">" +
String.valueOf(car.getModel()) + "</td>");
out.println(" <td align=\"left\">" +
String.valueOf(car.getDayPrice()) + "</td>");
out.println(" </tr>");
}
}
out.println(" </table>");
out.println(" <p><a href=\"/Servlet01/RegCars.html\">Return" +
"</a> car registration</p>");
out.println("</body></html>");
-
Right click inside the class, CarParkServlet, and select Fix Imports.
For more understanding please read the comments in this code.
Configuration.
The last thing to do is to change the welcome file name so
open the web.xml file in the Configuration folder of the project, select Pages, and then change the Welcome File(s) to be RegCars.html.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>CarparkServlet</servlet-name>
<servlet-class>show.CarparkServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CarparkServlet</servlet-name>
<url-pattern>/CarparkServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>RegCars.html</welcome-file>
</welcome-file-list>
</web-app>
Run the Java Application.
-
Right click the project, Servlet01, and select run from the menu.
-
This will deploy the project to the web-server and start a session in you default browser with the url http://localhost:8090/Servlet01.
Enter RegNo, Cartype, Model, DayPrice and press the submit button.