경로 설정
WebContent > img폴더
WebContent/WEB-INF/lib > cos.jar, jstl.jar, ojdbc6.jar, standard.jar 임포트하기
com.BoardDAO
package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class BoardDAO {
private Connection conn;
private PreparedStatement psmt;
private ResultSet rs;
private void getConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String db_url = "jdbc:oracle:thin:@localhost:1521:xe";
String db_id = "hr";
String db_pw = "hr";
conn = DriverManager.getConnection(db_url, db_id, db_pw);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void close() {
try {
if (rs != null)
rs.close();
if (psmt != null)
psmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public int insert(BoardDTO dto) {
int cnt = 0;
try {
getConnection();
String sql = "insert into web_board values(board_num.nextval,?,?,?,?,sysdate)";
psmt = conn.prepareStatement(sql);
psmt.setString(1, dto.getTitle());
psmt.setString(2, dto.getId());
psmt.setString(3, dto.getFileName());
psmt.setString(4, dto.getContent());
cnt = psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return cnt;
}
public ArrayList<BoardDTO> selectAll() {
ArrayList<BoardDTO> list = new ArrayList<BoardDTO>();
try {
getConnection();
String sql = "Select * from web_board";
psmt = conn.prepareStatement(sql);
rs = psmt.executeQuery();
while (rs.next()) {
int num = rs.getInt(1);
String title = rs.getString(2);
String id = rs.getString(3);
String fileName = rs.getString(4);
String content = rs.getString(5);
String day = rs.getString(6);
BoardDTO dto = new BoardDTO(num, title, id, fileName, content, day);
list.add(dto);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return list;
}
public BoardDTO select(int choice) {
BoardDTO dto = null;
try {
getConnection();
String sql = "Select * from web_board where num = ?";
psmt = conn.prepareStatement(sql);
psmt.setInt(1, choice);
rs = psmt.executeQuery();
while (rs.next()) {
int num = rs.getInt(1);
String title = rs.getString(2);
String id = rs.getString(3);
String fileName = rs.getString(4);
String content = rs.getString(5);
String day = rs.getString(6);
dto = new BoardDTO(num, title, id, fileName, content, day);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return dto;
}
public void deleteAll(String id) {
try {
getConnection();
String sql = "delete web_board where id=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, id);
psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
public void delete(int num) {
try {
getConnection();
String sql = "delete web_board where num=?";
psmt = conn.prepareStatement(sql);
psmt.setInt(1, num);
psmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
}
com.BoardDTO
package com;
public class BoardDTO {
private int num;
private String title;
private String id;
private String fileName;
private String content;
private String day;
public BoardDTO(int num, String title, String id, String fileName, String content, String day) {
this.num = num;
this.title = title;
this.id = id;
this.fileName = fileName;
this.content = content;
this.day = day;
}
public BoardDTO(String title, String id, String fileName, String content) {
this.title = title;
this.id = id;
this.fileName = fileName;
this.content = content;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
}
com.UploadService
package com;
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.xml.ws.Response;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
@WebServlet("/UploadService")
public class UploadService extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("EUC-KR");
String saveDir = request.getServletContext().getRealPath("img");
int maxSize = 5 * 1024 * 1024; // 5MB
String encoding = "EUC-KR";
MultipartRequest multi = new MultipartRequest(request, saveDir, maxSize, encoding,
new DefaultFileRenamePolicy());
String title = multi.getParameter("title");
String id = multi.getParameter("id");
String content = multi.getParameter("content");
String fileName = multi.getFilesystemName("fileName");
BoardDTO dto = new BoardDTO(title, id, fileName, content);
BoardDAO dao = new BoardDAO();
int cnt = dao.insert(dto);
if(cnt > 0) {
response.sendRedirect("main.jsp");
}else {
response.sendRedirect("upload.jsp");
}
}
}
Board.sql
drop table web_board
create table web_board(
num number,
title varchar2(200),
id varchar2(100),
fileName varchar2(100),
content varchar2(1000),
day date
)
drop sequence board_num
create sequence board_num start with 1 increment by 1
select * from WEB_BOARD
main.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="com.BoardDTO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
div {
width: 80%;
margin: 0px auto;
text-align: center;
}
table {
width: 800px;
margin: 0px auto;
text-align: center;
}
</style>
</head>
<body>
<%
BoardDAO dao = new BoardDAO();
ArrayList<BoardDTO> list = dao.selectAll();
pageContext.setAttribute("list", list);
%>
<div>
<table border="1px solid black">
<tr>
<td>번호</td>
<td>제목</td>
<td>아이디</td>
<td>시간</td>
</tr>
<c:forEach items="${list}" var="board">
<c:set var="num" value="${num+1}"/>
<tr>
<td>${num}</td>
<td><a href="view.jsp?choice=${board.num}">${board.title}</a></td>
<td>${board.id}</td>
<td>${board.day}</td>
</tr>
</c:forEach>
</table>
<a href="upload.jsp">작성하기</a>
</div>
</body>
</html>
upload.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
div{
width: 80%;
margin: 0px auto;
text-align: center;
}
table{
width: 800px;
height: 300px;
margin: 0px auto;
text-align: center;
}
input[type="text"]{
width: 80%;
}
</style>
</head>
<body>
<div>
<form action="UploadService" method="post" enctype="multipart/form-data">
<table border="1px solid black">
<tr>
<td>제목</td>
<td colspan="3"><input type="text" name="title"></td>
</tr>
<tr>
<td>아이디</td>
<td><input type="text" name="id"></td>
<td>파일</td>
<td><input type="file" name="fileName"></td>
</tr>
<tr>
<td colspan="4">내용</td>
</tr>
<tr>
<td colspan="4">
<textarea name="content" rows="10" cols="50"></textarea>
</td>
</tr>
<tr>
<td colspan="4">
<input type="reset" value="취소">
<input type="submit" value="작성">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
view.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="com.BoardDTO"%>
<%@page import="com.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
<style type="text/css">
div {
width: 80%;
margin: 0px auto;
text-align: center;
}
table {
width: 800px;
margin: 0px auto;
text-align: center;
}
</style>
</head>
<body>
<%
int choice = Integer.parseInt(request.getParameter("choice"));
BoardDAO dao = new BoardDAO();
BoardDTO dto = dao.select(choice);
pageContext.setAttribute("dto", dto);
%>
<div>
<form>
<table border="1px solid black">
<tr>
<td>제목</td>
<td colspan="3">${dto.title}</td>
</tr>
<tr>
<td colspan="2">아이디</td>
<td colspan="2">${dto.id}</td>
</tr>
<tr>
<td colspan="4">내용</td>
</tr>
<tr>
<td colspan="4">${dto.content}<br> <img
src="img/<%=URLEncoder.encode(dto.getFileName(), "EUC-KR")%>"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
'Programming > Servlet & JSP' 카테고리의 다른 글
담벼락 웹페이지(MessageSystem) (0) | 2020.02.24 |
---|---|
회원관리(MemberSystem) (0) | 2020.02.24 |
JSTL 연습하기 (0) | 2020.02.24 |
쿠키 값 읽어오기 (0) | 2020.02.23 |
체크박스 EL식으로 값 가져오기 (0) | 2020.02.23 |