DoR@Hee의 끄적끄적

JSP를 이용해서 회원로그인 만들기 본문

개발/WEB개발

JSP를 이용해서 회원로그인 만들기

DoR@Hee 2019. 1. 31. 01:42

View - login.jsp 

controller에서 생성된 값 받아와서 뿌려주는 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<% if(session.getAttribute("ValidMem") !null) { %>
    <jsp:forward page="main.jsp"></jsp:forward>
<% } %>
    
<html>
<head>
    <script charset="utf-8" type="text/javascript" src="Check.js"></script>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <link href="https://fonts.googleapis.com/css?family=Gamja+Flower&amp;subset=korean" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Mukta" rel="stylesheet">
</head>
<body>
    <form action="loginOk.jsp" method="post" name="logChk">
        <div id="loginbox">
            <p id="sign_in">Sign in</p>
            <label >아이디</label>
            <input type="text" name="uId" placeholder="ID" autofocus>
            <label >비밀번호</label>
            <input type="password" name="uPassword" placeholder="Password">
            <button type="button" id="login_btn" name="login_btn" onclick="loginChk();">로그인</button>
            <p id="sign_up">Don't have an account? <a href="register.html">sign up</a></p>
        </div>        
    </form>
</body>
</html>
 

cs


Controller 
login_ok.jsp db접근 및 세션 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@page import="com.javalec.ex.dao.*"%>
<%@page import="com.javalec.ex.dto.*"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    
 <%
     request.setCharacterEncoding("EUC-KR");
 
     String uId = request.getParameter("uId");
     String uPassword = request.getParameter("uPassword");
     
     UDao dao = UDao.getInstance();
     
     int checkNum = dao.userChk(uId,uPassword);
     if(checkNum == 1) { 
 
%>         
     <script>
         alert("아이디 또는 비밀번호가 올바르지 않습니다.");
         history.go(-1);
     </script>
<%
    } else if(checkNum == 0){
        session.setAttribute("uId",uId);
        session.setAttribute("ValidMem","yes");
        response.sendRedirect("main2.jsp");
        } %>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
 
</body>
</html>
cs

UDao.java 
db접근하는 model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    public int userChk(String uId, String uPassword){ // 입력받은 user 의 id와 pw를 db의 id와 pw 비교
        int ri = 0;
        
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet res = null;
        
        try {
            String query = "select * from members where id =? and pw = ?";
            con = DriverManager.getConnection(url, user, password);
            pstmt=con.prepareStatement(query);
            pstmt.setString(1, uId);
            pstmt.setString(2, uPassword);
            res = pstmt.executeQuery();
            
            if(res.next()) {
                ri = 0;
            } else { 
                ri = 1// 아이디 존재 x -1
            }            
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(con != null) con.close();
                if(res != null) res.close();
                if(pstmt != null) pstmt.close();
            }catch(Exception e2) {
                e2.printStackTrace();
            }
        }
        return ri;
    }
cs

완성 사이트


'개발 > WEB개발' 카테고리의 다른 글

JSP 로그아웃 만들기  (0) 2019.01.31
JSP 글 보기 만들기  (0) 2019.01.31
JSP 글 리스트 만들기  (0) 2019.01.31
JSP 게시판 글 쓰기 만들기  (0) 2019.01.31
JSP 회원가입 만들기  (0) 2019.01.31
Comments