JSP

[수업] Bean, 액션 태그, Redirect/Forward

hs_developer 2022. 7. 21. 17:57

**Bean

 

MemberBean.java

package com.sist.dao;

/*
 * Bean(JSP) 
 * -> 데이터를 모아서 한 번에 전송/ 출력
 * -> VO(Spring), DTO(MyBatis), Entity(JPA)
 * -> 읽기/ 쓰기 -> getter/setter
 * -> 데이터베이스 컬럼 매칭
 */

public class MemberBean {

	private String name, address, tel, sex;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

 


 

 

input.java

<%@ 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>
<link rel="stylesheet" href="table.css">
<style type="text/css">

.container {
	margin-top: 50px;
	width: 100%; /* 가운데 정렬 */
}
.table_content {
	margin: 0px auto;
	width: 450px;
}
h1 {
	text-align: center;
}

</style>
</head>
<body>
	<div class="container">
		<h1>회원 정보</h1>
		<form method="post" action="output2.jsp">
		<table class="table_content">
			<tr>
				<th width=30%>이름</th>
				<td width=70%>
					<input type=text name=name size=15>
				</td>
			</tr>
			<tr>
				<th width=30%>성별</th>
				<td width=70%>
					<input type="radio" name=sex value="남자" checked="checked">남자
					<input type="radio" name=sex value="여자">여자
				</td>
			</tr>
			<tr>
				<th width=30%>나이</th>
				<td width=70%>
					<input type=number name=age min="20" max="50">
				</td>
			</tr>
			<tr>
				<th width=30%>주소</th>
				<td width=70%>
					<input type=text name=address size=25>
				</td>
			</tr>
			<tr>
				<th width=30%>전화</th>
				<td width=70%>
					<input type=text name=tel size=20>
				</td>
			</tr>
			<tr>
				<td colspan="2" align=center>
					<button>전송</button>
				</td>
			</tr>
		</table>
		</form>
	</div>
</body>
</html>

 

 

output.jsp

<%@page import="com.sist.dao.MemberBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<% 
	request.setCharacterEncoding("UTF-8");

	MemberBean mb= new MemberBean();
	String name= request.getParameter("name");
	String sex= request.getParameter("sex");
	String age= request.getParameter("age");
	String address= request.getParameter("address");
	String tel= request.getParameter("tel");
	
	mb.setName(name);
	mb.setSex(sex);
	mb.setAge(Integer.parseInt(age));
	mb.setAddress(address);
	mb.setTel(tel);
			
%>    
    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	이름: <%= mb.getName() %><br>
	성별: <%= mb.getSex() %><br>
	나이: <%= mb.getAge() %><br>
	주소: <%= mb.getAddress() %><br>
	전화: <%= mb.getTel() %><br>
</body>
</html>

 

 

output2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.sist.dao.*"%>
    
<% 
	request.setCharacterEncoding("UTF-8");
%>

<jsp:useBean id="mb" class="com.sist.dao.MemberBean">
	<jsp:setProperty name="mb" property="*" />
</jsp:useBean>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	이름: <%= mb.getName() %><br>
	성별: <%= mb.getSex() %><br>
	나이: <%= mb.getAge() %><br>
	주소: <%= mb.getAddress() %><br>
	전화: <%= mb.getTel() %><br>
</body>
</html>

 


 

output, output2의 차이점은 JSP action tags 사용 유무라는 것.

 

output.jsp

<% 
	request.setCharacterEncoding("UTF-8");

	MemberBean mb= new MemberBean();
	String name= request.getParameter("name");
	String sex= request.getParameter("sex");
	String age= request.getParameter("age");
	String address= request.getParameter("address");
	String tel= request.getParameter("tel");
	
	mb.setName(name);
	mb.setSex(sex);
	mb.setAge(Integer.parseInt(age));
	mb.setAddress(address);
	mb.setTel(tel);
			
%>

 

output2.jsp

<jsp:useBean id="mb" class="com.sist.dao.MemberBean">
	<jsp:setProperty name="mb" property="*" />
</jsp:useBean>

 

 

모두 같은 기능으로 동작한다. 다 간결한 표현인 JSP action tags 사용 권장.

 

 


 

**redirect, forward

 

input.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>
</head>
<body>
	<a href="redirect.jsp?id=admin">Redirect</a><br>
	<a href="forward.jsp?id=admin2">Forward</a><!-- MVC에서 사용 -->
</body>
</html>

 

 

 

output.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>
</head>
<body>
	<h1>ID: <%= request.getParameter("id") %></h1>
</body>
</html>

 

 

redirect.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	response.sendRedirect("output.jsp");
%>

 

forward.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<jsp:forward page="output.jsp"></jsp:forward>