JSP 게시판 만들기 - 글쓰기
페이지 정보
본문
[ write.jsp ]
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" language="java" import="java.sql.*" errorPage="" %>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>게시판</title>
<script language = "javascript"> // 자바 스크립트 시작
function writeCheck()
{
var form = document.writeform;
if( !form.name.value ) // form 에 있는 name 값이 없을 때
{
alert( "이름을 적어주세요" ); // 경고창 띄움
form.name.focus(); // form 에 있는 name 위치로 이동
return;
}
if( !form.password.value )
{
alert( "비밀번호를 적어주세요" );
form.password.focus();
return;
}
if( !form.title.value )
{
alert( "제목을 적어주세요" );
form.title.focus();
return;
}
if( !form.memo.value )
{
alert( "내용을 적어주세요" );
form.memo.focus();
return;
}
form.submit();
}
</script>
</head>
<body>
<table>
<tr>
<td>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr style="background:url('img/table_mid.gif') repeat-x; text-align:center;">
<td width="5"><img src="img/table_left.gif" width="5" height="30" /></td>
<td>글쓰기</td>
<td width="5"><img src="img/table_right.gif" width="5" height="30" /></td>
</tr>
</table>
<form name="writefor"m method="post" action="write_ok.jsp">
<table width="100%">
<tr>
<td align="center">제목</td>
<td><input name="title" size="50" maxlength="100"></td>
</tr>
<tr height="1" bgcolor="#dddddd"><td colspan="2"></td></tr>
<tr>
<td align="center">이름</td>
<td><input name="name" size="50" maxlength="50"></td>
</tr>
<tr height="1" bgcolor="#dddddd"><td colspan="2"></td></tr>
<tr>
<td align="center">비밀번호</td>
<td><input name="password" size="50" maxlength="50"></td>
</tr>
<tr height="1" bgcolor="#dddddd"><td colspan="2"></td></tr>
<tr>
<td align="center">내용</td>
<td><textarea name="memo" cols="50" rows="13"></textarea></td>
</tr>
<tr height="1" bgcolor="#dddddd"><td colspan="2"></td></tr>
<tr height="1" bgcolor="#82B5DF"><td colspan="2"></td></tr>
<tr align="center">
<td colspan="2">
<input type=button value="등록" OnClick="javascript:writeCheck();">
<input type=button value="취소" OnClick="javascript:history.back(-1)">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
[ write_ok.jsp ]
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.sql.*"%>
<%
request.setCharacterEncoding("utf-8"); //받아오는 값들을 한글로 인코딩합니다.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:디비네임";
String id = "디비아이디";
String pass = "디비패스워드";
String name = request.getParameter("name"); // write.jsp에서 name에 입력한 데이터값
String password = request.getParameter("password"); // write.jsp에서 password에 입력한 데이터값
String title = request.getParameter("title"); // write.jsp에서 title에 입력한 데이터값
String memo = request.getParameter("memo"); // write.jsp에서 memo에 입력한 데이터값
try {
Connection conn = DriverManager.getConnection(url,id,pass);
String sql = "insert into board (name, passowrd, title, memo) values (?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
pstmt.setString(3, title);
pstmt.setString(4, memo);
pstmt.execute();
pstmt.close();
conn.close();
} catch(SQLException e) {
out.println( e.toString() );
}
%>
<script language=javascript>
self.window.alert("입력한 글을 저장하였습니다.");
location.href="list.jsp";
</script>
자료출처
http://seinarin.tistory.com/5
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" language="java" import="java.sql.*" errorPage="" %>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>게시판</title>
<script language = "javascript"> // 자바 스크립트 시작
function writeCheck()
{
var form = document.writeform;
if( !form.name.value ) // form 에 있는 name 값이 없을 때
{
alert( "이름을 적어주세요" ); // 경고창 띄움
form.name.focus(); // form 에 있는 name 위치로 이동
return;
}
if( !form.password.value )
{
alert( "비밀번호를 적어주세요" );
form.password.focus();
return;
}
if( !form.title.value )
{
alert( "제목을 적어주세요" );
form.title.focus();
return;
}
if( !form.memo.value )
{
alert( "내용을 적어주세요" );
form.memo.focus();
return;
}
form.submit();
}
</script>
</head>
<body>
<table>
<tr>
<td>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr style="background:url('img/table_mid.gif') repeat-x; text-align:center;">
<td width="5"><img src="img/table_left.gif" width="5" height="30" /></td>
<td>글쓰기</td>
<td width="5"><img src="img/table_right.gif" width="5" height="30" /></td>
</tr>
</table>
<form name="writefor"m method="post" action="write_ok.jsp">
<table width="100%">
<tr>
<td align="center">제목</td>
<td><input name="title" size="50" maxlength="100"></td>
</tr>
<tr height="1" bgcolor="#dddddd"><td colspan="2"></td></tr>
<tr>
<td align="center">이름</td>
<td><input name="name" size="50" maxlength="50"></td>
</tr>
<tr height="1" bgcolor="#dddddd"><td colspan="2"></td></tr>
<tr>
<td align="center">비밀번호</td>
<td><input name="password" size="50" maxlength="50"></td>
</tr>
<tr height="1" bgcolor="#dddddd"><td colspan="2"></td></tr>
<tr>
<td align="center">내용</td>
<td><textarea name="memo" cols="50" rows="13"></textarea></td>
</tr>
<tr height="1" bgcolor="#dddddd"><td colspan="2"></td></tr>
<tr height="1" bgcolor="#82B5DF"><td colspan="2"></td></tr>
<tr align="center">
<td colspan="2">
<input type=button value="등록" OnClick="javascript:writeCheck();">
<input type=button value="취소" OnClick="javascript:history.back(-1)">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
[ write_ok.jsp ]
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.sql.*"%>
<%
request.setCharacterEncoding("utf-8"); //받아오는 값들을 한글로 인코딩합니다.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:디비네임";
String id = "디비아이디";
String pass = "디비패스워드";
String name = request.getParameter("name"); // write.jsp에서 name에 입력한 데이터값
String password = request.getParameter("password"); // write.jsp에서 password에 입력한 데이터값
String title = request.getParameter("title"); // write.jsp에서 title에 입력한 데이터값
String memo = request.getParameter("memo"); // write.jsp에서 memo에 입력한 데이터값
try {
Connection conn = DriverManager.getConnection(url,id,pass);
String sql = "insert into board (name, passowrd, title, memo) values (?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
pstmt.setString(3, title);
pstmt.setString(4, memo);
pstmt.execute();
pstmt.close();
conn.close();
} catch(SQLException e) {
out.println( e.toString() );
}
%>
<script language=javascript>
self.window.alert("입력한 글을 저장하였습니다.");
location.href="list.jsp";
</script>
자료출처
http://seinarin.tistory.com/5
댓글목록
등록된 댓글이 없습니다.