<writerBoard.jsp>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 파일 업로드 기능 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Forty by HTML5 UP</title>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--[if lte IE 8]><script src="assets/js/ie/html5shiv.js"></script><![endif]-->
<link rel="stylesheet" href="assetsBoard/css/main.css" />
<link rel="stylesheet" href="assetsBoard/css/board.css" />
<!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><![endif]-->
<!--[if lte IE 8]><link rel="stylesheet" href="assets/css/ie8.css" /><![endif]-->
</head>
<body>
<div id="board">
<!-- enctype 기본값 : application/x-www-form-urlencoded
: Key&Value 형태로 데이터 전송
파일, 이미지와 같은 많은 양의 데이터들은 전송해야 할 때는
인코딩 방식을 multipart/form-data로 바꿔주어야함 -->
<form action="WriteBoardService" method="post" enctype="multipart/form-data">
<table id="list">
<tr>
<td>제목</td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>작성자</td>
<td><input type="text" name="writer"></td>
</tr>
<tr>
<td colspan="2">내용</td>
</tr>
<tr>
<td colspan="2"><input type="file" name="fileName" style="float: right;">
<textarea name="content" rows="10" style="resize: none;"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="reset" value="초기화"> <input
type="submit" value="작성하기"></td>
</tr>
</table>
</form>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.scrolly.min.js"></script>
<script src="assets/js/jquery.scrollex.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
<script src="assets/js/main.js"></script>
</body>
</html>
<main.jsp>
<%@page import="com.smhrd.model.MemberVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Forty by HTML5 UP</title>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="assets/css/main.css" />
</head>
<body>
<%
// 2. session에 저장된 로그인 정보 가져오기
MemberVO vo = (MemberVO)session.getAttribute("member");
%>
<!-- Wrapper -->
<div id="wrapper">
<!-- Header -->
<header id="header" class="alt">
<a href="index.html" class="logo"><strong>Forty</strong> <span>by HTML5 UP</span></a>
<nav>
<% if(vo == null) {%>
<a href="#menu">로그인</a>
<%}else{%>
<% if(vo.getEmail().equals("admin")){ %>
<a href="select.jsp">회원정보관리</a>
<%} %>
<a href="update.jsp">개인정보수정</a>
<a href = "logoutService">로그아웃</a>
<%} %>
</nav>
</header>
<!-- Menu -->
<nav id="menu">
<ul class="links">
<li><h5>로그인</h5></li>
<form action = "LoginService" method = "post">
<li><input type="text" placeholder="Email을 입력하세요" name="email"></li>
<li><input type="password" placeholder="PW를 입력하세요" name="pw"></li>
<li><input type="submit" value="LogIn" class="button fit"></li>
</form>
</ul>
<ul class="actions vertical">
<li><h5>회원가입</h5></li>
<form action = "JoinService" method = "post">
<li><input type="text" placeholder="Email을 입력하세요" name="email"></li>
<li><input type="password" placeholder="PW를 입력하세요" name="pw"></li>
<li><input type="text" placeholder="전화번호를 입력하세요" name="phone"></li>
<li><input type="text" placeholder="집주소를 입력하세요" name ="address" ></li>
<li><input type="submit" value="JoinUs" class="button fit"></li>
</form>
</ul>
</nav>
<!-- Banner -->
<section id="banner" class="major">
<div class="inner">
<header class="major">
<% if(vo != null) {%>
<h1><%=vo.getEmail()%>님 환영합니다. </h1>
<%}else{%>
<h1>로그인 해주세요.</h1>
<%} %>
</header>
<div class="content">
<p> 게시판으로 이동! <br></p>
<ul class="actions">
<li><a href="boardMain.jsp" class="button next scrolly">게시판</a></li>
</ul>
</div>
</div>
</section>
<!-- Main -->
<div id="main">
<!-- One -->
<section id="one" class="tiles">
<article>
<span class="image">
<img src="images/pic01.jpg" alt="" />
</span>
<header class="major">
<h3><a href="#" class="link">HTML</a></h3>
<p>홈페이지를 만드는 기초 언어</p>
</header>
</article>
<article>
<span class="image">
<img src="images/pic02.jpg" alt="" />
</span>
<header class="major">
<h3><a href="#" class="link">CSS</a></h3>
<p>HTML을 디자인해주는 언어</p>
</header>
</article>
<article>
<span class="image">
<img src="images/pic03.jpg" alt="" />
</span>
<header class="major">
<h3><a href="#" class="link">Servlet/JSP</a></h3>
<p>Java를 기본으로 한 웹 프로그래밍 언어/스크립트 언어</p>
</header>
</article>
<article>
<span class="image">
<img src="images/pic04.jpg" alt="" />
</span>
<header class="major">
<h3><a href="#" class="link">JavaScript</a></h3>
<p>HTML에 기본적인 로직을 정의할 수 있는 언어</p>
</header>
</article>
<article>
<span class="image">
<img src="images/pic05.jpg" alt="" />
</span>
<header class="major">
<h3><a href="#" class="link">MVC</a></h3>
<p>웹 프로젝트 중 가장 많이 사용하는 디자인패턴</p>
</header>
</article>
<article>
<span class="image">
<img src="images/pic06.jpg" alt="" />
</span>
<header class="major">
<h3><a href="#" class="link">Web Project</a></h3>
<p>여러분의 최종프로젝트에 웹 기술을 활용하세요!</p>
</header>
</article>
</section>
<!-- Two -->
<section id="two">
<div class="inner">
<header class="major">
<h2>나에게 온 메세지 확인하기</h2>
</header>
<p></p>
<ul class="actions">
<li>로그인을 하세요.</li>
<li><a href="#" class="button next scrolly">전체삭제하기</a></li>
</ul>
</div>
</section>
</div>
<!-- Contact -->
<section id="contact">
<div class="inner">
<section>
<form>
<div class="field half first">
<label for="name">Name</label>
<input type="text" id="name" placeholder="보내는 사람 이름" />
</div>
<div class="field half">
<label for="email">Email</label>
<input type="text" id="email" placeholder="보낼 사람 이메일"/>
</div>
<div class="field">
<label for="message">Message</label>
<textarea id="message" rows="6"></textarea>
</div>
<ul class="actions">
<li><input type="submit" value="Send Message" class="special" /></li>
<li><input type="reset" value="Clear" /></li>
</ul>
</form>
</section>
<section class="split">
<section>
<div class="contact-method">
<span class="icon alt fa-envelope"></span>
<h3>Email</h3>
<% if(vo != null) {%>
<a><%=vo.getEmail()%></a>
<%}else{%>
<a href="#">로그인 한 사람의 이메일을 출력</a>
<%} %>
<!-- 로그인 한 사용자의 이메일을 출력하시오 -->
</div>
</section>
<section>
<div class="contact-method">
<span class="icon alt fa-phone"></span>
<h3>Phone</h3>
<% if(vo != null) {%>
<a><%=vo.getPhone()%></a>
<%}else{%>
<span>로그인 한 사람의 전화번호를 출력</span>
<%} %>
<!-- 로그인 한 사용자의 전화번호를 출력하시오 -->
</div>
</section>
<section>
<div class="contact-method">
<span class="icon alt fa-home"></span>
<h3>Address</h3>
<% if(vo != null) {%>
<a><%=vo.getAddress()%></a>
<%}else{%>
<span>로그인 한 사람의 집주소를 출력</span>
<%} %>
<!-- 로그인 한 사용자의 집주소를 출력하시오 -->
</div>
</section>
</section>
</div>
</section>
<!-- Footer -->
<footer id="footer">
<div class="inner">
<ul class="icons">
<li><a href="#" class="icon alt fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="#" class="icon alt fa-facebook"><span class="label">Facebook</span></a></li>
<li><a href="#" class="icon alt fa-instagram"><span class="label">Instagram</span></a></li>
<li><a href="#" class="icon alt fa-github"><span class="label">GitHub</span></a></li>
<li><a href="#" class="icon alt fa-linkedin"><span class="label">LinkedIn</span></a></li>
</ul>
<ul class="copyright">
<li>© Untitled</li><li>Design: <a href="https://html5up.net">HTML5 UP</a></li>
</ul>
</div>
</footer>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/jquery.scrolly.min.js"></script>
<script src="assets/js/jquery.scrollex.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
<script src="assets/js/main.js"></script>
</body>
</html>
<mybatis-config.xml>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- Mapper.xml 파일안에 parameterType, resultType 지정하게 될때
alias를 줘서 보다 간단하게 코드를 작성할 수 있게 도와주는 방법 -->
<typeAliases>
<typeAlias type="com.smhrd.model.MemberVO" alias="MemberVO"/>
<typeAlias type="com.smhrd.model.BoardVO" alias="BoardVO"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- DB연결설정 -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="hr"/>
<property name="password" value="hr"/>
<!-- url, username, password 프로젝트시 바꿔주기 -->
</dataSource>
</environment>
</environments>
<!-- Mapper 파일(SQL 쿼리문을 가지고 있는 파일) 설정하기 -->
<!-- 파일이 여러개 생성될 수 있다. -->
<mappers>
<mapper resource = "com/smhrd/database/MemberMapper.xml"/>
<mapper resource = "com/smhrd/database/BoardMapper.xml"/>
</mappers>
</configuration>
<BoardVO.java>
package com.smhrd.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@AllArgsConstructor // 모든변수생성자
@NoArgsConstructor // 기본생성자
@RequiredArgsConstructor // 원하는 변수 생성자
@Getter
@Setter
public class BoardVO {
private int num;
@NonNull
private String title;
@NonNull
private String writer;
@NonNull
private String fileName;
@NonNull
private String content;
private String b_day;
}
<BoardDAO.java>
package com.smhrd.model;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.smhrd.database.SqlSessionManager;
public class BoardDAO {
private SqlSessionFactory sqlSessionFactory = SqlSessionManager.getSqlSession();
private SqlSession sqlSession = null;
public int upload(BoardVO vo) {
int row = 0;
try {
sqlSession = sqlSessionFactory.openSession(true);
row = sqlSession.insert("com.smhrd.model.BoardDAO.boardinsert",vo);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return row;
}
}
<BoardMapper.xml>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.smhrd.model.BoardDAO" >
<insert id="boardinsert" parameterType="BoardVO">
insert into messageboard values(num_board.nextval, #{title}, #{writer}, #{fileName}, #{content}, sysdate)
</insert>
</mapper>
<WriteBoardService.java>
package com.smhrd.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 com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import com.smhrd.model.BoardDAO;
import com.smhrd.model.BoardVO;
@WebServlet("/WriteBoardService")
public class WriteBoardService extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// post방식 인코딩
request.setCharacterEncoding("UTF-8");
// MultipartRequest 매개변수
// 파일 저장 경로
// request.getServletContext() : 서블릿의 정보
// getRealPath("") : 실제 경로
String path = request.getServletContext().getRealPath("file");
System.out.println(path);
// 파일의 최대크기
// 1 mb = 1024kb
// 1 kb = 1024byte
int maxSize = 10*1024*1024; // 10mb
// 인코딩방식
String encoding = "UTF-8";
// 중복제거
DefaultFileRenamePolicy rename = new DefaultFileRenamePolicy();
// MultipartRequest : cos.jar 라이브러리에 있는 클래스
// 매개변수 : request, 파일 저장 경로, 파일 최대 크기, 인코딩 방식, 중복제거
MultipartRequest multi = new MultipartRequest(request, path, maxSize, encoding, rename);
// 게시글 정보 DB에 저장하기!
String title = multi.getParameter("title");
String writer = multi.getParameter("writer");
String fileName = multi.getFilesystemName("fileName");
String content = multi.getParameter("content");
BoardVO vo = new BoardVO(title, writer, fileName, content);
BoardDAO dao = new BoardDAO();
int row = dao.upload(vo);
if(row > 0) {
System.out.println("업로드 성공!");
}
RequestDispatcher rd = request.getRequestDispatcher("boardMain.jsp");
rd.forward(request, response);
}
}
'Maven Project' 카테고리의 다른 글
게시글 목록 조회 (0) | 2022.05.27 |
---|---|
게시판 삭제, 게시판 글 수정 (0) | 2022.05.26 |
게시판 만들기 (0) | 2022.05.25 |
JSTL 사용법 (0) | 2022.05.25 |
회원관리삭제 (0) | 2022.05.25 |