본문 바로가기

JSP, Servlet/JSP, Servlet 기초

[JSP/Servlet] 자바빈을 이용하여 게시판 만들기

자바빈을 이용하여 게시판을 만들어보자.

 

먼저 데이터를 저장할 자바빈 클래스를 작성한다.

package com.mission.javabeans;

public class BoardBean {
	private String name;
	private String pass;
	private String email;
	private String title;
	private String content;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}

 

 

게시판 작성 페이지를 만든다

<03_boardWriteForm.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
.grey {
	background-color: lightgrey;
	width: 150px;
	text-align: center;
}
</style>
</head>
<body>
	<form method="POST" action="03_boardWrite.jsp">
		<table>
			<thead>
				<h2>게시판 글쓰기</h2>
			</thead>
			<tr>
				<td class="grey"><b>작성자</b></td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td class="grey"><b>비밀번호</b></td>
				<td><input type="password" name="pass">(게시물 수정 삭제시 필요합니다.)</td>
			</tr>
			<tr>
				<td class="grey"><b>이메일</b></td>
				<td><input type="text" name="email" style="width: 300px;"></td>
			</tr>
			<tr>
				<td class="grey"><b>글 제목</b></td>
				<td><input type="text" name="title" style="width: 500px;"></td>
			</tr>
			<tr>
				<td class="grey"><b>글 내용</b></td>
				<td><textarea rows="10" cols="80" name="content"></textarea></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="등록"><input type="reset" value="다시하기"></td>
			</tr>
		</table>
	</form>
</body>
</html>

 

 

 

입력된 정보를 출력하는 페이지를 작성한다.

<03_boardWrite.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
.grey {
	background-color: lightgrey;
	width: 150px;
	text-align: center;
}
</style>
</head>
<body>
	<%
		request.setCharacterEncoding("UTF-8");
	%>
	<jsp:useBean class="com.mission.javabeans.BoardBean" id="board" scope="page" />
	<jsp:setProperty property="name" name="board" value='<%=request.getParameter("name")%>' />
	<jsp:setProperty property="pass" name="board" value='<%=request.getParameter("pass")%>' />
	<jsp:setProperty property="email" name="board" value='<%=request.getParameter("email")%>' />
	<jsp:setProperty property="title" name="board" value='<%=request.getParameter("title")%>' />
	<jsp:setProperty property="content" name="board" value='<%=request.getParameter("content")%>' />
	<table>
		<thead>
			<h2>게시판 글쓰기</h2>
		</thead>
		<tr>
			<td class="grey"><b>작성자</b></td>
			<td><jsp:getProperty name="board" property="name" /></td>
		</tr>
		<tr>
			<td class="grey"><b>비밀번호</b></td>
			<td><jsp:getProperty name="board" property="pass" /></td>
		</tr>
		<tr>
			<td class="grey"><b>이메일</b></td>
			<td><jsp:getProperty name="board" property="email" /></td>
		</tr>
		<tr>
			<td class="grey"><b>글 제목</b></td>
			<td><jsp:getProperty name="board" property="title" /></td>
		</tr>
		<tr>
			<td class="grey"><b>글 내용</b></td>
			<td><jsp:getProperty name="board" property="content" /></td>
		</tr>
	</table>
</body>
</html>

 

 

 

 

728x90
반응형