본문으로 바로가기

Model1 간략한 로그인 기능 만들기 - 로그인

category JAVA 2019. 5. 28. 21:55
반응형

Model 1 (기초)간략 로그인 기능 만들기..    

기본적으로 오라클 DB와 연동해서 진행을 하였으며,   

DB 테이블 구성은 아이디, 비밀번호, 이름, 번호 입니다.   

JDK 1.8 버전   
Ojdbc7 버전  

 

login.jsp

1. 로그인 정보 입력폼(아이디, 비밀번호)

2. loginPro.jsp 로 정보 전송

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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <% String id = (String)session.getAttribute("id"); %>
 
 
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="loginPro.jsp">
<table border="1">
<tr>
<td>아이디<input type="text" name="id"></td>
</tr>
<tr>
<td>비밀번호<input type="text" name="pw"></td>
<td><input type="submit"></td>
</tr>
 
 
</table>
</form>
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
h

 

Loginpro.jsp

1. login.jsp에서 받아온 파라미터를 각 변수에 할당

2. userdao 의 logincheck 메소드를 이용하여 로그인

3. 로그인 id 를 세션에 저장

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
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="myproject.user.bean.UserDBBean" %>
<%@ page import="myproject.user.bean.UserDBDAO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<%
    if(session.getAttribute("id"== null){
        System.out.println("세션만료");
    }
%>
<%
        request.setCharacterEncoding("euc-kr");
        String id = request.getParameter("id");
        String pw = request.getParameter("pw");
        
        
        UserDBDAO userdao = UserDBDAO.getInstance();
        
        boolean check = userdao.LoginCheck(id, pw);
            out.println(check);
        if(check){
            session.setAttribute("id", id);    
            out.println("로그인성공");
            response.sendRedirect("main.jsp");
            
        }
        
%>
<body>
 
</body>
</html>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

DAO LoginCheck 메소드

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
public  boolean LoginCheck(String id , String pwd) {
            
            boolean check = false;
                try {
                    Connection con = null;
                    PreparedStatement pstmt = null;
                    ResultSet rs = null;
                    
                    String DriverName = "oracle.jdbc.driver.OracleDriver";
                    String url = "jdbc:oracle:thin:@localhost:1521:XE";
                    String user = "dbuser";
                    String pw = "dbpass";
                    
                    Class.forName(DriverName);
                    
                    con = DriverManager.getConnection(url, user, pw);
                    System.out.println("로그인체크연결성공");
                    
                    String sql = "select * from tbl_user where id = ? and pw = ?";
                    pstmt = con.prepareStatement(sql);
                    pstmt.setString(1,id);
                    pstmt.setString(2, pwd);
                    rs = pstmt.executeQuery();
                    check = rs.next();
                    
                    
                    
                }catch(Exception e) {
                    System.out.println(e.getMessage());
                }
            
            
            
            return check;
            
            
        }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 
반응형