본문 바로가기

java,jsp,spring/JSP

JSP 게시판 (1) 글쓰기

 1. write.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="board.js" charset="UTF-8"></script>
</head>
<body>
	<center>
		<h1>글 올 리 기</h1>
		<form method="post" action="write_ok.jsp" name="write_frm">
			<table>
				<tr height="30">
					<td width="80">작성자</td>
					<td width="100">
						<input type="text" id="name" name="name" size="10" maxlength="20"/>
					</td>
					<td width="80">이메일</td>
					<td width="240">
						<input type="text" id="email" name="email" size="24" maxlength="50" />
					</td>
				</tr>
				<tr>
					<td width="80">글제목</td>
					<td colspan="3" width="460">
						<input type="text" id="title" name="title" size="58" maxlength="80" />
					</td>
				</tr>
				<tr>
					<td colspan="4">
						<textarea cols="70" rows="10" id="content" name="content" maxlength="3000"></textarea>
					</td>
				<tr>
				<tr>
					<td width="80">암&nbsp;&nbsp;호</td>
					<td width="200">
						<input type="password" id="pwd" name="pwd" size="12" maxlength="12" />
					</td>
				</tr>
				<tr height="50">
					<td colspan="4" align="center">
						<input type="button" value="글쓰기" onclick="check_ok()" />
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<input type="reset" value="다시작성" />
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<input type="button" value="글목록" onclick="location.href='list.jsp'" />
					</td>
				</tr>
			</table>		
		</form>
	</center>
</body>
</html>

- write_ok.jsp 로 작성자, 이메일, 글제목, 글내용, 암호를 전송

- 글쓰기 버튼 클릭시 check_ok() 함수 실행

- 글목록을 클릭시 list.jsp로 이동

2. write_ok.jsp

<%@page import="java.net.InetAddress"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean class="magic.board.BoardBean" id="bb"></jsp:useBean>
<%
	String content = request.getParameter("content");
	content = content.replace("\r\n", "<br>"); 
	// DB에 콘텐트를 저장하면서 줄바꿈이 처리가 안되는 경우가 있기 때문에 자바에서 줄바꿈을 처리하는 명령어를 <br>로 대체
	bb.setB_name(request.getParameter("name"));	
	bb.setB_email(request.getParameter("email"));	
	bb.setB_title(request.getParameter("title"));
	bb.setB_date(new Timestamp(System.currentTimeMillis()));
	bb.setB_content(content);	
	bb.setB_pwd(request.getParameter("pwd"));
	
	// ip 주소 리턴 방법. 1
	InetAddress address = InetAddress.getLocalHost();
	String ip = address.getHostAddress();
	bb.setB_ip(ip);
	// ip 주소 리턴 방법. 2
	// bb.setB_ip(request.getRemoteAddr());
	
	BoardDBBean bdb = BoardDBBean.getInstance();
	int re = bdb.insertboard(bb);
	
	if(re == 1){
		response.sendRedirect("list.jsp");
	} else {
		response.sendRedirect("write.jsp");
	}
%>

3. BoardDBBean.java

package magic.board;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class BoardDBBean {
	private static BoardDBBean instance = new BoardDBBean();
	public static BoardDBBean getInstance() {
		return instance;
	}
	
	public Connection getConnection() throws Exception{
		Context ctx = new InitialContext();
		DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oracle");
		return ds.getConnection();
	}
	
	
	public int insertboard(BoardBean board) throws Exception {
		String sql = "SELECT MAX(B_ID) FROM boardT"; // id 최대값

		int re = -1;
		int number;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = getConnection();
			;
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();

			if (rs.next()) {
				number = rs.getInt(1) + 1;
			} else {
				number = 1;
			}
			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());
			// 조회수 컬럼의 초기값을 0으로 설정 > 데이터 구조에서 디폴트값을 0으로 설정(글쓰면 조회수 0이기 때문)
			pstmt.setString(8, board.getB_pwd());
			pstmt.setString(9, board.getB_ip());
			pstmt.executeUpdate();

			re = 1;
		} catch (SQLException ex) {
			System.out.print("글쓰기 실패");
			ex.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (pstmt != null) {
					pstmt.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return re;
	}

}

4.BoardBean.java

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;
	
	
	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_hit() {
		return b_hit;
	}
	public void setB_hit(int b_hit) {
		this.b_hit = b_hit;
	}
	public Timestamp getB_date() {
		return b_date;
	}
	public void setB_date(Timestamp b_date) {
		this.b_date = b_date;
	}
	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;
	}
	
	
}

5. board.js

function check_ok(){
	var name = document.getElementById("name").value;
	var email = document.getElementById("email").value;
	var title = document.getElementById("title").value;
	var content = document.getElementById("content").value;
	var pwd = document.getElementById("pwd").value;
	if(!name){
		alert("이름을 써주세요");
		write_frm.name.focus();
		return;
	}
	if(!email){
		alert("이메일을 써주세요");
		write_frm.email.focus();
		return;
	}
	if(!title){
		alert("제목을 써주세요");
		write_frm.title.focus();
		return;
	}
	if(!content){
		alert("내용을 써주세요");
		write_frm.content.focus();
		return;
	}
	if(!pwd){
		alert("비밀번호를 써주세요");
		write_frm.pwd.focus();
		return;
	}
	document.write_frm.submit();
}

 

'java,jsp,spring > JSP' 카테고리의 다른 글

JSP 게시판 (3) 글 수정  (0) 2022.09.06
JSP 게시판 (2) 글 목록, 보기  (0) 2022.09.06
JSP 게시판  (0) 2022.09.06
JSP 예제 - 사용자 관리(3단계)  (0) 2022.09.03
JSP 예제 - 사용자 관리(2단계)  (0) 2022.09.03