Cohe
5. javaBean 본문
728x90
반응형
- java bean
- 객체, getter와 setter로 이뤄져있다.
- jsp:useBean
- id : 인스턴스 객체 이름
- class : 실제 객체가 있는 객체의 주소, 경로 → 패키지까지 다 써야 한다.
- scope: 범위
- page : 하나의 페이지만 사용되는 영역
- request, session, application
실습
- src/main/java 에 com.bean 이라는 패키지 생성
- User라는 클래스 작성
- 자바 빈 클래스는 form과 데이터 베이스 통신과정에서 변수처리를 쉽게 하기 위해서 사용하는 클래스 관련된 변수들을 선언, getter, setter를 반드시 생성한다.
- 기본 생성자도 반드시 생성한다.
- 빈 객체 완성~!
- webapp에 bean 폴더 생성
- beanBasic.jsp 생성
- 코드
-
더보기
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> <form action = "bean_basic_ok.jsp" method="post"> ID : <input type="text" name="id"><br> PW : <input type="password" name="pw"><br> NAME : <input type="text" name="name"><br> EMAIL : <input type="email" name="email"><br> <input type = "submit" value= "확인"> </form> </body> </html>
- 이런 여러 개의 파라미터 값을 개별적으로 전달 할 수 있지만 클래스 객체를 생성해서 전달하면 보다 쉽게 처리가 가능하다.
- bean_basic_ok.jsp 생성
- id : 사용할 변수명
- class : 실제 경로
- scope : 빈의 사용범위
- <jsp:setProperty property="email" name="u1" value="<%=email %>"/>
- property : 멤버 변수 명
- name : 클래스 이름
- value는 집어넣을 값
- <jsp:setProperty property="*" name="u1"/> <!-- 자동으로 알아서 들어감 -→
- 단 파라미터와 멤버변수 값이 같아야 한다.
- 코드
-
더보기
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <% request.setCharacterEncoding("utf-8"); /*String id = request.getParameter("id"); String pw = request.getParameter("pw"); String name = request.getParameter("name"); String email = request.getParameter("email");*/ %> <!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body> <jsp:useBean id="u1" class="com.bean.User" scope="page"/> <jsp:setProperty property="*" name="u1"/> <!-- 자동으로 알아서 들어감 --> <%-- <!-- setProperty는 setter 메서드 사용 --> <jsp:setProperty property="id" name="u1" value="<%=id %>"/> <jsp:setProperty property="pw" name="u1" value="<%=pw %>"/> <jsp:setProperty property="name" name="u1" value="<%=name %>"/> <jsp:setProperty property="email" name="u1" value="<%=email %>"/> --%> <!-- getProperty는 getter 메서드 사용 --> <jsp:getProperty property="id" name="u1"/><br> <jsp:getProperty property="pw" name="u1"/><br> <jsp:getProperty property="name" name="u1"/><br> <jsp:getProperty property="email" name="u1"/><br> </body> </html>
- java 코드로도 bean을 사용할 수 있다.
<%@page import="com.bean.User"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>bean_make_java.jsp</title>
</head>
<body>
<h2>자바코드를 이용해 bean 사용</h2>
<%
User user = new User();
user.setId("k");
user.setPw("k");
user.setEmail("email");
user.setName("name");
request.setAttribute("user", user);
RequestDispatcher dp = request.getRequestDispatcher("bean_use_java.jsp");
dp.forward(request, response);
%>
</body>
</html>
<%@page import="com.bean.User"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
User user = (User)request.getAttribute("user");
%>
<%=user.getId() %>
<%=user.getPw() %>
</body>
</html>
action tag로 java bean 사용하기
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h2>액션태그로 자바 bean 사용</h2>
<jsp:useBean id="user" class="com.bean.User" scope="request"/>
<jsp:setProperty property="id" name="user" value="kkk123"/>
<jsp:setProperty property="name" name="user" value="chaeHyeonyeong"/>
<jsp:forward page="bean_use_tag.jsp"/>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>bean_use_tag.jsp</title>
</head>
<body>
<jsp:useBean id="user" class="com.bean.User" scope="request" />
<jsp:getProperty property="id" name="user"/>
<jsp:getProperty property="name" name="user"/>
</body>
</html>
quiz
- 앞에서 넘어온 폼 값을 받아서 처리
- pw와 pw_check 가 같다면
- user 객체에 form에서 넘어온 id, pw를 저장하고
- user 객체를 quiz03.jsp로 넘깁니다 : “화면에 id님의 회원 가입을 축하합니다. 비밀번호는 xxx입니다”
- pw와 pw_check가 다르다면 quiz04.jsp로 넘어가서 “회원가입에 실패했습니다.” 출력
quiz01
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="quiz02.jsp" method="post">
ID : <input type="text" name="id"><br>
PW : <input type="password" name="pw"><br>
PW 확인 : <input type="password" name="pw_check"><br>
<input type = "submit" value= "확인">
</form>
</body>
</html>
quiz02
<%@page import="com.bean.User"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String pwC = request.getParameter("pw_check");
User user = new User();
user.setId(id);
user.setPw(pw);
if(pwC.equals(pw)){
request.setAttribute("user", user);
RequestDispatcher dp = request.getRequestDispatcher("quiz03.jsp");
dp.forward(request, response);
}
else{
//그냥 리다이엑션 하고 싶다.
response.sendRedirect("quiz04.jsp");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
quiz03
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="user" class="com.bean.User" scope="request" />
<jsp:getProperty property="id" name="user"/> 님의 회원 가입을 축하합니다. 비밀번호는
<jsp:getProperty property="pw" name="user"/>입니다.
</body>
</html>
quiz04
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1>회원가입에 실패했습니다.</h1>
</body>
</html>
728x90
반응형
'개발 언어 > JSP' 카테고리의 다른 글
JSP mini project : mypage 실습 (0) | 2024.04.15 |
---|---|
6-1 JSP-JDBC 연동2 (0) | 2024.04.12 |
6. JSP-JDBC 연동 (0) | 2024.04.11 |
4. application, param, include, forward, action tag, error page (0) | 2024.04.09 |
3. Cookie, Session (0) | 2024.04.08 |