note

JSP 자바빈 사용하기(자동 생성방법) , 액션태그,getProperty,setProperty 사용 본문

JSP/기본

JSP 자바빈 사용하기(자동 생성방법) , 액션태그,getProperty,setProperty 사용

투한 2012. 2. 8. 10:14



Java Resource -> 패키지 -> 클래스

 
자바빈 자동으로 은닉화 시키는 방법
(정의만 내려주고 아래와 같이 하면됨)


쉽게 은닉화 캡슐화 시키는 방법

 


생성됨





선택후 ok






자바빈 생성확인 메소드 자동 생성 방법





 



 




실행화면




실행화면 


 


MemberInfo.java
package chap11.member;

import java.util.Date;

public class MemberInfo {

	private String id;
	private String password;
	private String name;
	private String address;
	private String email;
	private Date registerDate;
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	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 getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getRegisterDate() {
		return registerDate;
	}
	public void setRegisterDate(Date registerDate) {
		this.registerDate = registerDate;
	}
	//생성 확인 메소드
	@Override
	public String toString() {
		return "MemberInfo [id=" + id + ", password=" + password + ", name="
				+ name + ", address=" + address + ", email=" + email + "]";
	}
	
	
}


membershipForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>회원가입 입력 폼</title>
</head>
<body>
<form action="<%=request.getContextPath() %>/processJoining.jsp" method="post">


<table border="1" cellpadding="0" cellspacing="0" >
<tr>
	<td>아이디</td>
	<td><input type="text" name="id" size="10"></td>
	<td>비밀번호</td>
	<td><input type="text" name="password" size="10"></td>
</tr>
<tr>
	<td>이름</td>
	<td><input type="text" name="name" size="10"></td>
	<td>이메일</td>
	<td><input type="text" name="email" size="10"></td>
</tr>
<tr>
	<td>주소</td>
	<td colspan="3"><input type="text" name="address" size="30"></td>
</tr>
<tr>
	<td colspan="4" align="center"><input type="submit" value="회원가입"></td>
</tr>
</table>
</form>
</body>
</html>


processJoining.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import ="chap11.member.MemberInfo" %>
<%
	request.setCharacterEncoding("UTF-8");

	//자바빈 객체 생성
	MemberInfo member  = new MemberInfo();
	member.setId(request.getParameter("id"));
	member.setPassword(request.getParameter("password"));
	member.setName(request.getParameter("name"));
	member.setEmail(request.getParameter("email"));
	member.setAddress(request.getParameter("address"));
	
	//자바빈 객체 생성된 후 데이터가 정상적으로 저장되었는지
	//콘솔환경에서 확인하기 위해
	System.out.println(member);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>자바빈 연습</title>
</head>
<body>
아이디 : <%=member.getId() %><br>
암호 : <%=member.getPassword() %><br>
이름 : <%=member.getName() %><br>
이메일 : <%=member.getEmail() %><br>
주소 : <%=member.getAddress() %><br>
</body>
</html>

콘솔창 확인





<jsp:getProperty>액션태그와 <jsp:setProperty>액션 태그 사용



byAction 폴더 생성후 밑에 있는 파일들을 복사 -> 붙여넣기
 


실행 화면

 


실행화면


 


membershipForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>회원가입 입력 폼</title>
</head>
<body>
<form action="<%=request.getContextPath() %>/byAction/processJoining.jsp" method="post">


<table border="1" cellpadding="0" cellspacing="0" >
<tr>
	<td>아이디</td>
	<td><input type="text" name="id" size="10"></td>
	<td>비밀번호</td>
	<td><input type="text" name="password" size="10"></td>
</tr>
<tr>
	<td>이름</td>
	<td><input type="text" name="name" size="10"></td>
	<td>이메일</td>
	<td><input type="text" name="email" size="10"></td>
</tr>
<tr>
	<td>주소</td>
	<td colspan="3"><input type="text" name="address" size="30"></td>
</tr>
<tr>
	<td colspan="4" align="center"><input type="submit" value="회원가입"></td>
</tr>
</table>
</form>
</body>
</html>


processJoining.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String id = request.getParameter("id");
	String password = request.getParameter("password");
	String name = request.getParameter("name");
	String email = request.getParameter("email");
	String address = request.getParameter("address");
%>
<!-- usebean으로 객체 생성 -->
<jsp:useBean id="member" class="chap11.member.MemberInfo"></jsp:useBean>
<!-- 데이터 셋팅 -->
<jsp:setProperty name="member" property="id" value="<%=id %>"/>
<jsp:setProperty name="member" property="password" value="<%=password %>"/>
<jsp:setProperty name="member" property="name" value="<%=name %>"/>
<jsp:setProperty name="member" property="email" value="<%=email %>"/>
<jsp:setProperty name="member" property="address" value="<%=address %>"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>자바빈 연습</title>
</head>
<body>
<!-- get Property로 값 가져오기 -->
아이디 : <jsp:getProperty name="member" property="id" /><br>
암호 : <jsp:getProperty name="member" property="password" /><br>
이름 : <jsp:getProperty name="member" property="name" /><br>
이메일 : <jsp:getProperty name="member" property="email" /><br>
주소 : <jsp:getProperty name="member" property="address" /><br>
</body>
</html>




setProperty 코드 줄이기 




자바빈에 있는 멤버변수와 폼에 있는 name을 일치 시키면 간략하게 코드 사용 가능




변경 전
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
	String id = request.getParameter("id");
	String password = request.getParameter("password");
	String name = request.getParameter("name");
	String email = request.getParameter("email");
	String address = request.getParameter("address");
%>
<!-- usebean으로 객체 생성 -->
<jsp:useBean id="member" class="chap11.member.MemberInfo"></jsp:useBean>
<!-- 데이터 셋팅 -->
<jsp:setProperty name="member" property="id" value="<%=id %>"/>
<jsp:setProperty name="member" property="password" value="<%=password %>"/>
<jsp:setProperty name="member" property="name" value="<%=name %>"/>
<jsp:setProperty name="member" property="email" value="<%=email %>"/>
<jsp:setProperty name="member" property="address" value="<%=address %>"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>자바빈 연습</title>
</head>
<body>
<!-- get Property로 값 가져오기 -->
아이디 : <jsp:getProperty name="member" property="id" /><br>
암호 : <jsp:getProperty name="member" property="password" /><br>
이름 : <jsp:getProperty name="member" property="name" /><br>
이메일 : <jsp:getProperty name="member" property="email" /><br>
주소 : <jsp:getProperty name="member" property="address" /><br>
</body>
</html>

변경 후
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
%>
<!-- usebean으로 객체 생성 -->
<jsp:useBean id="member" class="chap11.member.MemberInfo"></jsp:useBean>
<!-- 데이터 셋팅 -->
<jsp:setProperty name="member" property="*"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>자바빈 연습</title>
</head>
<body>
<!-- get Property로 값 가져오기 -->
아이디 : <jsp:getProperty name="member" property="id" /><br>
암호 : <jsp:getProperty name="member" property="password" /><br>
이름 : <jsp:getProperty name="member" property="name" /><br>
이메일 : <jsp:getProperty name="member" property="email" /><br>
주소 : <jsp:getProperty name="member" property="address" /><br>
</body>
</html>






실행화면도 그전과 일치

'JSP > 기본' 카테고리의 다른 글

JSP MODEL1 방식  (0) 2012.02.10
PreparedStatement Statement 의 차이점(SELECT),(INSERT)  (0) 2012.02.09
JSP 세션 Session  (0) 2012.02.07
JSP 쿠키 생성,보기,삭제,수정  (0) 2012.02.07
JSP 에러페이지 만들기  (0) 2012.02.07