Bean(DTO) 객체의 활용

데이터를 담아서 전송하는 객체를 활용한 정보 입력 및 전달

 

예제)

com.baen>PersonInfo.java

package com.bean;

public class PersonInfo {
	private String name;
	private String gender;
	private int age;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

com.controller>PersonController.java

package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

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 com.bean.PersonInfo;

@WebServlet({ "/pInput", "/update" })
public class PersonController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}

	private void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String command = request.getServletPath();
		
		PersonInfo pInfo = null;
		
		if(command.equals("/pInput")) {
			pInfo = (PersonInfo)request.getAttribute("pInfo");
			System.out.println("이름 : " + pInfo.getName());
			pInfo = (PersonInfo)request.getAttribute("pInfo");
			System.out.println("성별 : " + pInfo.getGender());
			pInfo = (PersonInfo)request.getAttribute("pInfo");
			System.out.println("나이 : " + pInfo.getAge());
			
			PrintWriter out = response.getWriter();
			out.println("<html>");
			out.println("<head>");
			out.println("<title>입력결과</title>");
			out.println("</head>");
			out.println("<body>");
			out.println("<h2>입력 완료</h2>");
			out.println("<a href='index.jsp'>시작으로</a>");
			out.println("</body>");
			out.println("</html>");
			
		} else if(command.equals("/update")) {
			pInfo = new PersonInfo();
			pInfo.setName("홍길동");
			pInfo.setGender("남자");
			pInfo.setAge(25);
			
			request.setAttribute("uInfo", pInfo);
			
			RequestDispatcher dis = request.getRequestDispatcher("pUpdate.jsp");
			
			dis.forward(request,  response);
		}
	}

}

doProcess 메소드에서는 input(회원가입)으로 들어왔는지, update(수정)들어왔는지 구분해서 처리해야함.

request.getServletPath();를 통해 어떤 서블릿을 통해서 가야하는지를 구해야함.

 

입력받은 결과가 /pInput(회원가입)을 통해 들어왔다면 내용 확인을 위해 입력받은 내용을 콘솔에 출력해준다.

service → DAO → DB 순서로 DB에 입력처리한다.

입력받은 결과가 /update를 통해 들어왔다면 DB에서 해당 key로 검색한 정보를 가져와서 jsp 페이지로 전달.

여기서는 임의의 데이터를 전달함.

 

pInputFrm.jsp

<h2>회원 정보 입력</h2>
<form action="inProc.jsp" method="post">
<!-- 이름, 성별, 나이를 입력하도록 작성해 주세요. -->
<table>
	<tr>
		<th width="100px">이름</th>
		<td width="200px"><input type="text" name="name"></td>
	</tr>
	<tr>
		<th>성별</th>
		<td width="200px"><input type="text" name="gender"></td>
	</tr>
	<tr>
		<th>나이</th>
		<td width="200px"><input type="text" name="age"></td>
	</tr>
	<tr>
		<td colspan="2" style="text-align: center">
			<input type="submit" value="전송">
			<input type="reset" value="취소">
		</td>
	</tr>
</table>
</form>

회원 정보를 입력 받는 페이지 생성.

 

pUpdate.jsp

<h3>검색한 회원</h3>
<form action="inProc.jsp" method="post">
	<table>
		<tr>
			<th width="100px">이름 : </th>
			<td width="200px">
				<input type="text" name="name" value="${uInfo.name}">
			</td>
		</tr>
		<tr>
			<th>성별 : </th>
			<td width="200px">
				<input type="text" name="gender" value="${uInfo.gender}">
			</td>
		</tr>
		<tr>
			<th>나이 : </th>
			<td width="200px">
				<input type="text" name="age" value="${uInfo.age}">
			</td>
		</tr>
		<tr>
			<td colspan="2" style="text-align: center">
				<input type="submit" value="전송">
				<input type="reset" value="취소">
			</td>
		</tr>
	</table>
</form>
<a href="index.jsp">시작으로</a>

저장된 회원 정보를 확인하는 페이지 생성.

 

inProc.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="pInfo" class="com.bean.PersonInfo" scope="request"/>
<jsp:setProperty property="*" name="pInfo"/>
<jsp:forward page="/pInput"/>

PersonInfo DTO의 이름을 pInfo로 설정하고 DB 사용함.

 

 

+ Recent posts