create table pds1(
seq number not null primary key,
grp number not null,
grps number not null,
grpl number not null,
subject varchar2(100) not null,
writer varchar2(12) not null,
mail varchar2(50),
password varchar2(12) not null,
content varchar2(2000),
upload varchar2(200),
hit number not null,
wip varchar2(15) not null,
wdate date not null
);
2.board_write1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String cpage = request.getParameter("cpage");
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
<script type="text/javascript">
function ChkForm(form) {
if(form.value.trim() == "") {
alert("제목을 입력하셔야 합니다.");
return false;
}
if(form.writer.value.trim() == "") {
alert("작성자를 입력하셔야 합니다.");
return false;
}
if(form.password.value.trim() == "") {
alert("비밀번호를 입력하셔야 합니다.");
return false;
}
}
</script>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<!-- form 파일 업로드 속성 적어줌 -->
<form action='./board_write1_ok.jsp' method='post' name='wfrm' onSubmit='return ChkForm(this)' enctype="multipart/form-data">
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>제목</font> :
</td>
<td>
<input type='text' name='subject' size='80' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>작성자</font> :
</td>
<td>
이름 <input type='text' name='writer' size='10' maxlength='10' class='form'> /
메일 <input type='text' name='mail' size='40' maxlength='70' class='form'> /
암호 <input type='password' name='password' size='10' maxlength='10' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>내용</font> :
</td>
<td>
<textarea name='content' style='width:620;height:300' class='form'></textarea>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>파일</font> :
</td>
<td>
<!-- 업로드 버튼 -->
<input type='file' name='upload' size='84'>
</td>
</tr>
<tr>
<td colspan='2' class='gline'></td>
</tr>
</table>
<table width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='500' height='30'> </td>
<td align='right'>
<input type='image' src='../images/btn_wri.gif' border='0'>
<a href='board_list1.jsp?cpage=<%=cpage %>'><img src='../images/btn_list.gif' border='0'></a>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
3.board_write1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%
//업로드
String uploadPath = "C:\\Users\\user\\Documents\\BoardEx01\\WebContent\\upload";
int maxFileSize = 1024 * 1024 * 20;
String encoding = "utf-8";
MultipartRequest multi = new MultipartRequest(request, uploadPath, maxFileSize, encoding, new DefaultFileRenamePolicy());
//업로드파일, 클라이언트 정보 받기 multi
String subject = multi.getParameter("subject");
String writer = multi.getParameter("writer");
String mail = multi.getParameter("mail");
String password = multi.getParameter("password");
String content = multi.getParameter("content");
String upload = multi.getFilesystemName("upload") == null ? "" : multi.getFilesystemName("upload");
//클라이언트 정보 받기 request
String wip = request.getRemoteAddr();
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 1;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
conn = dataSource.getConnection();
String sql = "insert into pds1 values (board_seq.nextval, board_seq.currval, 0, 0, ?, ?, ?, ?, ?, ?, 0, ?, sysdate)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, subject);
pstmt.setString(2, writer);
pstmt.setString(3, mail);
pstmt.setString(4, password);
pstmt.setString(5, content);
pstmt.setString(6, upload) ;
pstmt.setString(7, wip);
int result = pstmt.executeUpdate();
if(result == 1) {
flag = 0;
}
} catch(NamingException e) {
System.out.println("에러 : " + e.getMessage());
} catch(SQLException e) {
System.out.println("에러 : " + e.getMessage());
} finally {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
out.println("<script type='text/javascript'>");
if(flag == 0) {
out.println("alert('글쓰기에 성공했습니다.');");
out.println("location.href='./board_list1.jsp';");
} else {
out.println("alert('글쓰기에 실패했습니다.');");
out.println("history.back();");
}
out.println("</script>");
%>
4.board_list1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%
request.setCharacterEncoding("utf-8");
int cpage = 1;
if(request.getParameter("cpage") != null && !request.getParameter("cpage").equals("")) {
cpage = Integer.parseInt(request.getParameter("cpage"));
}
int recordPerPage = 10;
int totalRecord = 0;
int totalPage = 1;
int blockPerPage = 5;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer result = new StringBuffer();
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
conn = dataSource.getConnection();
String sql = "select seq, grpl, subject, writer, upload, to_char(wdate, 'YY.MM.DD') wdate, hit, trunc(sysdate-wdate) wgap from pds1 order by grp desc, grps asc";
pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = pstmt.executeQuery();
rs.last();
totalRecord = rs.getRow();
rs.beforeFirst();
totalPage = (totalRecord > 0 && totalRecord % recordPerPage == 0) ? (totalRecord / recordPerPage) : (totalRecord / recordPerPage) + 1;
int skip = (cpage - 1) * recordPerPage;
if(skip != 0) rs.absolute(skip);
for(int i=0 ; i<recordPerPage && rs.next() ; i++) {
String seq = rs.getString("seq");
int grpl = rs.getInt("grpl");
String sgrpl = "";
for(int j=1 ; j<=grpl ; j++) {
sgrpl += " ";
}
String subject = rs.getString("subject");
String writer = rs.getString("writer");
String upload = rs.getString("upload") == null ? "" : rs.getString("upload");
String wdate = rs.getString("wdate");
String hit = rs.getString("hit");
int wgap = rs.getInt("wgap");
result.append("<table width='100%' border='0' cellpadding='0' cellspacing='0'>");
result.append("<tr>");
result.append(" <td height='1'></td>");
result.append("</tr>");
result.append("<tr>");
result.append(" <td>");
result.append(" <table width='100%' border='0' cellpadding='0' cellspacing='0'>");
result.append(" <tr height='25' onMouseOver=\"this.className='evencell'\" onMouseOut=\"this.className=''\">");
result.append(" <td width='40' align='center'>" + seq + "</td>");
result.append(" <td>");
result.append(" <span style='width:370' class='elltxt'>");
//답글 들여쓰기
if(grpl != 0) {
result.append(sgrpl + " <img src='../images/ico_re.gif' width='9' />");
}
//삭제된 게시글
if(subject.equals("사용자에 의해 삭제되었습니다.")) {
result.append( subject);
} else {
result.append(" <a href='board_view1.jsp?cpage=" + cpage + "&seq=" + seq + "'>" + subject + "</a>");
}
//new이미지 24시간
if(wgap == 0) {
result.append(" <img src='../images/ico_n.gif' width='8' height='8' border='0' hspace='3'>");
}
//첨부파일 이미지
if(!upload.equals("")) {
result.append("<img src='../images/ico_file.gif' width='10' height='10' border='0' hspace='3'>");
}
result.append(" </span>");
result.append(" </td>");
result.append(" <td width='100' align='center'>" + writer + "</td>");
result.append(" <td width='80' align='center'>" + wdate + "</td>");
result.append(" <td width='50' align='center'>" + hit + "</td>");
result.append(" </tr>");
result.append(" </table>");
result.append(" </td>");
result.append("</tr>");
result.append("<tr>");
result.append(" <td height='1'></td>");
result.append("</tr>");
result.append("<tr>");
result.append(" <td align='center' class='imgline'></td>");
result.append("</tr>");
result.append("</table>");
}
} catch(NamingException e) {
System.out.println("에러 : " + e.getMessage());
} catch(SQLException e) {
System.out.println("에러 : " + e.getMessage());
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td align='center' class='gline'></td>
</tr>
<tr>
<td align='center'>
<table width='100%' border='0' cellpadding='0' cellspacing='0' class='titlecell'>
<tr height='25' align='center'>
<td width='40'>No</td>
<td>제목</td>
<td width='100'>이름</td>
<td width='80'>등록일</td>
<td width='50'>조회수</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align='center' class='gline'></td>
</tr>
<tr>
<td height='3'></td>
</tr>
</table>
<!-- 리스트 -->
<%=result %>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='500' height='30'>
<!-- 페이지 네비게이션 -->
<%
//화면 하단에 보이는 페이지 리스트 갯수
int startBlock = ((cpage - 1) / blockPerPage) * blockPerPage + 1;
int endBlock = ((cpage - 1) / blockPerPage) * blockPerPage + blockPerPage;
if (endBlock >= totalPage) {
endBlock = totalPage;
}
//블럭 옮기기
if(startBlock == 1) {
out.println("<img src='../images/moveset/p_left_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'>");
} else {
out.println("<a href='board_list1.jsp?cpage=" + (startBlock - blockPerPage) + "'><img src='../images/moveset/p_left_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'></a>");
}
//페이지 옮기기 //page가 1일때는 이전페이지로 못감, 화살표 색깔off로 바뀜
if(cpage == 1) {
out.println("<img src='../images/moveset/p_left_arrow_02_off.gif' align='absmiddle' hspace='8' border='0'>");
} else {
out.println("<a href='board_list1.jsp?cpage=" + (cpage - 1) + "'><img src='../images/moveset/p_left_arrow_02.gif' align='absmiddle' hspace='8' border='0'></a>");
}
out.println("<img src='../images/moveset/p_orange.gif' align='absmiddle' hspace='3' border='0'>");
for(int i=startBlock ; i<=endBlock ; i++) {
if (cpage == i) {
out.println("<b><u>[" + i + "]</u></b> ");
} else {
out.println("<a href='board_list1.jsp?cpage=" + i + "'>" + i + "</a> ");
}
}
out.println("<img src='../images/moveset/p_orange.gif' align='absmiddle' hspace='3' border='0'>");
//페이지 옮기기 //page가 끝페이지일때 다음 페이지로 못감, 화살표 색깔off로 바뀜
if(cpage == totalPage) {
out.println("<img src='../images/moveset/p_right_arrow_02_off.gif' align='absmiddle' hspace='8' border='0'>");
} else {
out.println("<a href='board_list1.jsp?cpage=" + (cpage + 1) + "'><img src='../images/moveset/p_right_arrow_02.gif' align='absmiddle' hspace='8' border='0'></a>");
}
//블럭 옮기기
if(endBlock == totalPage) {
out.println("<img src='../images/moveset/p_right_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'>");
} else {
out.println("<a href='board_list1.jsp?cpage=" + (startBlock + blockPerPage) + "'><img src='../images/moveset/p_right_arrow_01_off.gif' align='absmiddle' hspace='2' border='0'></a>");
}
%>
<!-- 페이지 네비게이션 끝 -->
</td>
<td align='right'>
<a href='board_write1.jsp?cpage=<%=cpage %>'><img src='../images/btn_wri.gif' border='0'></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
5.board_view1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%
request.setCharacterEncoding("utf-8");
//선택한 글의 seq 받아오기
String cpage = request.getParameter("cpage");
String seq = request.getParameter("seq");
String subject = "";
String writer = "";
String mail = "";
String wip = "";
String wdate = "";
String hit = "";
String content = "";
String uploadPath = "../upload/";
String uploadRealPath = "C:\\Users\\user\\Documents\\BoardEx01\\WebContent\\upload\\";
String uploadInfo = "";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
conn = dataSource.getConnection();
//조회수 증가
String sql = "update pds1 set hit=hit+1 where seq=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
pstmt.executeUpdate();
pstmt.close();
//view창 출력
sql = "select subject, writer, mail, wip, wdate, hit, content, upload from pds1 where seq = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
if(rs.next()) {
subject = rs.getString("subject");
writer = rs.getString("writer");
mail = rs.getString("mail") == null ? "" : rs.getString("mail") ;
wip = rs.getString("wip");
wdate = rs.getString("wdate");
hit = rs.getString("hit");
content = rs.getString("content") == null ? "" : rs.getString("content").replaceAll("\n", "<br />");
String upload = rs.getString("upload") == null ? "" : rs.getString("upload");
//파일사이즈 검색
if(!upload.equals("")) {
String fileSize = "(" + new File(uploadRealPath + upload).length() + "Bytes)";
//uploadInfo = "<a href='" + uploadPath + upload + "'>" + upload + "</a>" + fileSize;
uploadInfo = "<a href='download.jsp?filename=" + upload + "'>" + upload + "</a>" + fileSize;
}
}
} catch(NamingException e) {
System.out.println("에러 : " + e.getMessage());
} catch(SQLException e) {
System.out.println("에러 : " + e.getMessage());
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<table width='100%' cellpadding='0' cellspacing='0' border='0' align='center'>
<tr>
<td height='25'><font class='titdot'>• </font><font class='title'>제목</font> | <%=subject %></td>
</tr>
<tr>
<td class='imgline'></td>
</tr>
<tr>
<td height='25'><font class='titdot'>• </font><font class='title'>이름</font> | <%=writer %>(<%=mail %>) (<%=wip %>)</td>
</tr>
<tr>
<td class='imgline'></td>
</tr>
<tr>
<td height='25'>
<table width='100%' cellpadding='0' cellspacing='0' border='0' align='center'>
<tr>
<td><font class='titdot'>•</font><font class='title'> 날짜</font> | <%=wdate %></td>
<td align='right'><font class='titdot'>• </font><font class='title'>조회</font> | <%=hit %></td>
</tr>
</table>
</td>
</tr>
<tr>
<td class='imgline'></td>
</tr>
<tr>
<td height='25'>
<font class='titdot'>• </font><font class='title'>파일</font> | <%=uploadInfo %>
</td>
</tr>
<tr>
<td class='imgline'></td>
</tr>
<tr>
<td height='100'><%=content %></td>
</tr>
</table>
<table width='100%' border='0' cellspacing='0' cellpadding='0' align='center'>
<tr>
<td id='tailarea'></td>
</tr>
</table>
<table width='100%' border='0' cellspacing='1' cellpadding='1'>
<tr>
<td colspan='2' class='gline'></td>
</tr>
<tr>
<td width='500' height='30'>
<a href='board_write1.jsp?cpage=<%=cpage %>'><img src='../images/btn_wri.gif' border='0'></a>
<a href='board_modify1.jsp?cpage=<%=cpage %>&seq=<%=seq%>'><img src='../images/btn_mod.gif' border='0'></a>
<a href='board_delete1.jsp?cpage=<%=cpage %>&seq=<%=seq%>'><img src='../images/btn_del.gif' border='0'></a>
<a href='board_reply1.jsp?cpage=<%=cpage %>&seq=<%=seq%>'><img src='../images/btn_rep.gif' border='0'></a>
</td>
<td align='right'>
<a href='board_list1.jsp?cpage=<%=cpage %>'><img src='../images/btn_list.gif' border='0'></a>
</td>
</tr>
<tr>
<td colspan='2' class='gline'></td>
</tr>
</table>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
6.download
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="com.oreilly.servlet.ServletUtils" %>
<%
String fileName = request.getParameter("filename");
String sFilePath = "C:\\Users\\user\\Documents\\BoardEx01\\WebContent\\upload\\" + fileName;
byte b[] = new byte[4096];
File oFile = new File(sFilePath);
out.clearBuffer();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
FileInputStream in = new FileInputStream(sFilePath);
ServletOutputStream sout = response.getOutputStream();
int numRead;
while((numRead = in.read(b, 0, b.length)) != -1) {
sout.write(b, 0, numRead);
}
sout.flush();
sout.close();
in.close();
%>
7.board_delete1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%
request.setCharacterEncoding("utf-8");
String cpage = request.getParameter("cpage");
String seq = request.getParameter("seq");
String subject = "";
String writer = "";
String mail = "";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
conn = dataSource.getConnection();
String sql = "select subject, writer, mail from pds1 where seq = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
if(rs.next()) {
subject = rs.getString("subject");
writer = rs.getString("writer");
mail = rs.getString("mail") == null ? "" : rs.getString("mail") ;
}
} catch(NamingException e) {
System.out.println("에러 : " + e.getMessage());
} catch(SQLException e) {
System.out.println("에러 : " + e.getMessage());
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
<script type="text/javascript">
function ChkForm(form) {
if(document.wfrm.password.value.trim() == "") {
alert('비밀번호를 입력하세요.');
return false;
}
}
</script>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<form action='./board_delete1_ok.jsp' method='post' name='wfrm' onSubmit='return ChkForm(this)'>
<input type='hidden' name='seq' value='<%=seq %>' />
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>제목</font> :
</td>
<td>
<input type='text' name='subject' value='<%=subject %>' size='80' class='form' readonly>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>작성자</font> :
</td>
<td>
이름 <input type='text' name='writer' value='<%=writer %>' size='10' maxlength='10' class='form' readonly> /
메일 <input type='text' name='mail' value='<%=mail %>' size='40' maxlength='70' class='form' readonly> /
암호 <input type='password' name='password' size='10' maxlength='10' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='gline'></td>
</tr>
</table>
<table width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='500' height='30'> </td>
<td align='right'>
<input type='image' src='../images/btn_del.gif' border='0'>
<a href='board_list1.jsp?cpage=<%=cpage %>'><img src='../images/btn_list.gif' border='0'></a>
<a href='javascript:history.back();'><img src='../images/btn_view.gif' border='0'></a>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
8.board_delete1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.File" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%
request.setCharacterEncoding("utf-8");
//클라이언트 정보 받기
String seq = request.getParameter("seq");
String password = request.getParameter("password");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int flag = 2;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
conn = dataSource.getConnection();
String sql = "select upload from pds1 where seq=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
String upload = "";
if(rs.next()) {
upload = rs.getString("upload");
}
//삭제가 아니라 수정
sql = "update pds1 set subject='사용자에 의해 삭제되었습니다.', upload='', content='' where seq=? and password=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
pstmt.setString(2, password);
int result = pstmt.executeUpdate();
if(result == 0) {
flag = 1;
} else if(result == 1) {
flag = 0;
//파일 삭제
File f = new File("C:\\Users\\user\\Documents\\BoardEx01\\WebContent\\upload", upload);
f.delete();
}
} catch(NamingException e) {
System.out.println("에러 : " + e.getMessage());
} catch(SQLException e) {
System.out.println("에러 : " + e.getMessage());
} finally {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
//성공시 리스트페이지 , 실패시 이전페이지
out.println("<script type='text/javascript'>");
if(flag == 0) {
out.println("alert('글삭제에 성공했습니다.');");
out.println("location.href='./board_list1.jsp';");
} else if(flag == 1) {
out.println("alert('비밀번호가 잘못되었습니다.');");
out.println("history.back();");
} else {
out.println("alert('글삭제에 실패했습니다.');");
out.println("history.back();");
}
out.println("</script>");
%>
9.board_modify1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%
request.setCharacterEncoding("utf-8");
//선택한 글의 정보
String cpage = request.getParameter("cpage");
String seq = request.getParameter("seq");
String subject = "";
String writer = "";
String mail = "";
String content = "";
String upload = "";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
conn = dataSource.getConnection();
//modify창 출력
String sql = "select subject, writer, mail, content, upload from pds1 where seq = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
if(rs.next()) {
subject = rs.getString("subject");
writer = rs.getString("writer");
mail = rs.getString("mail") == null ? "" : rs.getString("mail");
content = rs.getString("content") == null ? "" : rs.getString("content") ;
upload = rs.getString("upload") == null ? "" : rs.getString("upload") ;
}
} catch(NamingException e) {
System.out.println("에러 : " + e.getMessage());
} catch(SQLException e) {
System.out.println("에러 : " + e.getMessage());
} finally {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
<script type="text/javascript">
function ChkForm(form) {
if(form.subject.value.trim() == "") {
alert("제목을 입력하셔야 합니다");
return false;
}
if(form.password.value.trim() == "") {
alert("비밀번호를 입력하셔야 합니다");
return false;
}
}
</script>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<%-- 파일 보내기 form --%>
<form action='./board_modify1_ok.jsp' method='post' name='wfrm' onSubmit='return ChkForm(this)' enctype="multipart/form-data">
<input type='hidden' name='cpage' value='<%=cpage %>' /> <%-- cpage를 히든으로 보냄 --%>
<input type='hidden' name='seq' value='<%=seq %>' /> <%-- seq를 히든으로 보냄 --%>
<input type='hidden' name='oldupload' value='<%=upload %>' /> <%-- upload를 히든으로 보냄 --%>
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>제목</font> :
</td>
<td>
<input type='text' name='subject' value='<%=subject %>' size='80' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>작성자</font> :
</td>
<td>
이름 <input type='text' name='writer' value='<%=writer %>' size='10' maxlength='10' class='form' readonly> /
메일 <input type='text' name='mail' value='<%=mail %>' size='40' maxlength='70' class='form'> /
암호 <input type='password' name='password' size='10' maxlength='10' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>내용</font> :
</td>
<td>
<textarea name='content' style='width:620;height:300' class='form'><%=content %></textarea>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>첨부</font> :
</td>
<td>
<%=upload %>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>파일</font> :
</td>
<td>
<input type='file' name='upload' size='84'>
</td>
</tr>
<tr>
<td colspan='2' class='gline'></td>
</tr>
</table>
<table width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='500' height='30'> </td>
<td align='right'>
<input type='image' src='../images/btn_mod.gif' border='0'>
<a href='board_list1.jsp?cpage=<%=cpage %>'><img src='../images/btn_list.gif' border='0'></a>
<a href='javascript:history.back();'><img src='../images/btn_view.gif' border='0'></a>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
10.board_modify1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.File"%>
<%@ page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%
//업로드
String uploadPath = "C:\\Users\\user\\Documents\\BoardEx01\\WebContent\\upload";
int maxFileSize = 1024 * 1024 * 20;
String encoding = "utf-8";
MultipartRequest multi = new MultipartRequest(request, uploadPath, maxFileSize, encoding, new DefaultFileRenamePolicy());
//업로드파일, 클라이언트 정보 받기
String cpage = multi.getParameter("cpage");
String seq = multi.getParameter("seq");
String subject = multi.getParameter("subject");
String mail = multi.getParameter("mail");
String password = multi.getParameter("password");
String content = multi.getParameter("content");
String oldupload = multi.getParameter("oldupload");
String upload = multi.getFilesystemName("upload") == null ? "" : multi.getFilesystemName("upload");
//데이터베이스 연결
Connection conn = null;
PreparedStatement pstmt = null;
int flag = 2; //성공0, 비번다름 1, 연결실패2
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
conn = dataSource.getConnection();
String sql = "";
//업로드파일 있을때만 업로드 수정
if(upload.equals("")) {
sql = "update pds1 set subject=?, mail=?, content=? where seq=? and password=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, subject);
pstmt.setString(2, mail);
pstmt.setString(3, content);
pstmt.setString(4, seq);
pstmt.setString(5, password);
} else {
sql = "update pds1 set subject=?, mail=?, content=?, upload=? where seq=? and password=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, subject);
pstmt.setString(2, mail);
pstmt.setString(3, content);
pstmt.setString(4, upload);
pstmt.setString(5, seq);
pstmt.setString(6, password);
}
int result = pstmt.executeUpdate();
if(result == 0) {
flag = 1;
} else if(result == 1) {
//원래 파일 삭제
if(!upload.equals("")) {
File f = new File("C:\\Users\\user\\Documents\\BoardEx01\\WebContent\\upload", oldupload);
f.delete();
}
flag = 0;
}
} catch(NamingException e) {
System.out.println("에러 : " + e.getMessage());
} catch(SQLException e) {
System.out.println("에러 : " + e.getMessage());
} finally {
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
//성공시 리스트페이지, 실패시 이전페이지
out.println("<script type='text/javascript'>");
if(flag == 0) {
out.println("alert('글수정에 성공했습니다.');");
out.println("location.href='./board_view1.jsp?cpage=" + cpage + "&seq=" + seq + "';");
} else if(flag == 1) {
out.println("alert('비밀번호가 잘못되었습니다.');");
out.println("history.back();");
} else {
out.println("alert('글수정에 실패했습니다.');");
out.println("history.back();");
}
out.println("</script>");
%>
11.board_reply1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String cpage = request.getParameter("cpage");
String seq = request.getParameter("seq");
%>
<html>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<link rel='stylesheet' type='text/css' href='../images/common.css'>
<script type="text/javascript">
function ChkForm(form) {
if(form.value.trim() == "") {
alert("제목을 입력하셔야 합니다.");
return false;
}
if(form.writer.value.trim() == "") {
alert("작성자를 입력하셔야 합니다.");
return false;
}
if(form.password.value.trim() == "") {
alert("비밀번호를 입력하셔야 합니다.");
return false;
}
}
</script>
</head>
<body bgcolor='#ffffff' topmargin='5' rightmargin='0' leftmargin='5' bottommargin='10'>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='23' bgcolor='#f0f0f0' align='right'></td>
</tr>
<tr>
<td bgcolor='#ffffff' style='padding:20'>
<form action='./board_reply1_ok.jsp' method='post' name='wfrm' onSubmit='return ChkForm(this)' enctype="multipart/form-data">
<input type='hidden' name='cpage' value='<%=cpage %>' />
<input type='hidden' name='seq' value='<%=seq %>' />
<table width='100%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>제목</font> :
</td>
<td>
<input type='text' name='subject' size='80' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>작성자</font> :
</td>
<td>
이름 <input type='text' name='writer' size='10' maxlength='10' class='form'> /
메일 <input type='text' name='mail' size='40' maxlength='70' class='form'> /
암호 <input type='password' name='password' size='10' maxlength='10' class='form'>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>내용</font> :
</td>
<td>
<textarea name='content' style='width:620;height:300' class='form'></textarea>
</td>
</tr>
<tr>
<td colspan='2' class='imgline'></td>
</tr>
<tr>
<td width='70' style='padding:5' valign='top' align='right'>
<font class='titdot'>• </font>
<font class='title'>파일</font> :
</td>
<td>
<input type='file' name='upload' size='84'>
</td>
</tr>
<tr>
<td colspan='2' class='gline'></td>
</tr>
</table>
<table width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td width='500' height='30'> </td>
<td align='right'>
<input type='image' src='../images/btn_rep.gif' border='0'>
<a href='board_list1.jsp?cpage=<%=cpage %>'><img src='../images/btn_list.gif' border='0'></a>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<table width='750px' border='0' cellpadding='0' cellspacing='0' align='center'>
<tr>
<td height='15' bgcolor='#f0f0f0' style='padding:5' align='center'></td>
</tr>
</table>
</body>
</html>
12.board_reply1_ok
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.NamingException" %>
<%@ page import="javax.sql.DataSource" %>
<%
//업로드
String uploadPath = "C:\\Users\\user\\Documents\\BoardEx01\\WebContent\\upload";
int maxFileSize = 1024 * 1024 * 20;
String encoding = "utf-8";
MultipartRequest multi = new MultipartRequest(request, uploadPath, maxFileSize, encoding, new DefaultFileRenamePolicy());
//업로드파일, 클라이언트 정보 multi
String cpage = multi.getParameter("cpage");
String seq = multi.getParameter("seq");
String subject = multi.getParameter("subject");
String writer = multi.getParameter("writer");
String mail = multi.getParameter("mail");
String password = multi.getParameter("password");
String content = multi.getParameter("content");
String upload = multi.getFilesystemName("upload") == null ? "" : multi.getFilesystemName("upload");
//클라이언트 정보 request
String wip = request.getRemoteAddr();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
int flag = 1; //데이터베이스 입력 성공실패 여부 성공0, 실패 1
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource dataSource = (DataSource)envCtx.lookup("jdbc/oracle");
conn = dataSource.getConnection();
//부모글의 group 정보
String sql = "select grp, grps, grpl from pds1 where seq=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, seq);
rs = pstmt.executeQuery();
int pgrp = 0;
int pgrps = 0;
int pgrpl = 0;
if(rs.next()) {
pgrp = rs.getInt("grp");
pgrps = rs.getInt("grps");
pgrpl = rs.getInt("grpl");
}
//먼저 적힌 답글 grps 증가
sql = "update pds1 set grps = grps+1 where grp=? and grps>?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, pgrp);
pstmt.setInt(2, pgrps);
pstmt.executeUpdate();
//답글 DB 입력
sql = "insert into pds1 values (board_seq.nextval, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, sysdate)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, pgrp);
pstmt.setInt(2, pgrps + 1);
pstmt.setInt(3, pgrpl + 1);
pstmt.setString(4, subject);
pstmt.setString(5, writer);
pstmt.setString(6, mail);
pstmt.setString(7, password);
pstmt.setString(8, content);
pstmt.setString(9, upload);
pstmt.setString(10, wip);
//executeUpdate 리턴 값: 영향 받은 행수
int result = pstmt.executeUpdate();
if(result == 1) {
flag = 0;
}
} catch(NamingException e) {
System.out.println("에러 : " + e.getMessage());
} catch(SQLException e) {
System.out.println("에러 : " + e.getMessage());
} finally {
if(rs != null) rs.close();
if(pstmt != null) pstmt.close();
if(conn != null) conn.close();
}
//성공시 리스트, 실패시 이전페이지
out.println("<script type='text/javascript'>");
if(flag == 0) {
out.println("alert('답글쓰기에 성공했습니다.');");
out.println("location.href='./board_view1.jsp?cpage=" + cpage + "&seq=" + seq + "';");
} else {
out.println("alert('답글쓰기에 실패했습니다.');");
out.println("history.back();");
}
out.println("</script>");
%>
13. include directive방법
두개의 파일이 하나의 클래스로 합쳐지는 방식
문법적으로 완성이 안되도 합쳐서 문법이 완성되면 됨
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- include 파일 혼자서는 실행 안됨, 코드가 그대로 찍힘 --%>
include.jsp에서 지정한 번호 <%= number %> <%-- 선언을 안했지만 부분파일로 인식해서 에러 안남--%>
<p>
<%
String dataFolder = "c:\\data";
%>
</p>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int number = 10;
%>
<%@ include file = "include.jspf" %> <%-- 반복되는 구문을 외부파일로 만들고 불러쓰는 것을 include --%>
공통변수 DATAFOLDER = <%= dataFolder %>
</body>
</html>
클래스 파일 생성 경로
workspace=C:\Users\user\Documents,
C:\Users\user\Documents\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\IncludeEx01\org\apache\jsp
14. include 2번째 방법
두개의 파일이 각각 클래스로 실행되는 방식
두개의 파일이 모두 문법적으로 완성되어야 함
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<p>
sub.jsp에서 생성된 내용.
</p>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
main.jsp에서 생성한 내용<br>
include 이전<br>
<%-- action태그 기법 --%>
<jsp:include page = "sub.jsp" />
include 이후<br>
</body>
</html>
클래스 파일 생성 경로
C:\Users\user\Documents\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\work\Catalina\localhost\IncludeEx01\org\apache\jsp
include 비교
- <%@ include
include디렉티브 - 프로그램 모듈
- 변수 /상수를 미리 선언
- <jsp:include
include 액션태그 - 디자인 모듈
15. action태그 데이터 전달
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
main.jsp에서 생성한 내용<br>
include 이전<br>
<%-- action태그 데이터 전달 param --%>
<jsp:include page = "sub.jsp">
<jsp:param value="홍길동" name="name"/>
<jsp:param value="30" name="age"/>
</jsp:include>
include 이후<br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String name = request.getParameter("name");
String age = request.getParameter("age");
%>
<p>
sub.jsp에서 생성된 내용.
<%= name +"/" + age %>
</p>
16. 인코딩 한글 깨짐
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
main.jsp에서 생성한 내용<br>
include 이전<br>
<%
//데이터 전달 두번째 방법 setAtrribute
request.setAttribute("data1", "홍길동");
%>
<%-- action태그 데이터 전달 한글처리 인코딩 해줘야함--%>
<jsp:include page = "sub.jsp">
<jsp:param value='<%= java.net.URLEncoder.encode("홍길동", "utf-8") %>' name="name"/>
<jsp:param value="30" name="age"/>
</jsp:include>
include 이후<br>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String name = java.net.URLDecoder.decode(request.getParameter("name"),"utf-8");
String age = request.getParameter("age");
%>
<p>
sub.jsp에서 생성된 내용.<br>
첫번쨰 데이터 전달방법: <%= name +"/" + age %><br>
두번째 데이터 전달방법: <%= request.getAttribute("data1") %><br>
</p>
17. forward방식
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
이 페이지는 to.jsp가 생성한 것입니다.
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
이 페이지는 form.jsp가 생성한 것입니다.
<%--forward 방식은 원래파일의 데이터를 무시함 --%>
<jsp:forward page = "to.jsp"/>
</body>
</html>
18. JSP에서 클래스 다루기
- JSP만으로 전체 페이지를 구성 : hard coding
- JSP + class(=beans) : Model 1 pattern
(view, controller) (model) - beans를 만들려면 반드시 package선언 후
class만드는 폴더 경로
package Pack1;
//beans 만들기 : JSP에서 사용할 class
public class MemberTO {
private String id;
private String password;
public String getId() {
return id;
}
public String getPassword() {
return password;
}
public void setId(String id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- class를 사용하는 JSP페이지 --%>
<%@page import="Pack1.MemberTO"%>
<%
//자바식으로 직접 접근방법
request.setCharacterEncoding("utf-8");
MemberTO to =new MemberTO();
to.setId("tester");
to.setPassword("1234");
%>
아이디 : <%= to.getId()%><br>
비밀번호: <%= to.getPassword()%><br>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<%-- include 방법 --%>
<jsp:useBean id="to" class="Pack1.MemberTO" scope="request" />
<%
to.setId("tester");
to.setPassword("1234");
%>
아이디 : <%= to.getId()%><br>
비밀번호: <%= to.getPassword()%><br>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<%-- include 방법 --%>
<jsp:useBean id="to" class="Pack1.MemberTO" scope="request" />
<%-- jsp태그가 jsp 코드를 대체함 --%>
<%-- 객체명 to가 name으로 들어가야함 --%>
<jsp:setProperty property="id" name="to" value="tester"/>
<jsp:setProperty property="password" name="to" value="1234"/>
아이디 : <jsp:getProperty property="id" name='to' /> <br>
비밀번호: <jsp:getProperty property="password" name='to' /><br>
JSP는 똑같은 내용을 여러가지 코드로 만들수 있기때문에 해석하기 어려움
19. 클라이언트 데이터를 클래스에 사용
MemberTO
package Pack1;
//beans 만들기 : JSP에서 사용할 class
public class MemberTO {
private String id;
private String password;
public String getId() {
return id;
}
public String getPassword() {
return password;
}
public void setId(String id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
}
member1
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- class를 사용하는 JSP페이지 --%>
<%@page import="Pack1.MemberTO"%>
<%
request.setCharacterEncoding("utf-8");
MemberTO to =new MemberTO();
//데이터를 받은뒤 클래스에 넣음
to.setId(request.getParameter("id"));
to.setPassword(request.getParameter("password"));
%>
아이디 : <%= to.getId()%><br>
비밀번호: <%= to.getPassword()%><br>
member_form
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="member1.jsp" method="post">
아이디: <input type="text" name="id" size="30"><br>
비밀번호: <input type="password" name="password" size="30"><br>
<input type="submit" value="전송하기" >
</form>
</body>
</html>
include 방법
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<%-- include 방법 --%>
<jsp:useBean id="to" class="Pack1.MemberTO" scope="request" />
<jsp:setProperty property="id" name="to" value='<%=request.getParameter("id") %>'/>
<jsp:setProperty property="password" name="to" value='<%=request.getParameter("password") %>'/>
아이디 : <jsp:getProperty property="id" name='to' /> <br>
비밀번호: <jsp:getProperty property="password" name='to' /><br>
20. auto weaving
beans
package Pack1;
//beans 만들기 : JSP에서 사용할 class
public class MemberTO {
private String id;
private String password;
public String getId() {
System.out.println("getID()");
return id;
}
public String getPassword() {
System.out.println("getPassword()");
return password;
}
public void setId(String id) {
System.out.println("setID()");
this.id = id;
}
public void setPassword(String password) {
System.out.println("setPassword()");
this.password = password;
}
}
form
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="member2.jsp" method="post">
아이디: <input type="text" name="id" size="30"><br>
비밀번호: <input type="password" name="password" size="30"><br>
<input type="submit" value="전송하기" >
</form>
</body>
</html>
member2
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
%>
<%-- include 방법 --%>
<jsp:useBean id="to" class="Pack1.MemberTO" scope="request" />
<%-- auto weaving --%>
<%-- 자동으로 데이터가 들어감 //setter, getter 이름을 마음대로 바꾸면 안됨 --%>
<jsp:setProperty property="*" name="to" />
아이디 : <jsp:getProperty property="id" name='to' /> <br>
비밀번호: <jsp:getProperty property="password" name='to' /><br>
댓글 없음:
댓글 쓰기