[JSP/Servlet] 자바빈으로 주차장 구현 예제

parking.jsp

<%@page import="mybean.index.CarDto"%>

<%@ page import="java.util.*" %>

<%@ page language="java" contentType="text/html; charset=EUC-KR"%>

<!DOCTYPE html>

<html>

<head>

<title>Insert title here</title>

</head>

<jsp:useBean id="parking" class="mybean.index.ParkingBean" scope="application"/>

<jsp:useBean id="carDto" class="mybean.index.CarDto" />


<% request.setCharacterEncoding("euc-kr"); %>


<jsp:setProperty property="enter" name="parking" param="enter"/>

<jsp:setProperty property="exit" name="parking" />

<body>

<h1>주차 관리 프로그램</h1>

<form method="post">

주차장에 들어온 차 번호 : <input type="text" name="enter"/>

&nbsp;&nbsp;&nbsp;<input type="submit" value="입차"/>

  </form>

  <form method="post">

주차장에 나간 차 번호 : <input type="text" name="exit"/>

&nbsp;&nbsp;&nbsp;<input type="submit" value="출차"/>

  </form>

  <hr/>

  주차된 차량 목록 : <br><br>

  <table border="1">

  <tr>

  <th>차량 번호</th><th>입차 시간</th>

  </tr>

  <%

  ArrayList<CarDto> cars = parking.getCarInfo();

  for(int i=0; i<cars.size(); i++){

  //CarDto dto = cars.get(i);

  //carDto = cars.get(i);

  pageContext.setAttribute("carDto", cars.get(i));

  %>

  <tr>

  <td><jsp:getProperty name="carDto" property="plateNumber"/></td>

  <td><jsp:getProperty name="carDto" property="parkingTime"/></td>

  </tr>

  <%

  }

  %>

  </table>

  <br>

  총 주차대수 : <jsp:getProperty property="counter" name="parking"/>

</body>

</html>


ParkingBean.java

package mybean.index;


import java.util.ArrayList;


public class ParkingBean {

private ArrayList<CarDto> carList = new ArrayList<CarDto>(); //제너릭으로 TypeSafe

public void setEnter(String plate) {

CarDto dto = new CarDto(plate, System.currentTimeMillis());

carList.add(dto);

}

public void setExit(String plate) {

CarDto dto = null;

for(int i=0; i<carList.size(); i++) {

dto = carList.get(i);

if(plate.equals(dto.getPlateNumber())) {

carList.remove(i);

break;

}

}

}

public int getCounter() {

return carList.size();

}

public ArrayList getCarInfo() {

return carList;

}

}


CarDto.java

package mybean.index;


import java.util.Date;


public class CarDto {

private String plateNumber;

private long parkingTime;

public CarDto() {}

public CarDto(String plateNumber, long parkingTime) {

super();

this.plateNumber = plateNumber;

this.parkingTime = parkingTime;

}

public String getPlateNumber() {

return plateNumber;

}

public Date getParkingTime() {

return new Date(parkingTime);

}

}



결과


댓글

Designed by JB FACTORY