<jsp:useBean>
☞ 속성 영역(page, request, session, application)에 binding된 속성 객체를 lookup한다. 만약 가져오지 못하면 생성하여 그 영역에 binding 한다.
☞ 태그의 attribute(속성)
◎ id : 자바식별자, binding 시 설정 이름
◎ class : lookup할 클래스 이름. fully name으로 설정
◎ scope : 속성 영역, 값 : page(기본), request, session, application
◎ 예)
<jsp:useBean id="mto" class="dto.MemberDTO" scope="request" />
-> 의미
MemberDTO mto = (MemberDTO)request.getAttribute("mto");
if(mto==null) {
mto = new MemberDTO();
request.setAttribute("mto", mto);
}
RegisterServlet.java
☞ package : myjsp.actiontag
☞ req.jsp를 통해 입력받은 정보를 CutomerDTO객체에 저장하고 그 저장한 객체를 binding하여 요청 디스패치방식으로 res1.jsp와 res2.jsp로 전송한다.
소스보기
package myjsp.actiontag;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import myjsp.dto.CustomerDTO;
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//1. 요청 파라미터 조회
String id = request.getParameter("id");
String password = request.getParameter("password");
String name = request.getParameter("name");
String email = request.getParameter("email");
int age = -1;
try {
age= Integer.parseInt(request.getParameter("age"));
} catch(NumberFormatException e) {
e.printStackTrace();
}
//2. CustomerDTO 객체 생성 <- 요청 파라미터 값을 설정
CustomerDTO cto = new CustomerDTO(id, password, name, email, age);
//3. request scope에 2번의 객체를 binding
request.setAttribute("cto", cto);
HttpSession session = request.getSession();
session.setAttribute("session_cto", cto);
//4. /actiontag/res1.jsp로 요청 디스패치 방식으로 이동
RequestDispatcher rdp = request.getRequestDispatcher("/actiontag/res1.jsp");
rdp.forward(request, response);
}
}
CustomerDTO.java
☞ package : myjsp.dto
☞ id, password, name, email, age에 관한 생성자, setter/getter메소드, toString()메소드, hashCode() 메소드
소스보기
package myjsp.dto;
public class CustomerDTO {
private String id;
private String password;
private String name;
private String email;
private int age;
public CustomerDTO() { }
public CustomerDTO(String id, String password, String name, String email,
int age) {
super();
this.id = id;
this.password = password;
this.name = name;
this.email = email;
this.age = age;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "CustomerDTO [id=" + id + ", password=" + password + ", name="
+ name + ", email=" + email + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CustomerDTO other = (CustomerDTO) obj;
if (age != other.age)
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
}
req.jsp
☞ folder : WebContent/actiontag
소스보기
<%@ 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>Insert title here</title></head><body>고객 정보 등록<br><form action="/myjsp/register" method="post"> ID : <input type="text" name="id"><br> Password : <input type="password" name="password"><br> 이름 : <input type="text" name="name"><br> 나이 : <input type="text" name="age"><br> 이메일 : <input type="text" name="email"><br> <input type="submit" value="등록"></form></body></html>
res1.jsp
☞ foder : WebContent/actiontag
☞ jsp:useBean 을 사용하여 request scope에 공유된 CustomerDTO 객체를 출력
소스보기
<%@ 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">
<%--
<%
CustomerDTO mcto = (CustomerDTO)request.getAttribute("cto");
if(mcto==null) {
mcto = new CustomerDTO();
request.setAttribute("mcto", mcto);
}
%>
--%>
<jsp:useBean id="cto" class="myjsp.dto.CustomerDTO" scope="request"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
ID : <%= cto.getId() %><br>
Password : <%= cto.getPassword() %><br>
이름 : <%= cto.getName() %><br>
나이 : <%= cto.getAge() %><br>
이메일 : <%= cto.getEmail() %>
<p>
<a href="/myjsp/actiontag/res2.jsp">res2.jsp</a>
</p>
</body>
</html>
res2.jsp
☞ folder : WebContent/actiontag
☞ jsp:useBean 을 사용하여 session scope에 공유된 CustomerDTO 객체를 출력
소스보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="session_cto" class="myjsp.dto.CustomerDTO" scope="session" />
<!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>Insert title here</title>
</head>
<body>
Session Scope 결과<br>
ID : <%= session_cto.getId() %><br>
Password : <%= session_cto.getPassword() %><br>
이름 : <%= session_cto.getName() %><br>
나이 : <%= session_cto.getAge() %><br>
이메일 : <%= session_cto.getEmail() %><br>
</body>
</html>