1. index.jsp
<body>
<%
response.sendRedirect("/loginForm.jsp");
%>
</body>
2. loginForm.jsp
<%
String result = (String)request.getAttribute("result");
if(result == null){ //처음접속
//아무작업안함
}else{ //로그인실패
if(result.equals("1")){//패스워드틀림
%>
<script>
alert("패스워드가 틀립니다.");
</script>
<%
}else{
%>
<script>
alert("아이디가 틀립니다.");
</script>
<%
}
}
%>
<form action="/login.jsp" method="post">
<table border="1px">
<tr>
<td>ID</td>
<td><input type="text" name="userId"/></td>
</tr>
<tr>
<td>PW</td>
<td><input type="password" name="userPw"/></td>
</tr>
<tr>
<td><input type="submit" value="로그인"/></td>
</tr>
</table>
</form>
3. login.jsp
<%
//id와pw를 가져옴
String userId = request.getParameter("userId");
String userPw = request.getParameter("userPw");
System.out.println(userId+userPw);
String serverUrl = "jdbc:oracle:thin:@localhost:1521:XE";
String serverId = "scott";
String serverPw = "tiger";
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection conn = DriverManager.getConnection(serverUrl, serverId, serverPw);
System.out.println("OK");
String sql = "SELECT PW FROM MEMBER WHERE ID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, userId);
//결과값을 받아주는애
ResultSet rs = pstmt.executeQuery();
String result = "";
if(rs.next()){
if(userPw.equals(rs.getString("PW"))){
result = "success";
}else{
result = "1"; //패스워드틀림
}
}else{
result = "2"; //아이디존재안함
}
request.setAttribute("result", result);
if(result.equals("success")){//로그인성공
//redirect list.jsp
request.getRequestDispatcher("/list.jsp").forward(request, response);
}else{ // 로그인 실패
// redirect loginForm.jsp로 보냄
request.getRequestDispatcher("/loginForm.jsp").forward(request, response);
}
%>
4. list.jsp
'강의노트 > 웹' 카테고리의 다른 글
JSTL(JSP Standard Tag Library) (0) | 2013.07.09 |
---|---|
DTO와 DAO 패턴 (0) | 2013.06.14 |
JQry 이벤트 (0) | 2013.05.13 |
JQry 문서객체조작 (0) | 2013.05.13 |
JQry 문서객체탐색 (0) | 2013.05.07 |