반응형
📌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 |