java

JSP - ๊ฒŒ์‹œํŒ๋งŒ๋“ค๊ธฐ(8) ํšŒ์›๊ฐ€์ž…

dev_summer 2020. 11. 9. 15:40

 

 

 

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;
	}

- ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด(๋™๊ธฐํ™”์™€ ๊ด€๋ จ๋œ ํŒจํ„ด)