웹프로그래밍/jsp

JSP-ID 중복체크

zelkova 2016. 3. 16. 17:37

joinform.jsp

<script>
function idCheck() {
var form1 = document.joinform;
if (!form1.id.value){
alert("아이디를 적어주세요");
form1.id.focus();
return false;
}
var user_id = form1.id.value;
var url = "/member/idcheck.jsp?id=" + user_id;
window.open(url,"IdCheck","toolbar=no,location=no,status=no,menubar=no,scrollbar=no,resizable=no,width=300,height=180");
return;
}
</script>

아이디
<input type="text" name="id"/><input type="button" onClick="idCheck()" value="중복확인">

idcheck.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.SQLException" %>

<%@ page import="javax.sql.*,java.sql.*,javax.naming.*" %>


<%
request.setCharacterEncoding("EUC-KR");
String f_id=(String)request.getParameter("id");
int check=-1;
Connection myconn=null;
Statement st=null;
ResultSet rs=null;
try{

Context fcontext=new InitialContext();
DataSource ds=(DataSource)fcontext.lookup("java:/comp/env/jdbc/ylhome");
/* 이부분은 커넥션풀을 사용한 관계로 다를 수 있습니다.*/
myconn=ds.getConnection();

String q1="select id from member where id = '"+f_id+"'";
st=myconn.createStatement();
st.executeQuery(q1);
rs=st.executeQuery(q1);
if(rs.next())
{
check=1;
}
else
{
check=0;
}
}
finally{
if(rs !=null) try{rs.close();} catch(SQLException err){}
if(st !=null) try{st.close();} catch(SQLException err){}
if(myconn !=null) try{myconn.close();} catch(SQLException err){}
}

if(check==1)
{
%>
<b><font color="red"> <%=f_id%> </font>는 이미 사용중인 아이디입니다.</b>
<form name="checkForm" method="post" action="member.jsp">
<b>다른 아이디를 선택하세요. </b> <br/> <br/>
<input type="text" name="id" />
<input type="submit" value="ID중복확인"/>
</form>
<%
}
else
{
%><center>
<b>입력하신 <font color="red"><%=f_id%></font>는 <br/>
사용하실 수 있는 ID입니다.
<input type="button" value="닫기" onclick="setid()"></center>

<script language="javascript">
<%
}
%>
function setid()
{
opener.document.joinform.id.value="<%=f_id%>";
window.self.close();
}
</script>

참조! Opener 메서드는 띄워진 창의 부모창을 제어 할 수 있습니다.

이 opner 메서드는 해당 창이 띄워 졌을때,
이를 띄운 부모 페이지에 어떠한 작동을 하려 할때 사용 하게 됩니다.

<!DOCTYPE html>
<html>
<head>
<script>
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>이창을 띄운 페이지가 어떻게 변했나요?</p>");
myWindow.focus();
myWindow.opener.document.write("<p>버튼이 사라지고 제가 나옵니다.</p>");
}
</script>
</head>
<body>
<input type="button" value="눌러 주세요" onclick="openWin()" />
</body>
</html>

[출처] [JavaScript] Opener|작성자 바보상자


반응형

'웹프로그래밍 > jsp' 카테고리의 다른 글

DBCP 설정하기 JSP+MySql+Tomcat9.0  (0) 2016.03.11
JSP 구조 이해하기 - 스크립트요소  (0) 2015.12.27
기본구조 파해치기1 - 디렉티브  (0) 2015.12.27
JSP실행해보기  (0) 2015.12.27
JSP 데이터타입과 형변환  (0) 2015.08.04