: software system์ 3๊ฐ์ง type์ component๋ก ๋ถํ ํ๋ ์ํํธ์จ์ด ํจํด์ด๋ค.
CREATE DATABASE bookstore;
USE bookstore;
CREATE TABLE members (
id VARCHAR(30) NOT NULL PRIMARY KEY,
pw VARCHAR(30) NOT NULL,
name VARCHAR(30) NOT NULL
);
INSERT INTO members VALUES('hong', 'test1234', 'ํ๊ธธ๋');
INSERT INTO members VALUES('kim', 'test1234', '๊น์ฐ์');
์ฌ์ฉ์ ๊ด๋ฆฌ ์ฝ๋๋ฅผ ๋ฃ๊ธฐ ์ํด webapp/member ํด๋ ์์ฑ
์ฝ๋
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>๋ก๊ทธ์ธํ์ธ์!</div><br><br>
<form action="/book/login" method="POST">
ID : <input type="text" name="userID"><br><br>
PW : <input type="password" name="userPW"><br><br>
<input type="submit" value="๋ก๊ทธ์ธ">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
๋ก๊ทธ์ธ์ ์คํจํ์ต๋๋ค!
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
import="member.vo.Member"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% Member member = (Member) request.getAttribute("member"); %>
<%= member.getName() %>๋ ํ์ํฉ๋๋ค.
</body>
</html>
package member.vo;
public class Member {
private String id;
private String pw;
private String name;
public Member() {
}
public Member(String id, String pw, String name) {
super();
this.id = id;
this.pw = pw;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package member.dao;
import member.vo.Member;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class MemberDAO {
private DataSource ds;
public MemberDAO() throws Exception {
Context init = new InitialContext();
ds = (DataSource)init.lookup("java:comp/env/jdbc/MySQLDB");
}
public Member select(Member member) throws Exception {
Connection con = ds.getConnection();
System.out.println("connection ํ๋ ์ฑ๊ณต!");
String sql = "SELECT * FROM members WHERE id = ? AND pw = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, member.getId());
pstmt.setString(2, member.getPw());
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
// ๋ก๊ทธ์ธ์ด ๋๋ค๋ ์๋ฏธ
member.setName(rs.getString("name"));
return member;
}
return null;
}
}
package member.service;
import member.dao.MemberDAO;
import member.vo.Member;
public class MemberService {
// login ์ฒ๋ฆฌํ๋ business method
public Member login(Member member) {
// ๋ก๊ทธ์ธ์ด ๋๋์ง ํ์ธํ๋ ๋ก์ง ์ฒ๋ฆฌ
// Database ์ฒ๋ฆฌ
// Database ์ฒ๋ฆฌ๋ฅผ ์ํด DAO๊ฐ ์์ด์ผ ํ๋ค.(class๋ ์กด์ฌํด์ผ ํ๋ค.)
try {
MemberDAO dao = new MemberDAO();
member = dao.select(member);
} catch (Exception e) {
System.out.println(e);
}
return member;
}
}
package member.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import member.service.MemberService;
import member.vo.Member;
/**
* Servlet implementation class LoginController
*/
@WebServlet("/login")
public class LoginController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. ์
๋ ฅ๋ฐ๊ณ
String id = request.getParameter("userID");
String pw = request.getParameter("userPW");
// 2. ๋ก์ง ์ฒ๋ฆฌ
// ๋ก์ง ์ฒ๋ฆฌ ํ๊ธฐ ์ํด์ service ๊ฐ์ฒด๋ฅผ ์์ฑ(service class๊ฐ ํ์)
// ํด๋ผ์ด์ธํธ์์ ์
๋ ฅ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ service์ ์ ๋ฌํ๊ธฐ ์ํด VO๊ฐ ํ์ํ๋ค
// VO๋ฅผ ๋ง๋ค๊ธฐ ์ํด class๊ฐ ์์ด์ผ ํ๋ค.
Member member = new Member();
member.setId(id);
member.setPw(pw);
MemberService service = new MemberService();
member = service.login(member);
// 3. ์ถ๋ ฅ
// ๋ก๊ทธ์ธ์ ์ฑ๊ณตํ๋ฉด member vo ๊ฐ์ฒด ์์ ๋ก๊ทธ์ธํ ์ฌ๋์ ์ด๋ฆ์ด ๋ค์ด๊ฐ ์๋ค.
if(member != null) {
// ๋ก๊ทธ์ธ์ ์ฑ๊ณตํ๋ฉด JSP๋ฅผ ์ด์ฉํด์ ์ถ๋ ฅํด์ผ ํ๋ค.
// member vo ๊ฐ์ฒด๋ฅผ JSP์๊ฒ ์ ๋ฌํด์ผ ํ๋ค.
// ์ฆ, controller servlet์ด request๋ฅผ ์ ๋ฌํด์ ๋ค๋ฅธ ์๋ธ๋ฆฟ(JSP)์ ํธ์ถ
RequestDispatcher rd = request.getRequestDispatcher("/member/loginSuccess.jsp");
request.setAttribute("member", member);
rd.forward(request, response);
} else {
response.sendRedirect("/book/member/loginFail.html");
}
}
}