wasup

게시판 만들기#ver3 본문

IT/Java

게시판 만들기#ver3

wasupup 2021. 6. 8. 18:54
반응형

RequestDispatcher

: 현재 request에 담긴 정보를 저장(다음 페이지에서 해당 정보를 볼 수 있게 계속 저장)

 

element.setAttribute("attribute_name", "attribute_value");

: element를 선택, setAttribute함수를 실행할 때 속성 이름과 속성 값을 넣어 사용함.


board_ready.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%

//포워드
RequestDispatcher setViewName = request.getRequestDispatcher("board_write.jsp");
setViewName.forward(request, response);

%>

위 board_ready.jsp에서 포워드했으므로 그 아래로 어떠한 작업을 하더라도

응답시 보여지지 않음.

 

board_write.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시물 작성하기</title>
</head>
<body>

<%@ include file="./nav.jsp" %>

<form action="board_update.jsp" method="post">

	<p>
		<label for="bb_tit">제목</label>
		<input type="text" name="bb_tit" id="bb_tit" maxlength="50"/>
	</p>
	<p>
		<label for="bb_con">내용</label>
		<input type="text" name="bb_con" id="bb_con" maxlength="200"/>
	</p>
	<input type="submit" value="등록" />

</form>

</body>
</html>

넘겨받은 값을 board_write.jsp에서 출력, 가공 등이 가능함.

현재는 따로 넘긴 값이 없음.

 

board_update.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="basicmanage.*" %>
<%

request.setCharacterEncoding("utf-8");
String bb_tit = request.getParameter("bb_tit");
String bb_con = request.getParameter("bb_con");

Basic newBasic = new Basic();
newBasic.setBb_tit(bb_tit);
newBasic.setBb_con(bb_con);

BasicDAO dao = new BasicDAO();
dao.basicSave(newBasic);

request.setAttribute("bb_tit", newBasic.getBb_tit());

RequestDispatcher setViewName = request.getRequestDispatcher("board_result.jsp");
setViewName.forward(request, response);

%>

board_ready.jsp와 마찬가지로 board_update.jsp의 포워드 아래 어떠한 작업을 해도

응답시 표시되지 않음.

 

board_result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>결과!</title>
</head>
<body>
<%@ include file="./nav.jsp" %>
<h3>게시물등록결과</h3>

${bb_tit} 게시물이 정상 등록 되었습니다.

</body>
</html>

board_update.jsp에서 setAttribute함수로 속성이름과 값을 넣어 전달한 값을

board_result.jsp에서 속성이름을 이용해 값을 출력함.


src>basicmanage>Basic.java

package basicmanage;

import java.sql.Date;

public class Basic {
	private int bb_num; 
	private String bb_tit;
	private String bb_con;
	private Date bb_date; 
	private int bb_hit;
	
	public int getBb_num() {
		return bb_num;
	}
	public void setBb_num(int bb_num) {
		this.bb_num = bb_num;
	}
	public String getBb_tit() {
		return bb_tit;
	}
	public void setBb_tit(String bb_tit) {
		this.bb_tit = bb_tit;
	}
	public String getBb_con() {
		return bb_con;
	}
	public void setBb_con(String bb_con) {
		this.bb_con = bb_con;
	}
	public Date getBb_date() {
		return bb_date;
	}
	public void setBb_date(Date bb_date) {
		this.bb_date = bb_date;
	}
	public int getBb_hit() {
		return bb_hit;
	}
	public void setBb_hit(int bb_hit) {
		this.bb_hit = bb_hit;
	}
}

src>basicmanage>BasicDAO.java

package basicmanage;

import java.sql.*;

import control.ConnectionInfo;

public class BasicDAO {

	public void basicSave( Basic newBasic ) {
		try { // DB연결관리객체 : Connection, 명령전달객체 : Statement, 질의결과관리객체 : ResultSet
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			Connection DBConnect = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf8", ConnectionInfo.DB_IP, ConnectionInfo.DB_Port, ConnectionInfo.DB_Name), ConnectionInfo.DB_ID, ConnectionInfo.DB_PW);

			Statement DBState = DBConnect.createStatement();
			
			String insertSQL = "INSERT INTO basic_board(bb_tit, bb_con) VALUES('" + newBasic.getBb_tit() + "', '" + newBasic.getBb_con() + "')";
			DBState.executeUpdate(insertSQL);
		
			DBConnect.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

src>control>ConnectionInfo.java

package control;

public class ConnectionInfo {

	public static String DB_IP = "localhost";
	public static String DB_Port = "3306";
	public static String DB_Name = "dbName";
	public static String DB_ID = "root";
	public static String DB_PW = "password";
}
반응형

'IT > Java' 카테고리의 다른 글

Maven 시작해보기  (0) 2021.06.12
게시판 만들기_기본게시판 + 회원가입  (0) 2021.06.09
게시판 만들기_로그인  (0) 2021.06.06
게시판 만들기_회원가입  (0) 2021.06.05
게시판 만들기#ver2_정리  (0) 2021.05.31
Comments