본문 바로가기
Java Web Programming/4. JSP

[JSP] Model 1 Architecture (설계방식)

by 파프리카_ 2020. 8. 27.
728x90
반응형

 [ Model 1 Architecture (설계방식) ]

: jsp 페이지를 Web application의 control과 View를 핸들링하기 위해 사용한다.

Model 1 설계방식 테스트 ( jsp <--> javabeans <--> db)

 

jsp

/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Model Architecture</title>
</head>
<body>
Model 1 설계방식 테스트 ( jsp <--> javabeans <--> db)
<ul>
	<li><a href="AllMemberCount.jsp">총 회원수 조회</a></li>
	<li><a href="findMemberById-form.jsp">회원 정보 조회</a></li>
</ul>

<hr>
<img src = "picture/model1.PNG">
</body>
</html>

 

/AllMemberCount.jsp

MemberDAO 객체에 있는 getAllMemberCount method 호출 - 총 회원수를 조회하는 메서드

<%@page import="model.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>총 회원수</title>
</head>
<body>
<%
	MemberDAO dao = new MemberDAO();
	
%>
총회원수 : <%=dao.getAllMemberCount() %>명
</body>
</html>

 

 

javabeans

/MemberDAO.java

package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MemberDAO {
	private String driver = "oracle.jdbc.OracleDriver";
	private String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	private String userName = "scott";
	private String userPassword = "tiger";
	
	//Driver Loading
	public MemberDAO() throws ClassNotFoundException {
		Class.forName(driver);
	}
	
	// getAllMemberCount() method
	public int getAllMemberCount() throws SQLException {
		int count = 0;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			con = DriverManager.getConnection(url, userName, userPassword);
			String sql= "SELECT COUNT(*) FROM web_member";
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs, pstmt, con);
		}
		
		return count;
	}
	
	//findMemberById method
	public MemberVO findMemberById(String id) throws SQLException {
		MemberVO vo = new MemberVO();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			con = DriverManager.getConnection(url, userName, userPassword);
			String sql = "SELECT id, name, address FROM web_member "
						+"WHERE id=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				String name = rs.getString("name");
				String address = rs.getString("address");
				vo = new MemberVO(id, name, address);
			}
			
		} finally {
			closeAll(rs, pstmt, con);
		}
		return vo;
	}
	
	
	//close
	public void closeAll(ResultSet rs, PreparedStatement pstmt,
			Connection con) throws SQLException {
		if (rs!=null)
			rs.close();
		if (pstmt!=null)
			pstmt.close();
		if (con!=null)
			con.close();
	}
}

 

/MemberVO.java

package model;

public class MemberVO {
	private String id;
	private String password;
	private String name;
	private String address;

	public MemberVO() {
		super();
	}

	public MemberVO(String id, String name, String address) {
		super();
		this.id = id;
		this.name = name;
		this.address = address;
	}
	
	public MemberVO(String id, String password, String name, String address) {
		super();
		this.id = id;
		this.password = password;
		this.name = name;
		this.address = address;
	}

	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;
	}

}

 

브라우저 화면

index.jsp

 

AllMemberCount.jsp


예제 2

[요구사항]

전송한 아이디에 해당하는 회원이 -> 있으면아이디 : java이름 : [  ]주소 : [  ]-> 없으면alert()으로 model2 아이디에 해당하는 회원이 없습니다 ! findMemberById-form.jsp로 다시 이동시킨다.============================================

[ 개발 단계 ]

step1. MemberDAO에 findMemberById(String id) : MemberVO 구현

step2. findMemberById-action.jsp 구현

 

 

jsp

/findMemberById-form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>findMemberById Form</title>
</head>
<body>
<form method="POST" action="findMemberById-action.jsp">
	회원아이디 <input type="text" name="memberId" required="required">
	<input type="submit" value="검색">
</form>

</body>
</html>

 

/findMemberById-action.jsp

<%@page import="model.MemberVO"%>
<%@page import="model.MemberDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>회원 정보 결과</title>
</head>
<body>
	<%  //POST 방식이니까 한글 설정
		response.setCharacterEncoding("utf-8");
		
		//DAO 와 VO 객체 가져오기
		MemberDAO dao = new MemberDAO();
		MemberVO vo = new MemberVO();
		
		//browser에서 전송된 id
		String id = request.getParameter("memberId"); 
		
		//id를 통해 vo 객체 만들어왔다
		vo = dao.findMemberById(id);
		
		if (vo.getId()!=null) {
	%>
			아이디 : <%=id %><br>
			이름 :  <%=vo.getName() %><br>
			주소 :  <%=vo.getAddress() %>
	<% } else { %>
		<script type="text/javascript">
			alert("<%=id%>에 해당하는 회원이 없습니다!");
			location.href = "findMemberById-form.jsp";
		</script>
	<% } %>
</body>
</html>

 

javabeans

위에 기재된 MemberDAO와 MemberVO를 둘 다 사용한다

/MemberVO.java

package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MemberDAO {
	private String driver = "oracle.jdbc.OracleDriver";
	private String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	private String userName = "scott";
	private String userPassword = "tiger";
	
	//Driver Loading
	public MemberDAO() throws ClassNotFoundException {
		Class.forName(driver);
	}
	
	// getAllMemberCount() method
	public int getAllMemberCount() throws SQLException {
		int count = 0;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			con = DriverManager.getConnection(url, userName, userPassword);
			String sql= "SELECT COUNT(*) FROM web_member";
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				count = rs.getInt(1);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeAll(rs, pstmt, con);
		}
		
		return count;
	}
	
	//close
	public void closeAll(ResultSet rs, PreparedStatement pstmt,
			Connection con) throws SQLException {
		if (rs!=null)
			rs.close();
		if (pstmt!=null)
			pstmt.close();
		if (con!=null)
			con.close();
	}
}

 

브라우저 화면

--> index.jsp

--> 회원 정보 조회 클릭

 

--> 테이블에 있는 직원 아이디를 검색했을 때,

 

--> 테이블에 없는 직원 아이디를 검색했을 때,

728x90
반응형