[JSP/Servlet] 자바빈으로 주차장 구현 예제
- BACKEND/Servlet&JSP
- 2017. 12. 27. 11:28
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"/>
<input type="submit" value="입차"/>
</form>
<form method="post">
주차장에 나간 차 번호 : <input type="text" name="exit"/>
<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);
}
}
결과
'BACKEND > Servlet&JSP' 카테고리의 다른 글
[JSP/Servlet] 방문자 카운트 올리는 예제 <JSP, BEAN> (0) | 2017.12.27 |
---|---|
[JSP/Servlet] 자바빈으로 회원가입 폼 만들기 예제 (0) | 2017.12.27 |
[JSP/Servlet] 자바빈으로 Count 구하는 예제 (0) | 2017.12.27 |
[JSP/Servlet] 설문조사 폼 만들기 예제 (0) | 2017.12.26 |
[JSP/Servlet] request에서 제공하는 메소드 예제 (0) | 2017.12.26 |