1. ์ฃผ์ํ ์
(1) ๋ชจ๋ ์๋ต์ requestDispather๋ก ํ๋ค.
์ด์ ๋ ์ฃผ์๋ฅผ ๋จ๊ธฐ์ง ์๊ณ ๋ฐ์ดํฐ๋ฅผ ๋ค๊ณ ์ด๋ํ ์ ์๊ธฐ ๋๋ฌธ
(2) ์คํจ์์๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ์๋ตํ๋ค.history.back()
(3) DB์ User table์ username ์ ์ฝ์กฐ๊ฑด์ uniq ๊ฑธ์ด์ผํจ
alter table user add unique(username);
2. include/nav.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-md bg-dark navbar-dark">
<!-- Server/web.xml์ Welcome File List๊ฐ index.jsp๋ก ์ ์ ๋์ด ์๋ค. -->
<a class="navbar-brand" href="<%=request.getContextPath()%>/">seoha</a>
<button class="navbar-toggler" type="button" data-toggle="collapse"
data-target="#collapsibleNavbar">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="collapsibleNavbar">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link"
href="/board?cmd=write">Post</a></li>
<li class="nav-item"><a class="nav-link" href="/user?cmd=join">join</a>
</li>
<li class="nav-item"><a class="nav-link" href="/user?cmd=login">login</a>
</li>
</ul>
</div>
</nav>
<br>
3. include/footer.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<br />
<div class="jumbotron text-center" style="margin-bottom: 0">
<p>made in seoha:-)</p>
</div>
</body>
</html>
4. index/.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/include/nav.jsp"%>
<%@ include file="/include/footer.jsp"%>
5. dao/USerDao.java
package com.cos.board.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.cos.board.db.DButil;
import com.cos.board.model.User;
// User Test
// DAO -> data Access object : ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ํด์
public class UserDao {
private UserDao() {}
private static UserDao instance = new UserDao(); // ์ง์
ํ๋ ์๊ฐ ์์ฑ ๋๋ค.
public static UserDao getinstance(){ // ์ฑ๊ธํค ํจํด
return instance;
}
public int save(String username, String password, String email) {
// 1. Stream ์ฐ๊ฒฐ
Connection conn = DButil.getConnection();
PreparedStatement pstmt = null;
try {
// 2. ์ฟผ๋ฆฌ ๋ฉ์์ง ์ ์ก ํด๋์ค(๊ท์ฝ์ ๋ง๊ฒ), sql์ ์ฟผ๋ฆฌ
final String SQL = "INSERT INTO user(username, password, email, createTime) VALUES(?,?,?,now())";
pstmt = conn.prepareStatement(SQL);
// 3. SQL๋ฌธ ์์ฑํ๊ธฐ
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.setString(3, email);
// 4. SQL๋ฌธ ์ ์ก write,read์ ์ ์ก ๋ฐฉ๋ฒ์ด ๋ค๋ฅด๋ค. ํ๋กํ ์ฝ์ด 2๊ฐ ํ์. executeQuery๋ ๊ฒฐ๊ณผ๋ฅผ ์๋ ค๋ฌ๋ผ๋ ๋ฌธ,
// executeUpdate๋ DB์ ๊ธฐ๋กํ๊ณ return์ด ํ์์๋ ๋ฌธ
// pstmt.executeQuery();
int result = pstmt.executeUpdate();// PSMT๊ฐ ๋ซํ๋ DB์ ์๋์ผ๋ก ์ปค๋ฐ ํด์ค.
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
// conn.commit(); ์๋๋ ์๋์ผ๋ก ์ปค๋ฐ๋๋ ๋๊ฐ์ง์ ์ผ์ด ํ๊ฐ์ ํธ๋์ญ์
์ผ๋๋ ๋ฐ๋ก ์ปค๋ฐํด์ผํจ
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return -1;
}
}
๊ธธ์ด์ update, delete๋ฑ์ ๋บ ๊น๊น
6.action/UserJoinAction.java
package com.cos.board.action.user;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cos.board.action.Action;
public class UserJoinAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. ํ์ด์ง ์ด๋
//resp.sendRedirect("/user/join.jsp"); // ์ฃผ์๊ฐ ๋จ๋ ๋จ์ ์ด ์๋ค.
RequestDispatcher dis = req.getRequestDispatcher("/user/join.jsp");
dis.forward(req, resp);
}
}
7. user/join.jsp
package com.cos.board.action.user;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cos.board.action.Action;
public class UserJoinAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. ํ์ด์ง ์ด๋
//resp.sendRedirect("/user/join.jsp"); // ์ฃผ์๊ฐ ๋จ๋ ๋จ์ ์ด ์๋ค.
RequestDispatcher dis = req.getRequestDispatcher("/user/join.jsp");
dis.forward(req, resp);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/include/nav.jsp"%>
<body>
<div class="container">
<div class="card">
<div class="card-header">join</div>
<div class="card-body">
<form action="/user?cmd=joinProc" method="POST">
<div class="form-group">
<label for="username">Username :</label> <input type="text"
class="form-control" placeholder="Enter username" name="username" required="required" maxlength="20">
</div>
<div class="form-group">
<label for="password">Password:</label> <input type="password"
class="form-control" placeholder="Enter password" name="password"
required="required" maxlength="12">
</div>
<div class="form-group">
<label for="email">Email address:</label> <input type="email"
class="form-control" placeholder="Enter email" name="email"
required="required" maxlength="30">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
<%@include file="/include/footer.jsp"%>
8. acion/UserJoinProAction.java
package com.cos.board.action.user;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cos.board.action.Action;
import com.cos.board.dao.UserDao;
import com.cos.board.util.Script;
public class UserjoinProcAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. ํ๋ผ๋ฉํฐ๋ฅผ ๋ฐ๊ธฐ
// 2. POST์์ฒญ์ด๊ธฐ๋๋ฌธ์ DB์ insert
// (1)DAO ์ฐ๊ฒฐ
// (2) ํจ์ ์คํ save(username, password,email);
// (3)return ๊ฐ์ด 1์ผ๋ index.jsp ์ด๋
// return ๊ฐ์ด 1์ด ์๋๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ๋ก history.back();
// 1๋ฒ
String username = req.getParameter("username");
String password = req.getParameter("password");
String email = req.getParameter("email");
System.out.println("username : "+username);
System.out.println("password : "+password);
System.out.println("email : "+email);
// 2-(1)๋ฒ
UserDao userDao = UserDao.getinstance();
// 2-(2)๋ฒ
int result = userDao.save(username,password,email);
// 2-(3)๋ฒ
if(result==1) {
RequestDispatcher dis = req.getRequestDispatcher("/");
dis.forward(req, resp);
}else {
Script.back(resp);
}
}
}
9. util/Script.java
package com.cos.board.util;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
public class Script {
public static void back(HttpServletResponse resp) throws IOException {
/* out์ response๋ฅผ ํตํด ๋ฐ์. */
PrintWriter out = resp.getWriter();
out.print("<script>");
out.print("alert('ํ์๊ฐ์
์คํจ');");
out.print("history.back();");
out.print("</script>");
out.flush();
}
}
private static UserDao instance = new UserDao(); // ์ง์
ํ๋ ์๊ฐ ์์ฑ ๋๋ค.
public static UserDao getinstance(){ // ์ฑ๊ธํค ํจํด
return instance;
}
- ์ฑ๊ธํค ํจํด(๋๊ธฐํ์ ๊ด๋ จ๋ ํจํด)
'java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํ๋ฆฌ๋ง ๋ถํธ ๊ธฐ๋ณธ๊ฐ๋ (0) | 2020.11.11 |
---|---|
JSP - ๊ฒ์ํ๋ง๋ค๊ธฐ(10) ๋ก๊ทธ์ธ -v2(Cookie,ELํํ์, JSTL ์ ์ฉ) (0) | 2020.11.09 |
JAVA - Thread์ดํดํ๊ธฐ (0) | 2020.11.09 |
JSP - ๊ฒ์ํ๋ง๋ค๊ธฐ(7) ์ค๋ฅ๋ฌธ์ ๋ฆฌ:-) (0) | 2020.11.09 |
JSP - ๊ฒ์ํ ๋ง๋ค๊ธฐ(6) PrepareStatement๋ฅผ ์ด์ฉํ CRUD ํ ์คํธ (0) | 2020.11.09 |