- BoardBean.java
- private String b_fname;
- private int b_fsize;
- write.jsp =>ํ์ผ ์ถ๊ฐ
- write_ok.jsp =>ํ์ผ ์ ๋ก๋ ์ฒ๋ฆฌ
- BoardDBBean.java
- insertBoard() ๋ฉ์๋ => insert ์ ํ์ผ ์ด๋ฆ, ํ์ผ ํฌ๊ธฐ ์ถ๊ฐ
- getBoard() ๋ฉ์๋ => BoardBean ๊ฐ์ฒด์ ํ์ผ์ด๋ฆ/ํฌ๊ธฐ ์ ํ
- listBoard() ๋ฉ์๋ => board ๊ฐ์ฒด์ ํ์ผ์ด๋ฆ/ํฌ๊ธฐ ์ ํ- list.jsp => ์ฒจ๋ถํ์ผ ์ถ๊ฐ
- show.jsp > ํ์ผ ๋ด์ฉ ํ์
- delete_ok.jsp =>ํ์ผ ์ญ์
CREATE TABLE BOARDT
(B_ID NUMBER(5) PRIMARY KEY
,B_NAME VARCHAR2(20)
,B_EMAIL VARCHAR2(50)
,B_TITLE VARCHAR2(80)
,B_CONTENT VARCHAR2(3000)
,B_DATE DATE
,B_HIT NUMBER(5) DEFAULT 0
,B_PWD VARCHAR2(12)
,B_IP VARCHAR2(15)
,B_REF NUMBER(5)
,B_STEP NUMBER(5)
,B_LEVEL NUMBER(5)
,B_FNAME VARCHAR2(100)
,B_FSIZE NUMBER(10)
);
package magic.board;
import java.sql.Timestamp;
public class BoardBean {
private int b_id;
private String b_name;
private String b_email;
private String b_title;
private String b_content;
private Timestamp b_date;
private int b_hit;
private String b_pwd;
private String b_ip;
private int b_ref;
private int b_step;
private int b_level;
private String b_fname;
private int b_fsize;
public String getB_fname() {
return b_fname;
}
public void setB_fname(String b_fname) {
this.b_fname = b_fname;
}
public int getB_fsize() {
return b_fsize;
}
public void setB_fsize(int b_fsize) {
this.b_fsize = b_fsize;
}
public int getB_ref() {
return b_ref;
}
public void setB_ref(int b_ref) {
this.b_ref = b_ref;
}
public int getB_step() {
return b_step;
}
public void setB_step(int b_step) {
this.b_step = b_step;
}
public int getB_level() {
return b_level;
}
public void setB_level(int b_level) {
this.b_level = b_level;
}
public String getB_ip() {
return b_ip;
}
public void setB_ip(String b_ip) {
this.b_ip = b_ip;
}
public String getB_pwd() {
return b_pwd;
}
public void setB_pwd(String b_pwd) {
this.b_pwd = b_pwd;
}
public int getB_id() {
return b_id;
}
public void setB_id(int b_id) {
this.b_id = b_id;
}
public String getB_name() {
return b_name;
}
public void setB_name(String b_name) {
this.b_name = b_name;
}
public String getB_email() {
return b_email;
}
public void setB_email(String b_email) {
this.b_email = b_email;
}
public String getB_title() {
return b_title;
}
public void setB_title(String b_title) {
this.b_title = b_title;
}
public String getB_content() {
return b_content;
}
public void setB_content(String b_content) {
this.b_content = b_content;
}
public Timestamp getB_date() {
return b_date;
}
public void setB_date(Timestamp b_date) {
this.b_date = b_date;
}
public int getB_hit() {
return b_hit;
}
public void setB_hit(int b_hit) {
this.b_hit = b_hit;
}
//================ ํ์ด์ง ๋ชฉ๋ก ===================
public static int pageSize =10; //ํํ์ด์ง์ 10๊ฐ์ ๊ธ์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํ ๋ณ์
public static int pageCount = 1; //ํ์ด์ง ๊ฐ์ ์นด์ดํธ, ํ์ด์ง(์ปฌ๋ผ)๊ฐ ๋ง์ ์๋ก ๋์ด๋จ(์ด๊ธฐ๊ฐ)
public static int pageNum = 1; //ํ์ด์ง ๋ฒํธ(๊ธฐ๋ณธ๊ฐ)
//์ด๋ถ๋ถ ์ ๋ฆฌ ๋ค์ ํด์ ๋
ธ์
์ ์ ๋ฆฌํ๊ธฐ
// 84๊ฑด์ ๊ฒ์๊ธ ์กด์ฌ
//=> pageCount = 9
//pageNum = 1
//limit = 4(์ฌ์ง์์ ๋ชฉ๋ก์ค ์ซ์๊ฐ ๋ช๊ฐ ์๋๊ฐ? 1,2,3,4 => 5,6,7,8 =>9 ์ด๋ฐ ์์ผ๋ก ๋ชฉ๋ก์ด ์ ํ ์์
//temp = 0 // (pageNum -1) % limit;
//startPage = 1 //pageNum(1) - temp(0)
//if(1-4>0) ๊ฑฐ์ง
//for(int i = 1; i<5; i++){
// if(1=1)(์ฐธ) str=[1]
// if(1>=9)(๊ฑฐ์ง)
// }
//for(int 2 = 1; i<5; i++){
// if(2=1) ๊ฑฐ์ง
// else{str=[1]+ [2] //1์ ๋งํฌ๊ฐ ๊ฑธ๋ฆฌ์ง ์๊ณ 2๋ ๋งํฌ๊ฐ ๊ฑธ๋ฆผ
// if(2>=9)(๊ฑฐ์ง)
// }
//for(int 3 = 1; i<5; i++){
// if(3=1) ๊ฑฐ์ง
// else{str=[1]+ [2] +[3]//3์ ๋งํฌ ๊ฑธ๋ฆผ
// if(3>=9)(๊ฑฐ์ง)
// }
//for(int 4 = 1; 4<5; i++){
// if(4=1) ๊ฑฐ์ง
// else{str=[1]+ [2] + [3] + [4]//4์ ๋งํฌ ๊ฑธ๋ฆผ
// if(4>=9)(๊ฑฐ์ง)
// }
//if (5<= 9){ 5[๋ค์]
//
//}
// ์ต์ข
์ ์ผ๋ก [1]+[2]+[3]+[4]+ ๋ค์
//===============================
// 84๊ฑด์ ๊ฒ์๊ธ ์กด์ฌ
//=> pageCount = 9
//pageNum = 1
//limit = 4(์ฌ์ง์์ ๋ชฉ๋ก์ค ์ซ์๊ฐ ๋ช๊ฐ ์๋๊ฐ? 1,2,3,4 => 5,6,7,8 =>9 ์ด๋ฐ ์์ผ๋ก ๋ชฉ๋ก์ด ์ ํ ์์
//temp = 0 // (pageNum -1) % limit;
//startPage = 1 //pageNum(1) - temp(0)
//if(1-4>0) ๊ฑฐ์ง
//for(int i = 1; i<5; i++){
// if(1=1)(์ฐธ) str=[1]
// if(1>=9)(๊ฑฐ์ง)
// }
//for(int 2 = 1; i<5; i++){
// if(2=1) ๊ฑฐ์ง
// else{str= [1]+ [2] //1์ ๋งํฌ๊ฐ ๊ฑธ๋ฆฌ์ง ์๊ณ 2๋ ๋งํฌ๊ฐ ๊ฑธ๋ฆผ
// if(2>=9)(๊ฑฐ์ง)
// }
//for(int 3 = 1; i<5; i++){
// if(3=1) ๊ฑฐ์ง
// else{str=[1]+ [2] +[3]//3์ ๋งํฌ ๊ฑธ๋ฆผ
// if(3>=9)(๊ฑฐ์ง)
// }
//for(int 4 = 1; 4<5; i++){
// if(4=1) ๊ฑฐ์ง
// else{str=[1]+ [2] + [3] + [4]//4์ ๋งํฌ ๊ฑธ๋ฆผ
// if(4>=9)(๊ฑฐ์ง)
// }
//if (5<= 9){ 5[๋ค์]
//
//}
// ์ต์ข
์ ์ผ๋ก [1]+[2]+[3]+[4]+ ๋ค์
// 84๊ฑด์ ๊ฒ์๊ธ ์กด์ฌ
//=> pageCount = 9
//pageNum = 5
//limit = 4(์ฌ์ง์์ ๋ชฉ๋ก์ค ์ซ์๊ฐ ๋ช๊ฐ ์๋๊ฐ? 1,2,3,4 => 5,6,7,8 =>9 ์ด๋ฐ ์์ผ๋ก ๋ชฉ๋ก์ด ์ ํ ์์
//temp = 0 // (pageNum -1) % limit;
//startPage = 5 //pageNum(5) - temp(0)
//if(5-4>0) ์ฐธ {[์ด์ 4]}
//for(int i = 5; i<9; i++){
// if(5=5)(์ฐธ) str=[์ด์ 4]+[5] : ๋๊ฐ ๋ค ๋งํฌ ๊ฑธ๋ ค์์
// if(5>=9)(๊ฑฐ์ง)
// }
//for(int i = 6; i<9; i++){
// if(6=1) ๊ฑฐ์ง
// else{str= [์ด์ 4]+[5]+[6] //6์ ๋งํฌ๊ฐ ๊ฑธ๋ฆผ
// if(6>=9)(๊ฑฐ์ง)
// }
//for(int i = 7; i<9; i++){
// if(7=5) ๊ฑฐ์ง
// else{str=[์ด์ 4]+[5]+[6]+[7]
// if(7>=9)(๊ฑฐ์ง)
// }
//for(int i = 8; i<9; i++){
// if(7=1) ๊ฑฐ์ง
// else{str=[์ด์ 4]+[5]+[6]+[7]+[8]
// if(3>=9)(๊ฑฐ์ง)
// }
//if (9<= 9){ 9[๋ค์]
//
//}
// ์ต์ข
์ ์ผ๋ก [์ด์ 4]+[5]+[6]+[7]+[8]+ [๋ค์ 9]
// 84๊ฑด์ ๊ฒ์๊ธ ์กด์ฌ
//pageCount = 9
//pageNum = 9
//limit = 4(์ฌ์ง์์ ๋ชฉ๋ก์ค ์ซ์๊ฐ ๋ช๊ฐ ์๋๊ฐ? 1,2,3,4 => 5,6,7,8 =>9 ์ด๋ฐ ์์ผ๋ก ๋ชฉ๋ก์ด ์ ํ ์์
//temp = 0 // (pageNum -1) % limit;
//startPage = 9 //pageNum(9) - temp(0)
//if(9-4>0) ์ฐธ {[์ด์ 8]}
//for(int i = 9; i<13; i++){
// if(9=9)(์ฐธ) str=[์ด์ 8]+[9] : ๋๊ฐ ๋ค ๋งํฌ ๊ฑธ๋ ค์์
// if(9>=9)์ฐธ
// }
//if(13<=9)๊ฑฐ์ง
//
//}
//์ต์ข
์ ์ผ๋ก [์ด์ 8]+[9]
//=============
public static String pageNumber(int limit) {
//ํ์ด์ง ๋ชฉ๋ก๋ค์ ๋ง๋ค์ด ์ฃผ๋ ๋ฉ์๋
//int limit -> ๋ช๊ฐ์ ํ์ด์ง์ ๋ํ ๋ก์ง์ ์ฒ๋ฆฌํ ๊ฒ์ธ์ง ์ค์
String str ="";
int temp = (pageNum -1) % limit;//์์ ํ์ด์ง๋ฅผ ๊ตฌํจ
int startPage = pageNum - temp; // ์์ํ์ด์ง ์ค์ "1 - 0 = 1" ์ด๊ธฐ ๋๋ฌธ์ 1์ด ๋จ
if((startPage - limit) > 0) {//[์ด์ ]์ ๊ตฌํํ๊ธฐ ์ํ if๋ฌธ
//limit 4, temp 0 , startPage 1์ด๋ผ๊ณ ๊ฐ์ ํ์๋
//if((1-4)>0)
//pageNum = 5 ์ธ๊ฒฝ์ฐ => startPage 5
//if((5-4) >0 ) => ์ฐธ
str ="<a href='list.jsp?pageNum="+(startPage-1)+"'>[์ด์ ]</a> ";
//str = [์ด์ ]
}
for (int i = startPage; i < (startPage+limit); i++) {
//ํ์ด์ง ๋ฒํธ๊ฐ ๋ช๊ฐ์ธ์ง ๋ชจ๋ฆ. ์ฆ ํ์ด์ง ๋ฒํธ๋ฅผ ๋์ดํ ํ์๊ฐ ์์
//for(i = 1; i < 5; i++) - >4๊น์ง ๋์ด
//pageNum = 5์ธ๊ฒฝ์ฐ for(i= 5; i<9;i++)
if (i == pageNum) {
//i = 1 ์ด๋ฉด ๋งํฌ๊ฐ ๊ฑธ๋ฆฌ์ง ์์
str += "["+i+"] ";
//str = [1]
}else {
str += "<a href='list.jsp?pageNum="+i+"'>["+i+"]</a> ";
}
if (i >= pageCount) {
break;
}
}
if((startPage + limit) <= pageCount) {//[๋ค์]์ ๊ตฌํํ๊ธฐ ์ํ if๋ฌธ
str +="<a href='list.jsp?pageNum="+(startPage+limit)+"'>[๋ค์]</a> ";
// str = "<a href='list.jsp?pageNum="+(startPage-1)+"'>[์ด์ ]</a> ";
//str = [์ด์ ]
}
return str;
}
}
package magic.board;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BoardDBBean {
//1. ์ ์ญ BoardDBBean ๊ฐ์ฒด ๋ ํผ๋ฐ์ค๋ฅผ ๋ฆฌํดํ๋ ๋ฉ์๋
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance(){
return instance;
//getConnection์ด๋ getInstance๋ ๊ณ์ ์ฌ์ฉํ๊ธฐ ์ข์(์ฌ์ฌ์ฉํ๊ธฐ ์ข๋ค๋ ๋ป)
//jsp์์ ๋ฉ์๋ ํธ์ถํ๋ฉด ํธํ๊ธฐ ๋๋ฌธ
}
//2.์ฟผ๋ฆฌ ์์
์ ์ฌ์ฉํ ์ปค๋ฅ์
๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ ๋ฉ์๋
public Connection getConnection( ) throws Exception{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
//๊ฒฝ๋ก ์ฐพ๊ธฐ
return ds.getConnection();
//getConnection์ด๋ getInstance๋ ๊ณ์ ์ฌ์ฉํ๊ธฐ ์ข์(์ฌ์ฌ์ฉํ๊ธฐ ์ข๋ค๋ ๋ป)
//jsp์์ ๋ฉ์๋ ํธ์ถํ๋ฉด ํธํ๊ธฐ ๋๋ฌธ
}
//3. ์ ๋ฌ์ธ์๋ก ๋ฐ์ BoardBean board๋ฅผ BOARDT ํ
์ด๋ธ์ ์ฝ์
ํ๋ ๋ฉ์๋
public int insertBoard(BoardBean board) throws Exception {
int re = -1;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "";
int number;
// ๋ต๋ณ๊ธ์ ์ํ ๋ณ์
int id = board.getB_id();
int ref = board.getB_ref();
int step = board.getB_step();
int level = board.getB_level();
try {
conn = getConnection();
sql = "SELECT MAX(B_ID) FROM BOARDT";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
number = rs.getInt(1)+1;
} else {
number = 1;
}
// id๊ฐ ๊ฐ์ด ์๊ฑฐ๋ 0์ด๊ฑฐ๋ ๋ ์ค ํ๋๊ฐ ๋ ์ ์๋ค. 0 ์ผ๋๋ ์๊ธ, 0์ด ์๋ ๋๋ ๋ต๋ณ๊ธ
if (id != 0) { //๋ต๋ณ๊ธ์ผ ๋
// update๋ฌธ์ด ํต์ฌ
sql = "UPDATE BOARDT SET b_step = b_step+1 WHERE b_ref=? AND b_step>?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, ref);
pstmt.setInt(2, step);
pstmt.executeUpdate();
step = step + 1;
level = level + 1;
} else { //๋ต๋ณ๊ธ์ด ์๋ ๋=์๊ธ
ref = number;
step = 0;
level = 0;
}
// System.out.println("@@@### board.number ===> "+number);
// System.out.println("@@@### board.getB_name() ===> "+board.getB_name());
// System.out.println("@@@### board.getB_email() ===> "+board.getB_email());
// System.out.println("@@@### board.getB_title() ===> "+board.getB_title());
// System.out.println("@@@### board.getB_content() ===> "+board.getB_content());
// System.out.println("@@@### board.getB_date() ===> "+board.getB_date());
// System.out.println("@@@### board.getB_pwd() ===> "+board.getB_pwd());
// System.out.println("@@@### board.getB_ip() ===> "+board.getB_ip());
// sql = "INSERT INTO BOARDT VALUES(?,?,?,?,?,?,?,?,?)";
// sql = "INSERT INTO BOARDT VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
sql = "INSERT INTO BOARDT VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, number);
pstmt.setString(2, board.getB_name());
pstmt.setString(3, board.getB_email());
pstmt.setString(4, board.getB_title());
pstmt.setString(5, board.getB_content());
pstmt.setTimestamp(6, board.getB_date());
pstmt.setInt(7, board.getB_hit());
pstmt.setString(8, board.getB_pwd());
pstmt.setString(9, board.getB_ip());
pstmt.setInt(10, ref);
pstmt.setInt(11, step);
pstmt.setInt(12, level);
pstmt.setString(13, board.getB_fname());
pstmt.setInt(14, board.getB_fsize());
pstmt.executeUpdate();
re = 1;
// re = pstmt.executeUpdate();
} catch(SQLException ex) {
System.out.println("์ถ๊ฐ ์คํจ");
ex.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}
return re;
}
//4. ๋ฆฌํดํ์
์ด ArrayList<BoardBean> ์ธ listBoard() ๋ฉ์๋ ์ถ๊ฐ(๊ธ๋ชฉ๋ก์ ์ํ ๋ฉ์๋)
public ArrayList<BoardBean> listBoard(String pageNumber) throws Exception{//์ ๋ค๋ฆญ ์ฌ์ฉํจ , ํ๋ผ๋ฏธํฐ๋ BoardBean
Connection conn = null;//๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ ์
Statement stmt = null;// DB์ SQL ์ ๋ฌ
int dbCount = 0;//๊ธ์ด ๋ช๊ฐ๋ ์๋์ง ํ์ธ(๊ฒ์๊ธ์ ๊ฐฏ์
int absoultepage = 0;//๊ธ์ด ๋ช๊ฐ๋ ์๋์ง ํ์ธ(๊ฒ์๊ธ์ ๊ฐฏ์
ResultSet rs = null;
ResultSet pageSet = null;
//db ์ ๋ณด ๋ฐ๊ธฐ ์ํจ
String sql=" SELECT b_id \r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content\r\n" +
" , b_date\r\n" +
" , b_hit\r\n" +
" , b_pwd\r\n" +
" , b_ip\r\n" +
" , b_ref\r\n" +
" , b_step\r\n" +
" , b_level\r\n" +
" , b_fname\r\n" +
" , b_fsize\r\n" +
" FROM BOARDT\r\n" +
" ORDER BY b_ref desc, b_step asc";//ํด๋น ๋ฐ์ดํฐ ์ฟผ๋ฆฌ ์ ๋ ฌ๋๊ฒ๋ ํจ.
String sql2="SELECT COUNT(b_id) FROM BOARDT";
ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();//ArrayList๋ก ๊ฒ์๊ธ๋ค์ ๋ฐ์
try {
conn = getConnection();
// stmt = conn.createStatement();// DB์ SQL ์ ๋ฌ(SQL๋ฌธ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ์ ์ธ
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
// ๋งค๊ฐ๋ณ์ (,๋ณ๊ฒฝ์ ๋ด์ฉ์ ์ ์ฅํ๊ฒ ๋ค.)
// DB์ SQL ์ ๋ฌ(SQL๋ฌธ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ์ ์ธ
pageSet = stmt.executeQuery(sql2);//์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ rs์ ๋ฐ์(select ์กฐํ๋ฌธ์ executeQuery ๋ฉ์๋ ์ฒ๋ฆฌ
if(pageSet.next()) {
dbCount = pageSet.getInt(1);
pageSet.close();
}
if (dbCount % BoardBean.pageSize == 0) { //pageCount ์ธํ
(ํ์ด์ง ๊ฐ์๋ฅผ ์๋ ค์ฃผ๊ธฐ ์ํด ์ฌ์ฉ)
//80 % 10 = 0
BoardBean.pageCount = dbCount / BoardBean.pageSize; //80 /10
} else {//84 % 10 = 4
BoardBean.pageCount = dbCount / BoardBean.pageSize +1 ; //80/10 +1
}
if (pageNumber != null) {
BoardBean.pageNum = Integer.parseInt(pageNumber); //๋งค๊ฐ๋ณ์์ ํด๋น๋๋ pageNumber ์ธํ
absoultepage = (BoardBean.pageNum - 1) * BoardBean.pageSize + 1;
}
rs = stmt.executeQuery(sql);//์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ rs์ ๋ฐ์(select ์กฐํ๋ฌธ์ executeQuery ๋ฉ์๋ ์ฒ๋ฆฌ
if (rs.next()) {//while๋ฌธ์ ๊ฐ์
rs.absolute(absoultepage);
int count = 0;
while(count < BoardBean.pageSize) {//pageSize์ (10๋ฒ) ๋งํผ ๋ฐ๋ณต
//๋ค์(์ฌ๋ฌ๊ฐ์) ์ฟผ๋ฆฌ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ์กฐ๊ฑด๋ฌธ์ผ๋ก ์ค.
BoardBean board = new BoardBean();
//๊ฒฐ๊ณผ๊ฐ์ ์ธํ
board.setB_id(rs.getInt(1));//์์ด๋๋ INT์ด๋ฏ๋ก getInt๋ก ํด์ผํจ
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
board.setB_date(rs.getTimestamp(6));
board.setB_hit(rs.getInt(7));//์กฐํ์๋ INT์ด๋ฏ๋ก getInt๋ก ํด์ผํจ
board.setB_pwd(rs.getString(8));
board.setB_ip(rs.getString(9));
board.setB_ref(rs.getInt(10));
board.setB_step(rs.getInt(11));
board.setB_level(rs.getInt(12));
board.setB_fname(rs.getString(13));
board.setB_fsize(rs.getInt(14));
// ๋ฐ์ดํฐ๊ฐ ์ ๋๋ก ๋ค์ด๊ฐ๋์ง ํ์ธ ํด์ผํจ
// System.out.println("@@@@#### board.getB_id()=====>"+board.getB_id());
// System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
// System.out.println("@@@@#### board.getB_pwd()=====>"+board.getB_pwd());
// System.out.println("@@@@####");
boardList.add(board);//๋ฐ๋ณตํ๋ฉด์ ๊ฒ์๊ธ๋ค์ ์์
if (rs.isLast()) {//๊ฒฐ๊ณผ๊ฐ์ด ๋ง์ง๋ง์ด๋ฉด ๋น ์ ธ๋์ค๊ธฐ
break;
} else {
rs.next();//๋ง์ง๋ง์ด ์๋๋ฉด ๊ณ์ ์งํ
}
count++;
}
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//์์๋ฐ๋ฉ(์์ ์ค์)
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return boardList;
}
// 5. ๋ฆฌํดํ์
์ด BoardBean์ธ getBoard() ๋ฉ์๋ ์ถ๊ฐ
public BoardBean getBoard (int bid, boolean hitadd) throws Exception{
//๊ธ๋ฒํธ๋ง ๋๊ธฐ๋ฉด ์์์ ๋ด์ฉ์ด ๋์ด๊ฐ๋ ๋ฉ์๋
//์กฐํ ์ ๋๋ฌธ์ ๋ถ๊ธฐ์ฒ๋ฆฌ ํด๊ฐ
Connection conn = null;//๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ ์
PreparedStatement pstmt = null;// DB์ SQL ์ ๋ฌ
PreparedStatement pstmtup = null;// ์กฐํ์ ์
๋ฐ์ดํธ
ResultSet rs = null;
//db ์ ๋ณด ๋ฐ๊ธฐ ์ํจ
BoardBean board = null;
String sql = "";//ํด๋น ๋ฒํธ์ ๋ฐ์ดํฐ ์ถ๋ ฅ๋๊ฒ ์ฟผ๋ฆฌ ์์ฑ
try {
conn = getConnection();
if (hitadd==true) {
//์กฐํ์ ์
๋ฐ์ดํธ ์ถ๊ฐํจ
sql = "UPDATE BOARDT SET b_hit=b_hit+1 WHERE b_id=?";
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ๋ฐ์
// DB์ SQL ์ ๋ฌ(SQL๋ฌธ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ์ ์ธ
pstmt.setInt(1, bid);//์กฐ๊ฑด์ด ํ๋์ด๊ธฐ ๋๋ฌธ์ (?๊ฐ ํ๋์)
// rs = pstmt.executeQuery();//์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ rs์ ๋ฐ์(select ์กฐํ๋ฌธ์ executeQuery ๋ฉ์๋ ์ฒ๋ฆฌ
pstmt.executeUpdate();//์
๋ฐ์ดํธ๋๊น ์
๋ฐ์ดํธ๋ก ๋ฐ๊ฟ์ค
//pstmtup.close();
//์
๋ฐ์ดํธ ์ถ๊ฐ ๋!
}
sql = "SELECT b_id\r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content\r\n" +
" , b_date\r\n" +
" , b_hit\r\n" +
" , b_pwd\r\n" +
" , b_ip\r\n" +
" , b_ref\r\n" +
" , b_level\r\n" +
" , b_step\r\n" +
" , b_fname\r\n" +
" , b_fsize\r\n" +
" FROM BOARDT\r\n" +
" WHERE b_id=?";//ํด๋น ๋ฒํธ์ ๋ฐ์ดํฐ ์ถ๋ ฅ๋๊ฒ ์ฟผ๋ฆฌ ์์ฑ
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ๋ฐ์
// DB์ SQL ์ ๋ฌ(SQL๋ฌธ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ์ ์ธ
pstmt.setInt(1, bid);//์กฐ๊ฑด์ด ํ๋์ด๊ธฐ ๋๋ฌธ์ (?๊ฐ ํ๋์)
rs = pstmt.executeQuery();//select ๋ฌธ์ด๋๊น ์ต์คํํธ ์ฟผ๋ฆฌ
if(rs.next()) {//๋ค์(์ฌ๋ฌ๊ฐ์) ์ฟผ๋ฆฌ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ์กฐ๊ฑด๋ฌธ์ผ๋ก ์ค.
board = new BoardBean();//๊ฐ์ฒด์์ฑ(๋ฆฌํด๊ฐ์ด board)
board.setB_id(rs.getInt(1));
//board.setB_id(bid);๋ ๊ฐ๋ฅํจ
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
board.setB_date(rs.getTimestamp(6));
board.setB_hit(rs.getInt(7));
board.setB_pwd(rs.getString(8));
board.setB_ip(rs.getString(9));
board.setB_ref(rs.getInt(10));//์กฐํ์๋ INT์ด๋ฏ๋ก getInt๋ก ํด์ผํจ
board.setB_step(rs.getInt(11));
board.setB_level(rs.getInt (12));
board.setB_fname(rs.getString(13));
board.setB_fsize(rs.getInt (14));
//
//// ๋ฐ์ดํฐ๊ฐ ์ ๋๋ก ๋ค์ด๊ฐ๋์ง ํ์ธ ํด์ผํจ
System.out.println("@@@@#### board.getB_id()=====>"+board.getB_id());
System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
System.out.println("@@@@#### board.getB_fname()=====>"+board.getB_fname());
System.out.println("@@@@#### board.getB_fsize()=====>"+board.getB_fsize());
//System.out.println("@@@@#### board.setB_pwd()=====>"+board.setB_pwd(sql));
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//์์๋ฐ๋ฉ(์์ ์ค์)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return board;
}
// 6.deleteBoard() ๋ฉ์๋ => ์ญ์ ํ ๊ธ ๋น๋ฐ๋ฒํธ ํ์ธํ๋ ๋ฉ์๋
public int deleteBoard (int b_id, String b_pwd) throws Exception{//๊ธ๋ฒํธ๋ง ๋๊ธฐ๋ฉด ์์์ ๋ด์ฉ์ด ๋์ด๊ฐ๋ ๋ฉ์๋
// id์ pwd๋ฅผ ๋ฐ์ ์ญ์ ํ๋ ๋ฉ์๋
Connection conn = null;//๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ ์
PreparedStatement pstmt = null;// DB์ SQL ์ ๋ฌ
ResultSet rs = null;
int re = -1;
String sql = "";//ํด๋น ๋ฒํธ์ ๋ฐ์ดํฐ ์ถ๋ ฅ๋๊ฒ ์ฟผ๋ฆฌ ์์ฑ
String pwd = "";//๋น๋ฐ๋ฒํธ
try {
conn = getConnection();
sql = "SELECT B_PWD FROM BOARDT where B_ID=?"; // ๊ธ ๋ฒํธ์ ๋ฐ๋ฅธ ๋น๋ฐ๋ฒํธ ๊ฐ์ง๊ณ ์ค๊ธฐ
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ๋ฐ์
pstmt.setInt(1, b_id);//๊ฐ์ ์ง์ด๋ฃ์(์ฟผ๋ฆฌ์ ?๋ถ๋ถ)
rs = pstmt.executeQuery();
if(rs.next()) {//๋ค์(์ฌ๋ฌ๊ฐ์) ์ฟผ๋ฆฌ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ์กฐ๊ฑด๋ฌธ์ผ๋ก ์ค.
pwd = rs.getString(1);
//๋น๋ฐ๋ฒํธ๋ฅผ
if (pwd.equals(b_pwd)) {
sql = "DELETE FROM BOARDT WHERE B_ID=?";
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ์ฟผ๋ฆฌ๋ฅผ ๋ฐ์
pstmt.setInt(1, b_id);
pstmt.executeUpdate();
re = 1;
}else {
re =0;
}
}
} catch(SQLException ex) {
System.out.println("์ญ์ ์คํจ");
ex.printStackTrace();
} finally {
try{//์์๋ฐ๋ฉ(์์ ์ค์)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return re;
}
//7.ํ
์ด๋ธ ์์ editBoard ๋ฉ์๋
public int editBoard (BoardBean board) throws Exception{
int re = -1;
Connection conn = null;//๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ ์
PreparedStatement pstmt = null;// DB์ SQL ์ ๋ฌ
ResultSet rs = null;
String sql ="";
String pwd = "";//๋น๋ฐ๋ฒํธ
try {
conn = getConnection();
sql = "SELECT B_PWD FROM BOARDT where B_ID=?"; // ๊ธ ๋ฒํธ์ ๋ฐ๋ฅธ ๋น๋ฐ๋ฒํธ ๊ฐ์ง๊ณ ์ค๊ธฐ
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ๋ฐ์
pstmt.setInt(1, board.getB_id());//๊ฐ์ ์ง์ด๋ฃ์(์ฟผ๋ฆฌ์ ?๋ถ๋ถ),board ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ b_id ๊ฐ์ง๊ณ ์ค๊ธฐ
rs = pstmt.executeQuery();
if (rs.next()) {
pwd = rs.getString(1);
if (pwd.equals(board.getB_pwd())) {
sql="UPDATE BOARDT \r\n" +
" SET B_NAME=?\r\n" +
" , B_EMAIL=?\r\n" +
" , B_TITLE=?\r\n" +
" , B_CONTENT=?\r\n" +
" WHERE B_ID=?";
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ์ฟผ๋ฆฌ๋ฅผ ๋ฐ์
pstmt.setString(1,board.getB_name());
pstmt.setString(2,board.getB_email());
pstmt.setString(3,board.getB_title());
pstmt.setString(4,board.getB_content());
pstmt.setInt(5, board.getB_id());
pstmt.executeUpdate();
re = 1;
}else {
re =0;//๋น๋ฐ๋ฒํธ๊ฐ ๊ฐ์ง ์์๊ฒ
}
}
} catch(SQLException ex) {
System.out.println("์์ ์คํจ");
ex.printStackTrace();
} finally {
try{//์์๋ฐ๋ฉ(์์ ์ค์)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return re;
}
}
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String pageNum = request.getParameter("pageNum");//pageNum์ ๋ฐ์
int b_id=0, b_ref=1, b_step=0, b_level=0;
String b_title="";
if(request.getParameter("b_id") != null){
b_id = Integer.parseInt(request.getParameter("b_id"));
}
BoardDBBean db = BoardDBBean.getInstance();
BoardBean board = db.getBoard(b_id, false);
if(board != null){
b_title = board.getB_title();
b_ref = board.getB_ref();
b_step = board.getB_step();
b_level = board.getB_level();
}
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script language="JavaScript" src="board.js" charset="utf-8"></script>
</head>
<body>
<center>
<h1>๊ธ ์ฌ ๋ฆฌ ๊ธฐ</h1>
<form name="reg_frm" method="post" action="write_ok.jsp" enctype="multipart/form-data">
<input type="hidden" name="b_id" value="<%= b_id %>">
<input type="hidden" name="b_ref" value="<%= b_ref %>">
<input type="hidden" name="b_step" value="<%= b_step %>">
<input type="hidden" name="b_level" value="<%= b_level %>">
<table>
<tr height="30">
<td width="80">์์ฑ์</td>
<td width="140">
<input type="text" name="b_name" size="10" maxlength="20">
</td>
<td width="80">์ด๋ฉ์ผ</td>
<td width="240">
<input type="text" name="b_email" size="24" maxlength="50">
</td>
</tr>
<tr height="30">
<td width="80">๊ธ์ ๋ชฉ</td>
<td colspan="3" width="460">
<%
if(b_id == 0){
%>
<input type="text" name="b_title" size="55" maxlength="80">
<%
}else{
%>
<input type="text" name="b_title" size="55" maxlength="80"
value="[๋ต๋ณ]:<%= b_title %>">
<%
}
%>
</td>
</tr>
<tr height="30">
<td width="80">ํ ์ผ</td>
<td colspan="3" width="140">
<input type="file" name="b_fname" size="40" maxlength="100">
</td>
</tr>
<tr>
<td colspan="4">
<textarea name="b_content" rows="10" cols="65"></textarea>
</td>
</tr>
<tr height="30">
<td width="80">์ ํธ</td>
<td colspan="3" width="460">
<input type="password" name="b_pwd" size="12" maxlength="12">
</td>
</tr>
<tr height="50" align="center">
<td colspan="4">
<input type="button" value="๊ธ์ฐ๊ธฐ" onclick="check_ok()">
<input type="reset" value="๋ค์์์ฑ">
<input type="button" value="๊ธ๋ชฉ๋ก" onclick="location.href='list.jsp?pageNum=<%=pageNum%>'">
<!-- ํ์ด์ง ๋ชฉ๋ก์ ๋ง์ถฐ์ ์ด๋ํ ์ ์๋๋ก ํจ -->
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@page import="com.jspsmart.upload.File"%>
<%@page import="java.net.InetAddress"%>
<%@page import="magic.board.BoardDBBean"%>
<%@page import="java.sql.Timestamp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean class="magic.board.BoardBean" id="board"></jsp:useBean>
<jsp:setProperty property="*" name="board"/>
<%
//ํ์ผ ์
๋ก๋
SmartUpload upload = new SmartUpload();//๊ฐ์ฒด์์ฑ
upload.initialize(pageContext);//๊ธฐ๋ณธ ๊ฐ์ฒด ์ด๊ธฐํ
upload.upload();//upload ๋ฉ์๋ ํธ์ถ
String fName =null;
int fileSize = 0;
File file = upload.getFiles().getFile(0);//ํ์ผ์ด ํ๋์ด๋๊น 0์ผ๋ก ํ๋ฉด ๋จ ์ฌ๋ฌ๊ฐ๋ฉด ๋ฐ๋ณต๋ฌธ ์ฌ์ฉํด์ i๋ก ๋๊ธฐ(for)
if(!file.isMissing()){
fName = file.getFileName();
file.saveAs("/upload/"+file.getFileName());//ํ์ผ ์ด๋ฆ์ ์ ์ฅํ๊ฒ ๋ค.
fileSize = file.getSize();
}
%>
<%
InetAddress address = InetAddress.getLocalHost();
String ip = address.getHostAddress();
//๋ณ๋๋ก write.jsp์ name ์ฒ๋ฆฌ
//๋ฐ์ดํฐ ์
๋ ฅํ์์๋ null๊ฐ์ด ๋จ๊ธฐ ๋๋ฌธ์ ์ฒ๋ฆฌ๊ฐ ํ์ํจ
board.setB_name(upload.getRequest().getParameter("b_name"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_email(upload.getRequest().getParameter("b_email"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_title(upload.getRequest().getParameter("b_title"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_content(upload.getRequest().getParameter("b_content"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_pwd(upload.getRequest().getParameter("b_pwd"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
//
board.setB_date(new Timestamp(System.currentTimeMillis()));
//board.setB_ip(request.getRemoteAddr());
board.setB_ip(ip);
//ํ์ผ ์
๋ก๋ ๊ด๋ จ ๋ถ๋ถ
board.setB_fname(fName);
board.setB_fsize(fileSize);
////ํ์ผ ์
๋ก๋ ๊ด๋ จ ๋ถ๋ถ ๋!
BoardDBBean db = BoardDBBean.getInstance();
int re = db.insertBoard(board);
if(re == 1){
response.sendRedirect("list.jsp");
}else{
response.sendRedirect("write.jsp");
}
%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String pageNum = request.getParameter("pageNum");//pageNum์ ๋ฐ์
if(pageNum == null){//list๋ฅผ ์ฒ์ ์คํํ์์๋ ์๋ฌด๊ฒ๋ ์์๋ ๋ณด์ฌ์ค ํ์ด์ง
pageNum ="1";
}
BoardDBBean db = BoardDBBean.getInstance();
ArrayList<BoardBean> boardList = db.listBoard(pageNum);
int b_id, b_hit, b_level=0, b_fsize=0;//์ด๊ธฐ๊ฐ 0
String b_name, b_email, b_title, b_content,b_fname;
Timestamp b_date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>๊ฒ์ํ์ ๋ฑ๋ก๋ ๊ธ ๋ชฉ๋ก ๋ณด๊ธฐ</h1>
<table width="600">
<tr>
<td align="right">
<a href="write.jsp?pageNum=<%=pageNum%>">๊ธ ์ฐ ๊ธฐ</a>
<!-- ํ์ด์ง ๋ชฉ๋ก ๋ฒํธ์ ๋ง๊ฒ ๊ธ ์ฐ๊ธฐ ์ด๋ -->
</td>
</tr>
</table>
</center>
<center>
<table border="1" width="800" cellspacing="0">
<tr height="25">
<td width="40" align="center">๋ฒํธ</td>
<td width="80" align="center">์ฒจ๋ถํ์ผ</td>
<td width="450" align="center">๊ธ์ ๋ชฉ</td>
<td width="120" align="center">์์ฑ์</td>
<td width="130" align="center">์์ฑ์ผ</td>
<td width="60" align="center">์กฐํ์</td>
</tr>
<%
for(int i=0; i<boardList.size(); i++){
BoardBean board = boardList.get(i);
b_id = board.getB_id();
b_name = board.getB_name();
b_email = board.getB_email();
b_title = board.getB_title();
b_content = board.getB_content();
b_date = board.getB_date();
b_hit = board.getB_hit();
b_level = board.getB_level();
b_fname = board.getB_fname();
b_fsize = board.getB_fsize();
%>
<tr height="25" bgcolor="#f7f7f7"
onmouseover="this.style.backgroundColor='#eeeeef'"
onmouseout="this.style.backgroundColor='#f7f7f7'">
<td align="center"><%= b_id %></td>
<td align="center">
<%
if(b_fsize > 0){
%>
<img src="../images/zip.gif">
<%
}
%>
</td>
<td>
<%
if(b_level > 0){
for(int j=0; j<b_level; j++){
%>
<%
}
%>
<img src="../images/AnswerLine.gif" width="16" height="16">
<%
}
%>
<a href="show.jsp?b_id=<%= b_id %>&pageNum=<%= pageNum %>">
<%= b_title %>
</a>
</td>
<td align="center">
<a href="mailto:<%= b_email %>">
<%= b_name %>
</a>
</td>
<td align="center">
<%-- <%= b_date %> --%>
<%= sdf.format(b_date) %>
</td>
<td align="center">
<%= b_hit %>
</td>
</tr>
<%
}
%>
</table>
<%= BoardBean.pageNumber(4) %>
<!-- return ๋ฐ์์ 4ํ์ด์ง์ฉ ๋๋ ์ ์ถ๋ ฅ -->
</center>
</body>
</html>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String pageNum = request.getParameter("pageNum");//pageNum์ ๋ฐ์
int bid = Integer.parseInt(request.getParameter("b_id"));
BoardDBBean db = BoardDBBean.getInstance();
//BoardBean board = db.getBoard(bid);
BoardBean board = db.getBoard(bid, true);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>๊ธ ๋ด ์ฉ ๋ณด ๊ธฐ</h1>
<table border="1" width="600" cellspacing="0">
<tr height="30" align="center">
<td width="100">๊ธ๋ฒํธ</td>
<td width="200">
<%= bid %>
</td>
<td width="100">์กฐํ์</td>
<td width="200">
<%= board.getB_hit() %>
</td>
</tr>
<tr height="30" align="center">
<td width="100">์์ฑ์</td>
<td width="200">
<%= board.getB_name() %>
</td>
<td width="100">์์ฑ์ผ</td>
<td width="200">
<%-- <%= board.getB_date() %> --%>
<%= sdf.format(board.getB_date()) %>
</td>
</tr>
<tr height="30" align="center">
<td width="110">ํ ์ผ</td>
<td width="200" align="center" colspan="3">
<%
if(board.getB_fname() != null){
%>
<img src="../images/zip.gif">
<a href="../upload/<%= board.getB_fname() %>">
์๋ณธํ์ผ : <%= board.getB_fname() %>
</a>
<%
}
%>
</td>
</tr>
<tr height="30" align="center">
<td width="100">๊ธ์ ๋ชฉ</td>
<td width="200" align="left" colspan="3">
<%= board.getB_title() %>
</td>
</tr>
<tr height="30" align="center">
<td width="100">๊ธ๋ด์ฉ</td>
<td width="200" align="left" colspan="3">
<%= board.getB_content() %>
</td>
</tr>
<tr height="30">
<td colspan="4" align="right">
<!-- ํ์ด์ง ๋ชฉ๋ก์ ๋ง์ถฐ์ผ ํด์ ๋ชจ๋ ์ฟผ๋ฆฌ์คํธ๋ง์ ์ฌ์ฉํ์ฌ pageNum=<2%=pageNum%2> ๋ฃ์ด์ผํจ -->
<input type="button" value="๊ธ์์ " onclick="location.href='edit.jsp?b_id=<%= bid %>&pageNum=<%= pageNum %>'">
<input type="button" value="๊ธ์ญ์ " onclick="location.href='delete.jsp?b_id=<%= bid %>&pageNum=<%= pageNum %>'">
<input type="button" value="๋ต๋ณ๊ธ" onclick="location.href='write.jsp?b_id=<%= bid %>&pageNum=<%= pageNum %>'">
<input type="button" value="๊ธ๋ชฉ๋ก" onclick="location.href='list.jsp?pageNum=<%=pageNum%>'">
</td>
</tr>
</table>
</center>
</body>
</html>
<%@page import="java.io.File"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean" %>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<%
String pageNum = request.getParameter("pageNum");//pageNum์ ๋ฐ์
int b_id = Integer.parseInt(request.getParameter("b_id"));
//์
๋ ฅ ๋ฐ์ id ๊ฐ์ onclick์ผ๋ก ๋๊ฒจ์ค
String b_pwd = request.getParameter("b_pwd");
// ๋น๋ฐ๋ฒํธ๋ ๋ค์ ๋ฐ์
BoardDBBean db = BoardDBBean.getInstance();
//getInstance๋ก BoardDBBean ๊ฐ์ฒด ์์ฑ
BoardBean board = db.getBoard(b_id, false);//์กฐํ์ ์ฆ๊ฐ ํ์ ์์์ผ๋ก false
String fName = board.getB_fname();//ํ์ผ๋ช
๊ฐ์ง๊ณ ์ค๊ธฐ
String up ="D:\\space_jsp\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp0\\wtpwebapps\\magicWebApp\\upload\\";
//๊ฒฝ๋ก ๋ณ์๋ก ์ฃผ๊ธฐ
int re = db.deleteBoard(b_id, b_pwd);
//deleteBoard ๋ฉ์๋ ํธ์ถํ์ฌ ๊ธ๋ฒํธ์ ๋น๋ฐ๋ฒํธ ๋๊น
if(re == 1){//๋น๋ฐ๋ฒํธ๊ฐ ๋์ผ ํ ๊ฒฝ์ฐ//๊ฒ์๊ธ์ด ์ ์์ ์ผ๋ก ์ญ์ ๋์์๋
response.sendRedirect("list.jsp?pageNum="+pageNum);
if(fName != null){
File file = new File(up+fName); //์์ฑ์ ๋งค๊ฐ๋ณ์ : ํด๋๊ฒฝ๋ก+ํ์ผ์ด๋ฆ
file.delete();//ํ์ผ ์ญ์ ๋ฉ์๋ delete();
}
}else if(re == 0){//๋น๋ฐ๋ฒํธ๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ
%>
<script language="JavaScript">
alert("๋น๋ฐ๋ฒํธ๊ฐ ๋ง์ง ์์ต๋๋ค.");
history.go(-1);//์ด์ ํ์ด์ง๋ก ์ด๋
</script>
<%
}else if(re == -1){//๊ธ๋ฒํธ๊ฐ ์๋ ๊ฒฝ์ฐ(์ญ์ ์คํจํ ๊ฒฝ์ฐ)
%>
<script language="JavaScript">
alert("์ญ์ ์ ์คํจํ์์ต๋๋ค.");
history.go(-1);
</script>
<%
}
%>
์ ์๋ ๋จ.
a.txtํ์ผ์ ๋ง๋ค์ด์(๋ด์ฉ abc) ์ ๋ก๋ํ๊ณ (์ฒซ๋ฒ์งธ ๊ฒ์๋ฌผ) ํด๋ฆญํ๋ฉด abc๊ฐ ๋ธ.
๋ค์ ๋ด์ฉ๋ง ๋ณ๊ฒฝ ํ(abc โ xyz, a.txtํ์ผ) ์ ๋ก๋ ํ์์ ๋(๋๋ฒ์งธ ๊ฒ์๋ฌผ) ํด๋ฆญํ๋ฉด xyz๊ฐ ๋ธ.
๊ทผ๋ฐ ์ฒซ๋ฒ์งธ ๊ฒ์๋ฌผ์ ํ์ธํด๋ณด๋ฉด abc๊ฐ ์๋๋ผ xyz๊ฐ ๋ธ.
๋ง์ฝ์ ์ ๋ชฉ์ ๊ฐ๊ณ ๋ด์ฉ์ ๋ค๋ฅธ ํ์ผ์ ์ฒจ๋ถํ์๋ค๋ฉด ๊ธฐ์กด์ ๋ฐ์ดํฐ ๋๋ ์ด๋ฆ์ด ๊ฐ์ ํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ์์คํ ์์์. ์ด๋ฅผ ๋ง๊ธฐ ์ํ ์ฝ๋ ํ์
(์๋ฃ์ค 4)
(์์ ์ค๋ช ์ฐธ๊ณ )
๐ก
- BoardBean.java
- private String b_rfname;
- write_ok.jsp
- ํ์ผ ์ ๋ก๋ ์ฒ๋ฆฌ
- BoardDBBean.java
- insertBoard() ๋ฉ์๋ => insert ์ ์ค์ ํ์ผ ์ด๋ฆ ์ถ๊ฐ
- getBoard() ๋ฉ์๋ => ์กฐํ์
- BoardBean getFileName(int bid) => (์ ๊ท) ํ์ผ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
- show.jsp
- ์ค์ ํ์ผ ๋ด์ฉ ํ์(FileDownload.jsp ์ฒจ๋ถํ์ผ ํ์)
- FileDownload.jsp => ์ ๊ท ์ถ๊ฐ
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@page import="com.jspsmart.upload.File"%>
<%@page import="java.net.InetAddress"%>
<%@page import="magic.board.BoardDBBean"%>
<%@page import="java.sql.Timestamp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean class="magic.board.BoardBean" id="board"></jsp:useBean>
<jsp:setProperty property="*" name="board"/>
<%
// ํ์ผ ์
๋ก๋ ์๋ฃ์ค 3
/* SmartUpload upload = new SmartUpload();//๊ฐ์ฒด์์ฑ
upload.initialize(pageContext);//๊ธฐ๋ณธ ๊ฐ์ฒด ์ด๊ธฐํ
upload.upload();//upload ๋ฉ์๋ ํธ์ถ
String fName =null;
String rfName =null;
int fileSize = 0;
File file = upload.getFiles().getFile(0);//ํ์ผ์ด ํ๋์ด๋๊น 0์ผ๋ก ํ๋ฉด ๋จ ์ฌ๋ฌ๊ฐ๋ฉด ๋ฐ๋ณต๋ฌธ ์ฌ์ฉํด์ i๋ก ๋๊ธฐ(for)
if(!file.isMissing()){
fName = file.getFileName();
file.saveAs("/upload/"+file.getFileName());//ํ์ผ ์ด๋ฆ์ ์ ์ฅํ๊ฒ ๋ค.
fileSize = file.getSize();
}
*/
//์๋ฃ์ค 4
String path = request.getRealPath("upload");
int size=1024*1024;
int fileSize=0;//์ด๊ธฐ๊ฐ0
String file="";//ํ์ผ
String oriFile="";//์ค์ ํ์ผ
MultipartRequest multi = new MultipartRequest(request,path,size,"UTF-8",new DefaultFileRenamePolicy());//๋ ํ์คํธ, ๊ฒฝ๋ก,์ฌ์ด์ฆ,์ธ์ด, ๊ธฐ๋ณธํ์ผ๋ณ๊ฒฝ์ ์ฑ
์ฌ์ฉ??
// ๊ฑ MultipartRequest์ ์ด๋ฐ์์ผ๋ก ๊ฐ์ฒด ์์ฑํด์ ์ฌ์ฉํ๋ค๋ผ๊ณ ์ดํดํ๋ฉด ๋ ๋ฏ
Enumeration files = multi.getFileNames();
String str =(String)files.nextElement();
//์ค์ ํ์ผ ์ด๋ฆ ํ๋๋ฅผ ๋ฐ์
file = multi.getFilesystemName(str);
if(file != null){//ํ์ผ์ด ์กด์ฌํ๋ค๋ฉด
oriFile = multi.getOriginalFileName(str);
fileSize = file.getBytes().length;//ํ์ผ ๋ณ์์์ getBytees()๋ฉ์๋์ length ์ฌ์ฉํ๋ฉด ์ฉ๋..?
}
%>
<%
//ip์ฒ๋ฆฌ
InetAddress address = InetAddress.getLocalHost();
String ip = address.getHostAddress();
/* //์๋ฃ์ค 3
//๋ณ๋๋ก write.jsp์ name ์ฒ๋ฆฌ
//๋ฐ์ดํฐ ์
๋ ฅํ์์๋ null๊ฐ์ด ๋จ๊ธฐ ๋๋ฌธ์ ์ฒ๋ฆฌ๊ฐ ํ์ํจ
//ํ์ผ์ ์
๋ก๋ ํ์์๋ ํ์
board.setB_name(upload.getRequest().getParameter("b_name"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_email(upload.getRequest().getParameter("b_email"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_title(upload.getRequest().getParameter("b_title"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_content(upload.getRequest().getParameter("b_content"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_pwd(upload.getRequest().getParameter("b_pwd"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
//๋ณ๋๋ก write.jsp์ name ์ฒ๋ฆฌ ๋! */
board.setB_name(multi.getParameter("b_name"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_email(multi.getParameter("b_email"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_title(multi.getParameter("b_title"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_content(multi.getParameter("b_content"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
board.setB_pwd(multi.getParameter("b_pwd"));//upload๊ฐ์ฒด ์ด์ฉํด์ ์ฌ์ฉ
//board ๊ฐ์ฒด์ set์ผ๋ก ๊ฐ๋ค์ ๋์
์ํด
board.setB_date(new Timestamp(System.currentTimeMillis()));
//board.setB_ip(request.getRemoteAddr());
board.setB_ip(ip);
//์๋ฃ์ค 3
//ํ์ผ ์
๋ก๋ ๊ด๋ จ ๋ถ๋ถ
//board.setB_fname(fName);
//board.setB_fsize(fileSize);
////ํ์ผ ์
๋ก๋ ๊ด๋ จ ๋ถ๋ถ ๋!
//์๋ฃ์ค 4
if(file != null){//ํ์ผ์ด ์กด์ฌํ๋ค๋ฉด
board.setB_fname(file);
board.setB_fsize(fileSize);
board.setB_rfname(oriFile);
}
//์๋ฃ์ค 4๋
BoardDBBean db = BoardDBBean.getInstance();
int re = db.insertBoard(board);
if(re == 1){
response.sendRedirect("list.jsp");
}else{
response.sendRedirect("write.jsp");
}
%>
package magic.board;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BoardDBBean {
//1. ์ ์ญ BoardDBBean ๊ฐ์ฒด ๋ ํผ๋ฐ์ค๋ฅผ ๋ฆฌํดํ๋ ๋ฉ์๋
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance(){
return instance;
//getConnection์ด๋ getInstance๋ ๊ณ์ ์ฌ์ฉํ๊ธฐ ์ข์(์ฌ์ฌ์ฉํ๊ธฐ ์ข๋ค๋ ๋ป)
//jsp์์ ๋ฉ์๋ ํธ์ถํ๋ฉด ํธํ๊ธฐ ๋๋ฌธ
}
//2.์ฟผ๋ฆฌ ์์
์ ์ฌ์ฉํ ์ปค๋ฅ์
๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ ๋ฉ์๋
public Connection getConnection( ) throws Exception{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
//๊ฒฝ๋ก ์ฐพ๊ธฐ
return ds.getConnection();
//getConnection์ด๋ getInstance๋ ๊ณ์ ์ฌ์ฉํ๊ธฐ ์ข์(์ฌ์ฌ์ฉํ๊ธฐ ์ข๋ค๋ ๋ป)
//jsp์์ ๋ฉ์๋ ํธ์ถํ๋ฉด ํธํ๊ธฐ ๋๋ฌธ
}
//3. ์ ๋ฌ์ธ์๋ก ๋ฐ์ BoardBean board๋ฅผ BOARDT ํ
์ด๋ธ์ ์ฝ์
ํ๋ ๋ฉ์๋
public int insertBoard(BoardBean board) throws Exception {
int re = -1;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "";
int number;
// ๋ต๋ณ๊ธ์ ์ํ ๋ณ์
int id = board.getB_id();
int ref = board.getB_ref();
int step = board.getB_step();
int level = board.getB_level();
try {
conn = getConnection();
sql = "SELECT MAX(B_ID) FROM BOARDT";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if (rs.next()) {
number = rs.getInt(1)+1;
} else {
number = 1;
}
// id๊ฐ ๊ฐ์ด ์๊ฑฐ๋ 0์ด๊ฑฐ๋ ๋ ์ค ํ๋๊ฐ ๋ ์ ์๋ค. 0 ์ผ๋๋ ์๊ธ, 0์ด ์๋ ๋๋ ๋ต๋ณ๊ธ
if (id != 0) { //๋ต๋ณ๊ธ์ผ ๋
// update๋ฌธ์ด ํต์ฌ
sql = "UPDATE BOARDT SET b_step = b_step+1 WHERE b_ref=? AND b_step>?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, ref);
pstmt.setInt(2, step);
pstmt.executeUpdate();
step = step + 1;
level = level + 1;
} else { //๋ต๋ณ๊ธ์ด ์๋ ๋=์๊ธ
ref = number;
step = 0;
level = 0;
}
// System.out.println("@@@### board.number ===> "+number);
// System.out.println("@@@### board.getB_name() ===> "+board.getB_name());
// System.out.println("@@@### board.getB_email() ===> "+board.getB_email());
// System.out.println("@@@### board.getB_title() ===> "+board.getB_title());
// System.out.println("@@@### board.getB_content() ===> "+board.getB_content());
// System.out.println("@@@### board.getB_date() ===> "+board.getB_date());
// System.out.println("@@@### board.getB_pwd() ===> "+board.getB_pwd());
// System.out.println("@@@### board.getB_ip() ===> "+board.getB_ip());
// sql = "INSERT INTO BOARDT VALUES(?,?,?,?,?,?,?,?,?)";
// sql = "INSERT INTO BOARDT VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
// sql = "INSERT INTO BOARDT VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
sql = "INSERT INTO BOARDT VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, number);
pstmt.setString(2, board.getB_name());
pstmt.setString(3, board.getB_email());
pstmt.setString(4, board.getB_title());
pstmt.setString(5, board.getB_content());
pstmt.setTimestamp(6, board.getB_date());
pstmt.setInt(7, board.getB_hit());
pstmt.setString(8, board.getB_pwd());
pstmt.setString(9, board.getB_ip());
pstmt.setInt(10, ref);
pstmt.setInt(11, step);
pstmt.setInt(12, level);
pstmt.setString(13, board.getB_fname());
pstmt.setInt(14, board.getB_fsize());
pstmt.setString(15, board.getB_rfname());
pstmt.executeUpdate();
re = 1;
// re = pstmt.executeUpdate();
} catch(SQLException ex) {
System.out.println("์ถ๊ฐ ์คํจ");
ex.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}
return re;
}
//4. ๋ฆฌํดํ์
์ด ArrayList<BoardBean> ์ธ listBoard() ๋ฉ์๋ ์ถ๊ฐ(๊ธ๋ชฉ๋ก์ ์ํ ๋ฉ์๋)
public ArrayList<BoardBean> listBoard(String pageNumber) throws Exception{//์ ๋ค๋ฆญ ์ฌ์ฉํจ , ํ๋ผ๋ฏธํฐ๋ BoardBean
Connection conn = null;//๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ ์
Statement stmt = null;// DB์ SQL ์ ๋ฌ
int dbCount = 0;//๊ธ์ด ๋ช๊ฐ๋ ์๋์ง ํ์ธ(๊ฒ์๊ธ์ ๊ฐฏ์
int absoultepage = 0;//๊ธ์ด ๋ช๊ฐ๋ ์๋์ง ํ์ธ(๊ฒ์๊ธ์ ๊ฐฏ์
ResultSet rs = null;
ResultSet pageSet = null;
//db ์ ๋ณด ๋ฐ๊ธฐ ์ํจ
String sql=" SELECT b_id \r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content\r\n" +
" , b_date\r\n" +
" , b_hit\r\n" +
" , b_pwd\r\n" +
" , b_ip\r\n" +
" , b_ref\r\n" +
" , b_step\r\n" +
" , b_level\r\n" +
" , b_fname\r\n" +
" , b_fsize\r\n" +
" FROM BOARDT\r\n" +
" ORDER BY b_ref desc, b_step asc";//ํด๋น ๋ฐ์ดํฐ ์ฟผ๋ฆฌ ์ ๋ ฌ๋๊ฒ๋ ํจ.
String sql2="SELECT COUNT(b_id) FROM BOARDT";
ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();//ArrayList๋ก ๊ฒ์๊ธ๋ค์ ๋ฐ์
try {
conn = getConnection();
// stmt = conn.createStatement();// DB์ SQL ์ ๋ฌ(SQL๋ฌธ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ์ ์ธ
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
// ๋งค๊ฐ๋ณ์ (,๋ณ๊ฒฝ์ ๋ด์ฉ์ ์ ์ฅํ๊ฒ ๋ค.)
// DB์ SQL ์ ๋ฌ(SQL๋ฌธ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ์ ์ธ
pageSet = stmt.executeQuery(sql2);//์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ rs์ ๋ฐ์(select ์กฐํ๋ฌธ์ executeQuery ๋ฉ์๋ ์ฒ๋ฆฌ
if(pageSet.next()) {
dbCount = pageSet.getInt(1);
pageSet.close();
}
if (dbCount % BoardBean.pageSize == 0) { //pageCount ์ธํ
(ํ์ด์ง ๊ฐ์๋ฅผ ์๋ ค์ฃผ๊ธฐ ์ํด ์ฌ์ฉ)
//80 % 10 = 0
BoardBean.pageCount = dbCount / BoardBean.pageSize; //80 /10
} else {//84 % 10 = 4
BoardBean.pageCount = dbCount / BoardBean.pageSize +1 ; //80/10 +1
}
if (pageNumber != null) {
BoardBean.pageNum = Integer.parseInt(pageNumber); //๋งค๊ฐ๋ณ์์ ํด๋น๋๋ pageNumber ์ธํ
absoultepage = (BoardBean.pageNum - 1) * BoardBean.pageSize + 1;
}
rs = stmt.executeQuery(sql);//์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ rs์ ๋ฐ์(select ์กฐํ๋ฌธ์ executeQuery ๋ฉ์๋ ์ฒ๋ฆฌ
if (rs.next()) {//while๋ฌธ์ ๊ฐ์
rs.absolute(absoultepage);
int count = 0;
while(count < BoardBean.pageSize) {//pageSize์ (10๋ฒ) ๋งํผ ๋ฐ๋ณต
//๋ค์(์ฌ๋ฌ๊ฐ์) ์ฟผ๋ฆฌ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ์กฐ๊ฑด๋ฌธ์ผ๋ก ์ค.
BoardBean board = new BoardBean();
//๊ฒฐ๊ณผ๊ฐ์ ์ธํ
board.setB_id(rs.getInt(1));//์์ด๋๋ INT์ด๋ฏ๋ก getInt๋ก ํด์ผํจ
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
board.setB_date(rs.getTimestamp(6));
board.setB_hit(rs.getInt(7));//์กฐํ์๋ INT์ด๋ฏ๋ก getInt๋ก ํด์ผํจ
board.setB_pwd(rs.getString(8));
board.setB_ip(rs.getString(9));
board.setB_ref(rs.getInt(10));
board.setB_step(rs.getInt(11));
board.setB_level(rs.getInt(12));
board.setB_fname(rs.getString(13));
board.setB_fsize(rs.getInt(14));
// ๋ฐ์ดํฐ๊ฐ ์ ๋๋ก ๋ค์ด๊ฐ๋์ง ํ์ธ ํด์ผํจ
// System.out.println("@@@@#### board.getB_id()=====>"+board.getB_id());
// System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
// System.out.println("@@@@#### board.getB_pwd()=====>"+board.getB_pwd());
// System.out.println("@@@@####");
boardList.add(board);//๋ฐ๋ณตํ๋ฉด์ ๊ฒ์๊ธ๋ค์ ์์
if (rs.isLast()) {//๊ฒฐ๊ณผ๊ฐ์ด ๋ง์ง๋ง์ด๋ฉด ๋น ์ ธ๋์ค๊ธฐ
break;
} else {
rs.next();//๋ง์ง๋ง์ด ์๋๋ฉด ๊ณ์ ์งํ
}
count++;
}
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//์์๋ฐ๋ฉ(์์ ์ค์)
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return boardList;
}
// 5. ๋ฆฌํดํ์
์ด BoardBean์ธ getBoard() ๋ฉ์๋ ์ถ๊ฐ
public BoardBean getBoard (int bid, boolean hitadd) throws Exception{
//๊ธ๋ฒํธ๋ง ๋๊ธฐ๋ฉด ์์์ ๋ด์ฉ์ด ๋์ด๊ฐ๋ ๋ฉ์๋
//์กฐํ ์ ๋๋ฌธ์ ๋ถ๊ธฐ์ฒ๋ฆฌ ํด๊ฐ
Connection conn = null;//๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ ์
PreparedStatement pstmt = null;// DB์ SQL ์ ๋ฌ
PreparedStatement pstmtup = null;// ์กฐํ์ ์
๋ฐ์ดํธ
ResultSet rs = null;
//db ์ ๋ณด ๋ฐ๊ธฐ ์ํจ
BoardBean board = null;
String sql = "";//ํด๋น ๋ฒํธ์ ๋ฐ์ดํฐ ์ถ๋ ฅ๋๊ฒ ์ฟผ๋ฆฌ ์์ฑ
try {
conn = getConnection();
if (hitadd==true) {
//์กฐํ์ ์
๋ฐ์ดํธ ์ถ๊ฐํจ
sql = "UPDATE BOARDT SET b_hit=b_hit+1 WHERE b_id=?";
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ๋ฐ์
// DB์ SQL ์ ๋ฌ(SQL๋ฌธ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ์ ์ธ
pstmt.setInt(1, bid);//์กฐ๊ฑด์ด ํ๋์ด๊ธฐ ๋๋ฌธ์ (?๊ฐ ํ๋์)
// rs = pstmt.executeQuery();//์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ rs์ ๋ฐ์(select ์กฐํ๋ฌธ์ executeQuery ๋ฉ์๋ ์ฒ๋ฆฌ
pstmt.executeUpdate();//์
๋ฐ์ดํธ๋๊น ์
๋ฐ์ดํธ๋ก ๋ฐ๊ฟ์ค
//pstmtup.close();
//์
๋ฐ์ดํธ ์ถ๊ฐ ๋!
}
sql = "SELECT b_id\r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content\r\n" +
" , b_date\r\n" +
" , b_hit\r\n" +
" , b_pwd\r\n" +
" , b_ip\r\n" +
" , b_ref\r\n" +
" , b_level\r\n" +
" , b_step\r\n" +
" , b_fname\r\n" +
" , b_fsize\r\n" +
" , b_rfname\r\n" +
" FROM BOARDT\r\n" +
" WHERE b_id=?";//ํด๋น ๋ฒํธ์ ๋ฐ์ดํฐ ์ถ๋ ฅ๋๊ฒ ์ฟผ๋ฆฌ ์์ฑ
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ๋ฐ์
// DB์ SQL ์ ๋ฌ(SQL๋ฌธ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ์ ์ธ
pstmt.setInt(1, bid);//์กฐ๊ฑด์ด ํ๋์ด๊ธฐ ๋๋ฌธ์ (?๊ฐ ํ๋์)
rs = pstmt.executeQuery();//select ๋ฌธ์ด๋๊น ์ต์คํํธ ์ฟผ๋ฆฌ
if(rs.next()) {//๋ค์(์ฌ๋ฌ๊ฐ์) ์ฟผ๋ฆฌ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ์กฐ๊ฑด๋ฌธ์ผ๋ก ์ค.
board = new BoardBean();//๊ฐ์ฒด์์ฑ(๋ฆฌํด๊ฐ์ด board)
board.setB_id(rs.getInt(1));
//board.setB_id(bid);๋ ๊ฐ๋ฅํจ
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
board.setB_date(rs.getTimestamp(6));
board.setB_hit(rs.getInt(7));
board.setB_pwd(rs.getString(8));
board.setB_ip(rs.getString(9));
board.setB_ref(rs.getInt(10));//์กฐํ์๋ INT์ด๋ฏ๋ก getInt๋ก ํด์ผํจ
board.setB_step(rs.getInt(11));
board.setB_level(rs.getInt (12));
board.setB_fname(rs.getString(13));
board.setB_fsize(rs.getInt (14));
board.setB_rfname(rs.getString(15));
//
//// ๋ฐ์ดํฐ๊ฐ ์ ๋๋ก ๋ค์ด๊ฐ๋์ง ํ์ธ ํด์ผํจ
System.out.println("@@@@#### board.getB_id()=====>"+board.getB_id());
System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
System.out.println("@@@@#### board.getB_fname()=====>"+board.getB_fname());
System.out.println("@@@@#### board.getB_fsize()=====>"+board.getB_fsize());
System.out.println("@@@@#### board.getB_rfname()=====>"+board.getB_rfname());
//System.out.println("@@@@#### board.setB_pwd()=====>"+board.setB_pwd(sql));
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//์์๋ฐ๋ฉ(์์ ์ค์)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return board;
}
// 6.deleteBoard() ๋ฉ์๋ => ์ญ์ ํ ๊ธ ๋น๋ฐ๋ฒํธ ํ์ธํ๋ ๋ฉ์๋
public int deleteBoard (int b_id, String b_pwd) throws Exception{//๊ธ๋ฒํธ๋ง ๋๊ธฐ๋ฉด ์์์ ๋ด์ฉ์ด ๋์ด๊ฐ๋ ๋ฉ์๋
// id์ pwd๋ฅผ ๋ฐ์ ์ญ์ ํ๋ ๋ฉ์๋
Connection conn = null;//๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ ์
PreparedStatement pstmt = null;// DB์ SQL ์ ๋ฌ
ResultSet rs = null;
int re = -1;
String sql = "";//ํด๋น ๋ฒํธ์ ๋ฐ์ดํฐ ์ถ๋ ฅ๋๊ฒ ์ฟผ๋ฆฌ ์์ฑ
String pwd = "";//๋น๋ฐ๋ฒํธ
try {
conn = getConnection();
sql = "SELECT B_PWD FROM BOARDT where B_ID=?"; // ๊ธ ๋ฒํธ์ ๋ฐ๋ฅธ ๋น๋ฐ๋ฒํธ ๊ฐ์ง๊ณ ์ค๊ธฐ
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ๋ฐ์
pstmt.setInt(1, b_id);//๊ฐ์ ์ง์ด๋ฃ์(์ฟผ๋ฆฌ์ ?๋ถ๋ถ)
rs = pstmt.executeQuery();
if(rs.next()) {//๋ค์(์ฌ๋ฌ๊ฐ์) ์ฟผ๋ฆฌ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ์กฐ๊ฑด๋ฌธ์ผ๋ก ์ค.
pwd = rs.getString(1);
//๋น๋ฐ๋ฒํธ๋ฅผ
if (pwd.equals(b_pwd)) {
sql = "DELETE FROM BOARDT WHERE B_ID=?";
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ์ฟผ๋ฆฌ๋ฅผ ๋ฐ์
pstmt.setInt(1, b_id);
pstmt.executeUpdate();
re = 1;
}else {
re =0;
}
}
} catch(SQLException ex) {
System.out.println("์ญ์ ์คํจ");
ex.printStackTrace();
} finally {
try{//์์๋ฐ๋ฉ(์์ ์ค์)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return re;
}
//7.ํ
์ด๋ธ ์์ editBoard ๋ฉ์๋
public int editBoard (BoardBean board) throws Exception{
int re = -1;
Connection conn = null;//๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ ์
PreparedStatement pstmt = null;// DB์ SQL ์ ๋ฌ
ResultSet rs = null;
String sql ="";
String pwd = "";//๋น๋ฐ๋ฒํธ
try {
conn = getConnection();
sql = "SELECT B_PWD FROM BOARDT where B_ID=?"; // ๊ธ ๋ฒํธ์ ๋ฐ๋ฅธ ๋น๋ฐ๋ฒํธ ๊ฐ์ง๊ณ ์ค๊ธฐ
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ๋ฐ์
pstmt.setInt(1, board.getB_id());//๊ฐ์ ์ง์ด๋ฃ์(์ฟผ๋ฆฌ์ ?๋ถ๋ถ),board ๊ฐ์ฒด๋ฅผ ์ด์ฉํ์ฌ b_id ๊ฐ์ง๊ณ ์ค๊ธฐ
rs = pstmt.executeQuery();
if (rs.next()) {
pwd = rs.getString(1);
if (pwd.equals(board.getB_pwd())) {
sql="UPDATE BOARDT \r\n" +
" SET B_NAME=?\r\n" +
" , B_EMAIL=?\r\n" +
" , B_TITLE=?\r\n" +
" , B_CONTENT=?\r\n" +
" WHERE B_ID=?";
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ์ฟผ๋ฆฌ๋ฅผ ๋ฐ์
pstmt.setString(1,board.getB_name());
pstmt.setString(2,board.getB_email());
pstmt.setString(3,board.getB_title());
pstmt.setString(4,board.getB_content());
pstmt.setInt(5, board.getB_id());
pstmt.executeUpdate();
re = 1;
}else {
re =0;//๋น๋ฐ๋ฒํธ๊ฐ ๊ฐ์ง ์์๊ฒ
}
}
} catch(SQLException ex) {
System.out.println("์์ ์คํจ");
ex.printStackTrace();
} finally {
try{//์์๋ฐ๋ฉ(์์ ์ค์)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return re;
}
//8. ๊ธ๋ฒํธ๋ฅผ ๋๊ฒจ์ b_fname, b_rfname๋ฅผ ์ฌ์ฉํ๋ ๋ฉ์๋
public BoardBean getFileName(int bid) throws Exception{
Connection conn = null;//๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ ์
PreparedStatement pstmt = null;// DB์ SQL ์ ๋ฌ
ResultSet rs = null;
//db ์ ๋ณด ๋ฐ๊ธฐ ์ํจ
BoardBean board = null;
String sql = "SELECT b_fname, b_rfname from BOARDT where b_id=?";
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);//pstmt๊ฐ์ฒด๋ก ๋ฐ์
// DB์ SQL ์ ๋ฌ(SQL๋ฌธ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐธ์กฐ๋ณ์ ์ ์ธ
pstmt.setInt(1, bid);//์กฐ๊ฑด์ด ํ๋์ด๊ธฐ ๋๋ฌธ์ (?๊ฐ ํ๋์)
rs = pstmt.executeQuery();//select ๋ฌธ์ด๋๊น ์ต์คํํธ ์ฟผ๋ฆฌ
if(rs.next()) {//๋ค์(์ฌ๋ฌ๊ฐ์) ์ฟผ๋ฆฌ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ์กฐ๊ฑด๋ฌธ์ผ๋ก ์ค.
board = new BoardBean();//๊ฐ์ฒด์์ฑ(๋ฆฌํด๊ฐ์ด board)
board.setB_fname(rs.getString(1));// ํ์ผ๋ช
board.setB_rfname(rs.getString(2));//์ง์ง ํ์ผ๋ช
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//์์๋ฐ๋ฉ(์์ ์ค์)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return board;
}
}
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String pageNum = request.getParameter("pageNum");//pageNum์ ๋ฐ์
int bid = Integer.parseInt(request.getParameter("b_id"));
BoardDBBean db = BoardDBBean.getInstance();
//BoardBean board = db.getBoard(bid);
BoardBean board = db.getBoard(bid, true);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>๊ธ ๋ด ์ฉ ๋ณด ๊ธฐ</h1>
<table border="1" width="600" cellspacing="0">
<tr height="30" align="center">
<td width="100">๊ธ๋ฒํธ</td>
<td width="200">
<%= bid %>
</td>
<td width="100">์กฐํ์</td>
<td width="200">
<%= board.getB_hit() %>
</td>
</tr>
<tr height="30" align="center">
<td width="100">์์ฑ์</td>
<td width="200">
<%= board.getB_name() %>
</td>
<td width="100">์์ฑ์ผ</td>
<td width="200">
<%-- <%= board.getB_date() %> --%>
<%= sdf.format(board.getB_date()) %>
</td>
</tr>
<tr height="30" align="center">
<td width="110">ํ ์ผ</td>
<td width="200" align="center" colspan="3">
<%--์๋ฃ์ค 3 --%>
<%--
<%
if(board.getB_fname() != null){
%>
<img src="../images/zip.gif">
<a href="../upload/<%= board.getB_fname() %>">
์๋ณธํ์ผ : <%= board.getB_fname() %>
</a>
<%
}
%>
--%>
<%--์๋ฃ์ค 4 --%>
<%
out.print("<p>์ฒจ๋ถํ์ผ"+"<a href='FileDownload.jsp?fileN="+bid+"'>"+board.getB_rfname()+"</a></p>");
%>
</td>
</tr>
<tr height="30" align="center">
<td width="100">๊ธ์ ๋ชฉ</td>
<td width="200" align="left" colspan="3">
<%= board.getB_title() %>
</td>
</tr>
<tr height="30" align="center">
<td width="100">๊ธ๋ด์ฉ</td>
<td width="200" align="left" colspan="3">
<%= board.getB_content() %>
</td>
</tr>
<tr height="30">
<td colspan="4" align="right">
<!-- ํ์ด์ง ๋ชฉ๋ก์ ๋ง์ถฐ์ผ ํด์ ๋ชจ๋ ์ฟผ๋ฆฌ์คํธ๋ง์ ์ฌ์ฉํ์ฌ pageNum=<2%=pageNum%2> ๋ฃ์ด์ผํจ -->
<input type="button" value="๊ธ์์ " onclick="location.href='edit.jsp?b_id=<%= bid %>&pageNum=<%= pageNum %>'">
<input type="button" value="๊ธ์ญ์ " onclick="location.href='delete.jsp?b_id=<%= bid %>&pageNum=<%= pageNum %>'">
<input type="button" value="๋ต๋ณ๊ธ" onclick="location.href='write.jsp?b_id=<%= bid %>&pageNum=<%= pageNum %>'">
<input type="button" value="๊ธ๋ชฉ๋ก" onclick="location.href='list.jsp?pageNum=<%=pageNum%>'">
</td>
</tr>
</table>
</center>
</body>
</html>
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
//๊ธ ๋ฒํธ๋ฅผ ๊ฐ์ง๊ณ ํ์ผ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์ฌ ๊ฑฐ์
int bid = Integer.parseInt(request.getParameter("fileN"));//๊ธ๋ฒํธ
//์ด๋ถ๋ถ ์ค๋ช
๋์นจ ๋ญ๋ง์ด๋ผ๊ณ ?
BoardDBBean db = BoardDBBean.getInstance();
BoardBean board = db.getFileName(bid); //board๊ฐ์ฒด
String fileName="";
String realFileName="";
if(board != null){
fileName = board.getB_fname();
realFileName = board.getB_fname();
}
String saveDirectory = application.getRealPath("/upload");
String path = saveDirectory + File.separator +fileName;
File file = new File(path);//๊ฐ์ฒด ์์ฑ
long length = file.length();//file ๊ฐ์ฒด์์ length์ฌ์ฉ์ ์ฉ๋? ์ด ๋จ
new String(realFileName.getBytes("ms949"),"8859_1");
//๋ฆฌ์ผํ์ผ ๋ค์์ ์ธ์ฝ๋ฉ
response.setContentType("application/octet-stream");
response.setContentLength((int)length);
response.setHeader("Content-Disposition", "attachment;filename=" + realFileName);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
//bis๊ฐ์ฒด๋ฅผ ์ฝ์ด์ ์คํธ๋ง
out.clear();
out = pageContext.pushBody();
//๋จ์ ์๋๊ฑด ์ญ์
//BufferedInputStream bos = new BufferedOutputStream(response.getOutputStream());
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
int data;
while((data=bis.read()) != -1){//๋ผ์ธ์ ๋ฐ์ /EOF
bos.write(data);
}
bis.close();
bos.close();
%>
ํํ ์ธ์ด์ ํ์ํ์
<%=expr%> -> ${expr}
ํํ ์ธ์ด์ ๋ฐ์ดํฐ ํ์
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3>ํํ์ธ์ด์์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฐ์ดํฐ</h3>
<p>[1] ์ ์ํ : <%= 10 %> ====> ${10} </p>
<p>[2] ์ค์ํ : <%= 5.6 %> ====> ${5.6} </p>
<p>[3] ๋ฌธ์์ดํ : <%= "ํ๊ธธ๋" %> ====> ${"ํ๊ธธ๋"} </p>
<p>[3] ๋
ผ๋ฆฌํ : <%= true %> ====> ${true} </p>
</body>
</html>
1. ์ฐ์
+, -, *, /, % (or mod)
2. ๊ด๊ณํ
== (eq), != (ne), < (lt), > (gt), <= (le), >= (ge)
3. ์กฐ๊ฑด
a?b:c (์ผํญ์ฐ์ฐ์)
4. ๋ ผ๋ฆฌ
&&ย (and), || (or), ! (not)
5. ํ๋น์ฑ๊ฒ์ฌ
empty(๋น์์ ธ์๋์ง ํ์ธ)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
pageContext.setAttribute("input", "");
pageContext.setAttribute("input2", "bbb");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3>[ํํ ์ธ์ด์ ์ฐ์ฐ์]</h3>
\${empty ""} ===> ${empty ""}<br><!-- ๋น์์์ true -->
\${empty "aaa"} ===> ${empty "aaa"}<br><!-- ๋น์์์ง ์์ false -->
\${empty input} ===> ${empty input}<br><!-- input์ ๋น์์์ true -->
\${empty input2} ===> ${empty input2}<br><!-- input2์ ๋น์์์ง ์์ false -->
<br>
\${(5>2) ? 5: 2} ===> ${(5>2) ? 5: 2}<br>
<!--์ผํญ์ฐ์ฐ์ -->
\${2 gt 10} ===> ${2 gt 10}<br>
<!-- 2๊ฐ 10๋ณด๋ค ํฌ๋ค. -->
<br>
<!-- ์ฌ์น์ฐ์ฐ -->
\${5+2} ===> ${5+2}<br>
\${5/2} ===> ${5/2}<br>
\${5%2} ===> ${5%2}<br>
\${5 mod 2} ===> ${5 mod 2}<br>
<br>
\${5 > 2} ===> ${5 > 2}<br>
</body>
</html>
JSP์ ์ก์
ํ๊ทธ
<jsp:getProperty name="user" property="firstName"/>
<jsp:getProperty name="user" property="lastName"/>
//๊ฐ๊ฐ ๊ฐ์ ๊ฐ์ง๊ณ ์์ ์ฐธ์กฐ ๋ณ์์ ๊ฐ์ผ๋ก ์๊ฐํจ.(user ๊ฐ์ฒด)
ํํ ์ธ์ด
${user.firstName}
ย
${user.lastName}
//์๋ฐ์์ ๋ฉค๋ฒ๋ณ์๋ก ์ฐธ์กฐํ๋ฏ์ด ์ฌ์ฉ
package glory;
public class User {
private String firstName="๊ธธ๋";
private String lastName="ํ";
//getter,setter ์ถ๊ฐ
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
//getter,setter ์ถ๊ฐ ๋!
}
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3>[์ก์
ํ๊ทธ์ ํํ ์ธ์ด]</h3>
<jsp:useBean class="glory.User" id="user"></jsp:useBean>
<jsp:getProperty property="firstName" name="user"/>
<jsp:getProperty property="lastName" name="user"/>
<hr>
${user.firstName}
${user.lastName}
<hr>
<jsp:setProperty property="firstName" name="user" value="Gildong"/>
<jsp:setProperty property="lastName" name="user" value="Hong"/>
<jsp:getProperty property="firstName" name="user"/>
<jsp:getProperty property="lastName" name="user"/>
<hr>
${user.firstName}
${user.lastName}
</body>
</html>
๐requestScope
request ๊ธฐ๋ณธ ๊ฐ์ฒด์ ์ ์ฅ๋ ์์ฑ์ <์์ฑ, ๊ฐ> ๋งคํ์ ์ ์ฅํ Map ๊ฐ์ฒด
๐sessionScope
session ๊ธฐ๋ณธ ๊ฐ์ฒด์ ์ ์ฅ๋ ์์ฑ์ <์์ฑ, ๊ฐ> ๋งคํ์ ์ ์ฅํ Map ๊ฐ์ฒด
๐param
์์ฒญ ํ๋ผ๋ฏธํฐ์ <ํ๋ผ๋ฏธํฐ ์ด๋ฆ, ๊ฐ> ๋งคํ์ ์ ์ฅํ๋ Map ๊ฐ์ฒด, ํ๋ผ๋ฏธํฐ ๊ฐ์ ํ์
์ String์ผ๋ก์, request.getParameter(์ด๋ฆ)์ ๊ฒฐ๊ณผ์ ๋์ผํ๋ค.
๐cookie
<์ฟ ํค ์ด๋ฆ, Cookie> ๋งคํ์ ์ ์ฅํ๋ Map ๊ฐ์ฒด, request.getCookies()๋ก ๊ตฌํ Cookie ๋ฐฐ์ด๋ก๋ถํฐ ๋งคํ์ ์์ฑํ๋ค.
ํํ ์ธ์ด๋ย ๋ท(dot) ์ฐ์ฐ์(.)์ ๋ธ๋ผ์ผ ์ฐ์ฐ์([]) ๋ ๊ฐ์ ์ ๊ทผ์๋ฅผ ์ ๊ณตํ๋ค.
๋ท ์ฐ์ฐ์๋ ๊ฐ์ฒด์ ์์ฑ์ ์ ๊ทผํ๋๋ฐ ์ฌ์ฉ๋๋ค.
${[requestScope.name]
๋ธ๋ผ์ผ ์ฐ์ฐ์์์ ๊ฐ์ธ ์์ฑ์ ์ด๋ฆ์ ํฐ๋ฐ์ดํ(") ํน์ ์์๋ฐ์ดํ(')๋ก ๋๋ฌ์ธ์ผ ํ๋ค.
${requestScope["name"]}
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
request.setAttribute("name", "ํ๊ธธ๋");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3>JSP ํํ </h3>
<%= request.getAttribute("name") %>
<hr>
<h3>[ํํ ์ธ์ด์ ๋ด์ฅ ๊ฐ์ฒด์ ์ ๊ทผ์]</h3>
<h4>๋ท ์ฐ์ฐ์๋ก ์ ๊ทผ</h4>
${requestScope.name}
<h4>๋ธ๋ผ์ผ ์ฐ์ฐ์๋ก ์ ๊ทผ</h4>
${requestScope["name"]}
<br>
${requestScope['name']}
</body>
</html>
JSTL( JSP Standard Tag Library)
์ฌ์ฉ์ ์ ์ ํ๊ทธ์ ํ์ค
if, for, while, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฒ๋ฆฌ
๋ด์ฅ ๊ฐ์ฒด, ํ๋ผ๋ฏธํฐ, ํค๋, ์ฟ ํค ์ฝ๊ฒ ์ฌ์ฉ
๊ฐ์ฒด ๊ฐ์ ๋น๊ต๋ฅผ ==์ ๊ฐ์ ๊ฐ๋จํ ์ฐ์ฐ์๋ก ๊ตฌํ
JSTL์ด ์ ๊ณตํ๋ ํ๊ทธ์ ์ข
๋ฅ
JSTL์ด ์ ๊ณตํ๋ ํ๊ทธ ์ฌ์ฉ
JSP ํ์ด์ง์ <%@ taglib> ๋๋ ํฐ๋ธ ํ๊ทธ๋ฅผ ์ฌ์ฉ
jstl.jar ํ์ผ์ด ํ์
Jstl jar ํ์ผ์ด ์์๋
Core ํ๊ทธ์ ์ข ๋ฅ
https://mvnrepository.com/artifact/javax.servlet/jstl/1.2
์ ์ ์ํด์ jar ๋ค์ด๋ฐ๊ธฐ
๊ทธํ ์ฌ์ง๊ณผ ๊ฐ์ด jarํ์ผ ๋ฃ๊ธฐ
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
id ๋ณ์๊ฐ ์ค์
<c:set var="id" value="aaa"></c:set><br>
<c:out value="${id}"></c:out>
<br><br>
id ๋ณ์๊ฐ ์ญ์
<c:remove var="id"/><br>
<c:out value="${id}"></c:out>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="core02_process.jsp">
<p>
์ซ์ : <input type="text" name="number">
</p>
<p>
<input type="submit" value="์ ์ก">
</p>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String number = request.getParameter("number");
%>
<c:set var="number" value="<%= number %>"></c:set>
<c:choose>
<c:when test="${number % 2 == 0 }">
<c:out value="${number}"></c:out>์ ์ง์์
๋๋ค.
</c:when>
<c:when test="${number % 2 == 1 }">
<c:out value="${number}"></c:out>์ ํ์์
๋๋ค.
</c:when>
</c:choose>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h3>๊ตฌ๊ตฌ๋จ</h3>
<table>
<c:forEach var="i" begin="1" end="9">
<tr>
<c:forEach var="j" begin="1" end="9">
<td width="100">
${i}*${j}=${i*j}
</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>
2.2 SQL ํ๊ทธ
SQL ํ๊ทธ์ ์ข
๋ฅ
2.3 Functions ํ๊ทธ
Functions ํ๊ทธ์ ์ข
๋ฅ
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<p>java ๋ฌธ์์ด ๊ฒ์</p>
<p>Hello, Java Server Pages! => ${fn:contains("Hello, Java Server Pages!","java")}</p>
<p>Hello, Java Server Pages! => ${fn:contains("Hello, Java Server Pages!","Java")}</p>
<!-- ๋์๋ฌธ์ ๊ตฌ๋ณํจ -->
<br><br>
<p>Hello, Java Server Pages! => ${fn:containsIgnoreCase("Hello, Java Server Pages!","java")}</p>
<p>Hello, Java Server Pages! => ${fn:containsIgnoreCase("Hello, Java Server Pages!","Java")}</p>
<!-- ๋์๋ฌธ์ ๊ตฌ๋ณํ์ง ์์ -->
</body>
</html>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<c:set var="texts" value="${fn:split('Hello, Java Server Pages!',' ')}"></c:set>
<c:forEach var="i" begin="0" end="${fn:length(texts) - 1}">
<p>
texts[${i}] = ${texts[i]}
</p>
</c:forEach>
<p>
<c:out value="${fn:join(texts,'-')}"></c:out>
</p>
</body>
</html>