# 게시판 구현
- dbConn.jsp
<%@page import="java.sql.PreparedStatement"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<%
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "1234";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch(Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
- index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="dbConn.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
</head>
<body>
<%
String id = request.getParameter("id");
if(id != null){
%>
}
<table>
<caption>회원게시판</caption>
<tr>
<td>번호</td>
<td>제목</td>
<td>작성자</td>
<td>작성일</td>
<td>조회수</td>
</tr>
<%
String sql = "select * from board";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
int no = 0, rCnt = 0;
String name = null, title = null, contents = null, wTime = null;
while(rs.next()){
no = rs.getInt("no");
name = rs.getString("name");
title = rs.getString("title");
contents = rs.getString("contents");
wTime = rs.getString("wTime");
rCnt = rs.getInt("rCnt");
%>
<tr>
<td><%= no %></td>
<td><a href="writeContents.jsp?no=<%= no %>"><%=title %></a></td>
<td><%= name %></td>
<td><%= wTime %></td>
<td><%= rCnt %></td>
</tr>
<%
}
%>
</table>
<a href="write.jsp"><input type="button" value="글쓰기"></a>
<%
}
%>
</body>
</html>
- write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="dbConn.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="writeResult.jsp">
<table>
<caption>글쓰기</caption>
<tr>
<td>제목</td>
<td><input type="text" name="tltle"></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pw"></td>
</tr>
<tr>
<td>e-mail</td>
<td><input type="email" name="email"></td>
</tr>
<tr>
<td colspan="2">
<textarea name="contents" rows="8" cols="40"></textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="등록">
<a href="index.jsp"><input type="button" value="목록"></a>
</td>
</tr>
</table>
</form>
</body>
</html>
- writeResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="dbConn.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String title = request.getParameter("title");
String name = request.getParameter("name");
String pw = request.getParameter("pw");
String email = request.getParameter("email");
String contents = request.getParameter("contents");
String sql = "insert into board(title, name, pw, email, contents) values(?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, name);
pstmt.setString(3, pw);
pstmt.setString(4, email);
pstmt.setString(5, contents);
int result = 0;
result = pstmt.executeUpdate();
if(result > 0){
response.sendRedirect("index.jsp");
} else{
response.sendRedirect("error.jsp");
}
%>
</body>
</html>
- writeContents.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="dbConn.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String no = request.getParameter("no");
String sql = "select * from board where = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, no);
rs = pstmt.executeQuery();
String sql2 = "update board set rCnt = rCnt+1 where no =?";
PreparedStatement pstmt2 = conn.prepareStatement(sql2);
pstmt2.setString(1, no);
pstmt2.executeUpdate();
if(rs.next()){
%>
<table>
<caption>게시물 내용보기</caption>
<tr>
<td>제목</td>
<td><input type="text" name="title" value="<%= rs.getString("title") %>" readonly></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="name" value="<%= rs.getString("name") %>" readonly></td>
</tr>
<tr>
<td>작성일</td>
<td><input type="text" name="wTime" value="<%= rs.getString("wTime") %>" readonly></td>
</tr>
<tr>
<td colspan="2">
<textarea rows="8" cols="40" readonly><%= rs.getString("contents") %></textarea>
</td>
</tr>
</table>
<%
}
%>
<a href = "index.jsp"><input type="button" value="목록"></a>
<a href = "updatePw.jsp?no=<%= no %>"><input type="button" value="수정"></a>
<a href = "delete.jsp?no=<%= no %>"><input type="button" value="삭제"></a>
</body>
</html>
- updatePw.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="dbConn.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String no = request.getParameter("no");
%>
<form method="post" action="update.jsp">
<input type="hidden" name="no" value="<%= no %>">
<table>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pw"></td>
</tr>
<tr>
<td><input type="submit" value="확인"></td>
</tr>
</table>
</form>
</body>
</html>
- update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="dbConn.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String no = request.getParameter("no");
String pw = request.getParameter("pw");
String sql = "select * from board where no = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, no);
rs = pstmt.executeQuery();
while(rs.next()){
if(!pw.equals(rs.getString("pw"))){
%>
<script>
alert("비밀번호가 다릅니다.");
</script>
<a href="index.jsp">목록</a>
<%
} else {
%>
<form method="post" action="updateResult.jsp"></form>
<input type="hidden" name="no" value="<%= no%>">
<table>
<caption>게시물 수정하기</caption>
<tr>
<td>제목</td>
<td><input type="text" name="title"
value="<%= rs.getString("title") %>" ></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="name"
value="<%= rs.getString("name") %>" ></td>
</tr>
<tr>
<td>작성일</td>
<td><input type="text" name="wTime"
value="<%= rs.getString("wTime") %>"></td>
</tr>
<tr>
<td colspan="2"><textarea rows="8" cols="40" name="contents"><%= rs.getString("contents") %></textarea>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="등록"> <a
href="index.jsp"><input type="button" value="목록"></a>
</tr>
</table>
</form>
<%
}
}
%>
</body>
</html>
- updateResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="dbConn.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String no = request.getParameter("no");
String title = request.getParameter("title");
String name = request.getParameter("name");
String contents = request.getParameter("contents");
String sql = "update board set title= ?, name=?, contents=? where no=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, name);
pstmt.setString(3, contents);
pstmt.setString(4, no);
int result = 0;
result = pstmt.executeUpdate();
if(result > 0){
response.sendRedirect("index.jsp");
} else{
response.sendRedirect("error.jsp");
}
%>
</body>
</html>
- delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="dbConn.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String no = request.getParameter("no");
String sql = "delete from board where no =?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, no);
int result = 0;
result = pstmt.executeUpdate();
if(result > 0){
response.sendRedirect("index.jsp");
} else{
response.sendRedirect("error.jsp");
}
%>
</body>
</html>
-error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>에러가 발생했습니다.</p>
<a href = "index.jsp">목록으로 돌아가기</a>
</body>
</html>
# 회원제 구현
- loginMain.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String id = (String)session.getAttribute("id");
if(id == null)
{
%>
<form method="post" action="LoginOK">
아이디 : <input type="text" name="id"><br>
비밀번호 : <input type="password" name = "pw"><br>
<input type = "submit" value ="로그인">
<a href="memberRegister.html">회원가입</a>
</form>
<%
} else {
%>
<%= id %>님 환영합니다.<br>
<a href="index.jsp?id=<%= id %>>">회원게시판</a>
<a href="memberModify">개인정보수정</a>
<a href="Logout">로그아웃</a>
<%
}
%>
</body>
</html>
- LoginOK.java
package com.tistory.coderbear;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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 javax.servlet.http.HttpSession;
@WebServlet("/LoginOK")
public class LoginOK extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginOK() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "1234";
String sql = "SELECT ID, PASSWORD FROM MEMBER WHERE ID=? AND PASSWORD=?";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String id = request.getParameter("id");
String pw = request.getParameter("pw");
HttpSession session = request.getSession();
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if(rs.next()){
session.setAttribute("id", id);
response.sendRedirect("loginMain.jsp");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
- memberRegister.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>회원가입</h2>
<form name="member" method="post" action="MemberRegisterOK"
onsubmit="return validateForm()">
<table>
<tr>
<td>이름</td>
<td><input type="text" name="name" required></td>
</tr>
<tr>
<td>아이디</td>
<td><input type="text" name="id" required></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pw" required></td>
</tr>
<tr>
<td>비밀번호확인</td>
<td><input type="password" name="pw2" required></td>
</tr>
<tr>
<td>성별</td>
<td><input type="radio" name="gender" value="male" checked>남
<input type="radio" name="gender" value="female">여</td>
</tr>
<tr>
<td>이메일</td>
<td><input type="email" name="email" required></td>
</tr>
<tr>
<td>취미</td>
<td>
<input type="checkbox" name="hobby" value="swimming">수영
<input type="checkbox" name="hobby" value="baseball">야구
<input type="checkbox" name="hobby" value="movie">영화감상
<input type="checkbox" name="hobby" value="music">음악감상
</td>
</tr>
<tr>
<td>핸드폰</td>
<td>
<select name="phone">
<option value="010">010</option>
<option value="011">011</option>
<option value="016">016</option>
</select>
<input type="text" name="phone2" required>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="회원가입">
<input type="reset" value="초기화">
</td>
</tr>
</table>
</form>
<script>
function validateForm() {
var pw = document.forms["member"]["pw"].value;
var pw2 = document.forms["member"]["pw2"].value;
if (pw != pw2) {
alert("비밀번호가 서로 맞지 않습니다.");
return false;
}
}
</script>
</body>
</html>
- MemberRegister.java
package com.tistory.coderbear;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/MemberRegister")
public class MemberRegister extends HttpServlet {
private static final long serialVersionUID = 1L;
public MemberRegister() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "1234";
String sql = "INSERT INTO MEMBER (NAME, ID, pw, gender, EMAIL, phone1, phone2, HOBBY) VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int result = 0;
String phone1 = null, phone2 = null, hobby = null;
String[] temp = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, request.getParameter("name"));
pstmt.setString(2, request.getParameter("id"));
pstmt.setString(3, request.getParameter("pw"));
pstmt.setString(4, request.getParameter("gender"));
pstmt.setString(5, request.getParameter("email"));
pstmt.setString(6, request.getParameter("phone1"));
pstmt.setString(7, request.getParameter("phone2"));
temp = request.getParameterValues(hobby);
for(int i=0;i<temp.length;i++) {
hobby += temp[i] + " ";
}
pstmt.setString(8, hobby);
result = pstmt.executeUpdate();
if(result >=1) {
response.sendRedirect("loginMain.jsp");
} else {
response.sendRedirect("error.jsp");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
- memberModify.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file ="dbConn.jsp" %>
<%
String name = null, id = null, gender = null, email = null, hobby = null, phone1 = null, phone2 = null;
String sql = "SELECT * FROM MEMBER WHERE ID=?";
try{
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, (String)session.getAttribute("id"));
rs = pstmt.executeQuery();
if(rs.next()){
name = rs.getString("name");
id = rs.getString("id");
gender = rs.getString("gender");
email = rs.getString("email");
hobby = rs.getString("hobby");
phone1 = rs.getString("phone1");
phone2 = rs.getString("phone2");
}
} catch(Exception e){
e.printStackTrace();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>개인정보수정</h2>
<form name="member" method="post" action="MemberRegisterOK"
onsubmit="return validateForm()">
<table>
<tr>
<td>이름</td>
<td>
<input type="text" name="name" value="<%=name %>" disabled>
</td>
</tr>
<tr>
<td>아이디</td>
<td>
<input type="text" value="<%=id %>" readonly>
</td>
</tr>
<tr>
<td>비밀번호</td>
<td>
<input type="password" name="pw" required>
</td>
</tr>
<tr>
<td>비밀번호확인</td>
<td>
<input type="password" name="pw2" required>
</td>
</tr>
<tr>
<td>성별</td>
<td>
<input type="radio" name="gender" value="male"
<%
if(gender.equals("male")) {
%>
checked
<%
}
%>
>남
<input type="radio" name="gender" value="female"
<%
if(gender.equals("female")) {
%>
checked
<%
}
%>
>여
</td>
</tr>
<tr>
<td>이메일</td>
<td>
<input type="email" name="email" value="<%=email %>" required>
</td>
</tr>
<tr>
<td>취미</td>
<td>
<input type="checkbox" name="hobby" value="swimming"
<%
if(hobby.indexOf("swimming")!=-1){
%>
checked
<%
}
%>
>수영
<input type="checkbox" name="hobby" value="baseball"
<%
if(hobby.indexOf("야구")!=-1){
%>
checked
<%
}
%>
>야구
<input type="checkbox" name="hobby" value="movie"
<%
if(hobby.indexOf("영화감상")!=-1){
%>
checked
<%
}
%>
>영화감상
<input type="checkbox" name="hobby" value="music"
<%
if(hobby.indexOf("음악감상")!=-1){
%>
checked
<%
}
%>
>음악감상
</td>
</tr>
<tr>
<td>핸드폰</td>
<td>
<select name="phone1">
<option value="010"
<%
if(phone1.equals("010")){
%>
selected
<%
}
%>
>010
</option>
<option value="011"
<%
if(phone1.equals("011")){
%>
selected
<%
}
%>
>011
</option>
<option value="016"
<%
if(phone1.equals("016")){
%>
selected
<%
}
%>
>016
</option>
</select>
<input type="text" name="phone2" required>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="회원가입">
<input type="reset" value="초기화">
</td>
</tr>
</table>
</form>
<script>
function validateForm() {
var pw = document.forms["member"]["pw"].value;
var pw2 = document.forms["member"]["pw2"].value;
if (pw != pw2) {
alert("비밀번호가 서로 맞지 않습니다.");
return false;
}
}
</script>
</body>
</html>
- MemberModify.java
package com.tistory.coderbear;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/MemberModify")
public class MemberModify extends HttpServlet {
private static final long serialVersionUID = 1L;
public MemberModify() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/project?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "1234";
String sql = "UPDATE MEMBER SET pw=?, gender=?, EMAIL=?, HOBBY=?, phone1=?, phone2=? WHERE id=?";
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
String phone1 = null, phone2 = null, hobby = null;
String[] temp = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, request.getParameter("name"));
pstmt.setString(2, request.getParameter("id"));
pstmt.setString(3, request.getParameter("pw"));
pstmt.setString(4, request.getParameter("gender"));
pstmt.setString(5, request.getParameter("email"));
pstmt.setString(6, request.getParameter("phone1"));
pstmt.setString(7, request.getParameter("phone2"));
temp = request.getParameterValues(hobby);
for(int i=0;i<temp.length;i++) {
hobby += temp[i] + " ";
}
pstmt.setString(8, hobby);
result = pstmt.executeUpdate();
if(result > 0) {
response.sendRedirect("loginMain.jsp");
} else {
response.sendRedirect("error.jsp");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
- Logout.java
package com.tistory.coderbear;
import java.io.IOException;
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 javax.servlet.http.HttpSession;
@WebServlet("/Logout")
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;
public Logout() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("loginMain.jsp");
}
}
728x90
'Web > JSP' 카테고리의 다른 글
[필기정리]Day79-1 - Connection Pool, JNDI, Data Source 등 (0) | 2020.10.20 |
---|---|
[필기정리]Day78 - ActionTag, JavaBeans, DAO, DTO, VO 등 (0) | 2020.10.20 |
[필기정리]Day77 - include(), forward() 등 (0) | 2020.10.17 |
[필기정리]Day76-3 - 저장영역(page, request, session, application) (0) | 2020.10.14 |
[필기정리]Day76-2 - ServletMapping, cookie 등 (0) | 2020.10.14 |