반응형
여승철
INTP 개발자
여승철
  • 분류 전체보기 (376)
    • CS (16)
      • 면접 준비 (7)
      • 운영체제 (0)
      • 네트워크 (2)
      • HTTP (6)
      • 스프링(Spring) IoC 컨테이너 (0)
      • 알고리즘 (1)
    • Web (13)
    • AWS (6)
    • Java (43)
    • JSP & Servlet (65)
      • 개념 (42)
      • 실습 (23)
    • Spring Framework (33)
    • Spring Boot (10)
    • Spring Data (22)
      • JPA (14)
      • Query DSL (7)
      • Redis (1)
    • Spring Security (9)
    • Spring Batch (4)
    • MyBatis (10)
    • Front-End (51)
      • JS (27)
      • Vue.js (17)
      • React (5)
      • JQuery (0)
      • d3.js (2)
    • DBMS (24)
      • SQL, RDBMS (16)
      • MongoDB (5)
      • Redis (3)
    • Kafka (3)
    • 리눅스 (Linux) (4)
    • 디자인 패턴 (3)
    • VCS (8)
    • API (0)
    • TOOL (3)
    • Reading Book (28)
      • 이펙티브 자바 (11)
      • Clean Code (10)
      • 1분 설명력 (4)
      • HOW TO 맥킨지 문제해결의 기술 (3)
    • C# (4)
    • NSIS (6)
    • ETC (11)

블로그 메뉴

  • 홈
  • 태그

인기 글

태그

  • mybatis
  • 맥킨지
  • JSTL
  • 회원 관리
  • servlet
  • 이펙티브 자바
  • Dao
  • JDBC
  • ubuntu
  • 로그인
  • 게시판
  • querydsl
  • Spring Batch
  • controller
  • HTTP
  • 환경 세팅
  • 스트림
  • EC2
  • jsp
  • 디자인 패턴

최근 댓글

최근 글

hELLO· Designed By 정상우.
여승철

INTP 개발자

JSP & Servlet/실습

[JSP & Servlet] 로그인 (feat. 세션 & 쿠키) - View / DAO

2022. 6. 30. 21:51
반응형

 

📌index.jsp

사용자가 처음으로 접하는 페이지

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>로그인(feat.세션 & 쿠키)</title>
</head>
<body>
<jsp:forward page="/login"></jsp:forward>
</body>
</html>

index.jsp 페이지를 실행하면 "/login" 서블릿을 매핑하는 서블릿으로 포워딩 한다.

통상적으로 index.jsp를 사용자가 맨 처음 실행했을때 접하는 페이지로 많이 사용한다.

 

 

 

 

📌로그인 폼

loginForm.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
	#loginFormArea{
		margin : auto;
		width : 400px;
		height : 200px;
		border : 2px double rgb(243, 185, 244); 
		border-radius : 10px;
		text-align: center;
	}
	fieldset{
		text-align: center;
		border : none;
	}
	#selectButton{
		margin-top : 10px;
	}
	table{
	    width : 380px;
		margin : auto;
	}
	.td_left{
		width : 180px
	}
	.td_right{
		width : 200px
	}
</style>
</head>
<body>
    <section  id = "loginFormArea">
	<h1>로그인</h1>
	<form action="login" method = "POST">
		<fieldset>
			<table>
				<tr>
					<td class = "td_left">
					<label for = "id">아이디 : </label>
					</td>
					<td class = "td_right">
					<input type = "text" name = "id" id = "id"/>
					</td>
				</tr>
				<tr>
					<td class = "td_left">
					<label for = "passwd">비밀번호 : </label>
					</td>
					<td class = "td_right">
					<input type = "password" name = "passwd" id = "passwd"/>
					</td>
				</tr>
				<tr>
					<td class = "td_left">
					<label for = "useCookie">자동 로그인: </label>
					</td>
					<td class = "td_right">
					<input type = "checkbox" name = "useCookie" id = "useCookie"/>
					</td>
				</tr>
			</table>
			
			<input type = "submit" value = "로그인" id = "selectButton"/>
		</fieldset>
	</form>
	</section>
</body>
</html>

 

 

 

 

📌로그인 성공/실패

loginSuccess.jsp

<%@page import="vo.Member"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	Member loginMember = (Member)request.getAttribute("loginMember");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>로그인 성공</title>
</head>
<body>
<h1>사용자 정보</h1>
이름 : <%=loginMember.getName() %><br>
나이 : <%=loginMember.getAge() %><br>
주소 : <%=loginMember.getAddr() %><br>
이메일 : <%=loginMember.getEmail() %><br>
</body>
</html>

 

loginFail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>로그인 실패</title>
</head>
<body>
<h1>로그인실패</h1>
</body>
</html>

 

 

 

 

📌DB작업 시 반복적 수행을 편하게 해주기 위해 만든 파일

JdbcUtil.java

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

//DB 작업을 할 때 반복적으로 수행해야 하는 작업을 정의하는 클래스
public class JdbcUtil {
	
	public static Connection getConnection(){
		Connection con = null;
		
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/testDB";
		
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url,"아이디","비밀번호");
			con.setAutoCommit(false);
			
			System.out.println("connect succes");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	
	public static void close(Connection con){
		try {
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void close(Statement stmt){
		try {
			stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void close(ResultSet rs){
		try {
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void commit(Connection con){
		try {
			con.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void rollback(Connection con){
		try {
			con.rollback();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

 

 

 

📌DB에 SQL 쿼리 전송하는 파일

LoginDAO.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import vo.Member;
import static db.JdbcUtil.*;

public class LoginDAO {
	
	private static LoginDAO loginDAO;
	private Connection con;
	
	private LoginDAO() {
		// TODO Auto-generated constructor stub
	}
	
	public static LoginDAO getInstance(){
		if(loginDAO == null){
			loginDAO = new LoginDAO();
		}
		return loginDAO;
	}
	
	public void setConnection(Connection con){
		this.con = con;
	}
	
	public Member selectLoginMember(String id, String passwd) {
		// TODO Auto-generated method stub
		Member loginMember = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = con.prepareStatement("SELECT * FROM users WHERE id = ? AND passwd = ?");
			pstmt.setString(1, id);
			pstmt.setString(2, passwd);
			rs = pstmt.executeQuery();
			if(rs.next()){
				loginMember = new Member();
				loginMember.setAddr(rs.getString("addr"));
				loginMember.setAge(rs.getInt("age"));
				loginMember.setEmail(rs.getString("email"));
				loginMember.setGender(rs.getString("gender"));
				loginMember.setId(rs.getString("id"));
				loginMember.setName(rs.getString("name"));
				loginMember.setNation(rs.getString("nation"));
				loginMember.setPasswd(rs.getString("passwd"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			try {
				close(rs);
				close(pstmt);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return loginMember;
	}
	
}

 

 

반응형
저작자표시 (새창열림)

'JSP & Servlet > 실습' 카테고리의 다른 글

[JSP & Servlet] 쇼핑몰 (오늘 본 상품 & 장바구니) - 테이블 생성 및 vo(Value Object) + 기본 세팅  (0) 2022.07.01
[JSP & Servlet] 로그인 (feat. 세션 & 쿠키) - controller / service  (0) 2022.06.30
[JSP & Servlet] 로그인 (feat. 세션 & 쿠키) - 테이블 생성 및 vo(Value Object)  (0) 2022.06.30
[JSP & Servlet] 게시판 만들기 (feat. MySQL) - View  (0) 2022.06.29
[JSP & Servlet] 게시판 만들기 (feat. MySQL) - DAO(Data Access Object)  (0) 2022.06.27
    여승철
    여승철

    티스토리툴바