[JSP] 회원가입 / 회원인증 ③ - 회원가입 : 주소 검색


pack_Member/ZipCodeBean.java


package pack_Member;

public class ZipCodeBean {

	private String zipCode;	// 우편번호
	private String area1;	// 도시, 도
	private String area2;	// 구, 소도시
	private String area3;	// 동, 면, 리
	private String area4;	// 번지, 호
		
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	public String getArea1() {
		return area1;
	}
	public void setArea1(String area1) {
		this.area1 = area1;
	}
	public String getArea2() {
		return area2;
	}
	public void setArea2(String area2) {
		this.area2 = area2;
	}
	public String getArea3() {
		return area3;
	}
	public void setArea3(String area3) {
		this.area3 = area3;
	}
	public String getArea4() {
		return area4;
	}
	public void setArea4(String area4) {
		this.area4 = area4;
	}
}



pack_Member/MemberMgr.java


package pack_Member;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

public class MemberMgr {

	private DBConnectionMgr pool;
	
	Connection 			conn 	= null;
	PreparedStatement 	pstmt 	= null;
	ResultSet 			rs 		= null;
	String 				sql 	= null;

	public MemberMgr() {
		try {
			pool = DBConnectionMgr.getInstance();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	
    // 우편번호 검색
	public Vector<ZipCodeBean> zipCodeRead(String area3){
		
		Vector<ZipCodeBean> vlist = new Vector<ZipCodeBean>();
		
		try {
			conn = pool.getConnection();
			sql = "select * from tblZipcode where area3 like ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, "%" + area3 + "%");
			rs = pstmt.executeQuery();
			
			if (rs != null) {
				while (rs.next()) { 
					ZipCodeBean zipBean = new ZipCodeBean();
					zipBean.setZipCode(rs.getString("zipCode"));
					/* = zipBean.setuZip(rs.getString(1)); */
					zipBean.setArea1(rs.getString("area1"));
					/* =  zipBean.setArea1(rs.getString(2)); */		
					zipBean.setArea2(rs.getString("area2"));
					zipBean.setArea3(rs.getString("area3"));
					zipBean.setArea4(rs.getString("area4"));
					
					vlist.add(zipBean);
				}				
			}
			
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			pool.freeConnection(conn, pstmt, rs);
		}
		return vlist;
	}
}



member/ZipCheck.jsp


<%@page import="java.util.Vector"%>
<%@page import="pack_Member.ZipCodeBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="memberMgr" class="pack_Member.MemberMgr" scope="page" />
<%
	request.setCharacterEncoding("UTF-8");

	String zipChk = request.getParameter("zipChk");
	String area3param = null;
	Vector<ZipCodeBean> vlist = null;
	
	if (zipChk.equals("n")){
		area3param = request.getParameter("area3");
		vlist = memberMgr.zipCodeRead(area3param);
	}
%>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>우편번호 찾기</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
    	$(function(){
    		
    		// 우편번호 찾기
    		function dongChk() {
    			
    			let area3Val = $("#area3").val();
    			if(area3Val == ""){
    				alert("동 이름을 입력해주세요.");
    				$("#area3").focus();
    				return;
    			}

    			$("#zipFrm").submit();

    		}
    		
    		$("#findAreaBtn").click(function(){
    			dongChk();
    		})	
    		
    	})
    		
   		function sendAddr(zipCode, area){
   			opener.regFrm.uZip.value = zipCode;
   			opener.regFrm.uAddr1.value = area;
   			opener.regFrm.uAddr2.focus();
   			window.close();
   		};
    </script>
</head>
<body>
	<div id="wrap">
		<form name="zipFrm" id="zipFrm">
			<table>
				<caption>검색 결과</caption>
				<tbody>
					<tr>
						<td>
							동 이름 입력 :
							<input type="text" name="area3" id="area3" maxlength="10">
							<button type="button" id="findAreaBtn">검색</button>
						</td>
						
					</tr>
					
					<!-- 검색결과 시작 -->
				<%
					if(zipChk.equals("n")){
						if(vlist.isEmpty()){
				%>
					<tr>
						<td>검색된 결과가 없습니다.</td>
					</tr>
				<%			
						} else {
				%>
					<tr>
						<td>
							* 검색 후 아래 우편번호를 클릭하면 자동으로 입력됩니다.
						</td>
					</tr>
				<%
						for (int i = 0; i < vlist.size(); i++) {
							ZipCodeBean zipBean = vlist.get(i);
							/* 제네릭을 적으면 자동 형변환 */
							String zipCode = zipBean.getZipCode();
							String area1 = zipBean.getArea1();
							String area2 = zipBean.getArea2();
							String area3 = zipBean.getArea3();
							String area4 = zipBean.getArea4();
							
							String area = area1 + " " + area2 + " " + area3 + " " + area4;
				%>
					<tr>
						<td onclick="sendAddr('<%=zipCode %>', '<%=area %>')">
							<%=zipCode + " " + area %>
						</td>
					</tr>
				<%
							} 
						} 
					} 
				%>
					<!-- 검색결과 끝 -->
					
					<tr>
						<td>
							<button type="button" onclick="self.close()">닫기</button>
						</td>
					</tr>
				</tbody>
			</table>
			<input type="hidden" name="zipChk" value="n">
		</form>
	</div>
</body>
</html>


Categories:

JSP/Servlet