본문 바로가기
Back-End/JSP & Thymeleaf

9.게시판 글쓰기 기능

by 두두리안 2020. 12. 25.
728x90

write.jsp

글쓰기 기능

write의 jsp 구성

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.PrintWriter"%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<jsp:include page="head.jsp" flush="false" />
    <%
        String userID = null;
        if (session.getAttribute("userID") != null) {
            userID = (String) session.getAttribute("userID");
        }
        if(userID == null) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('로그인을 하세요.');");
            script.println("location.href='login.jsp'");
            script.println("</script>");
            script.close();
        }
    %>
    <div class="container">
            <form method="post" action="writeAction.jsp">
                <table class="table table-bordered" style="text-align: center;">
                    <thead>
                        <tr>
                            <th colspan="2" style="background-color: #eeeeee; text-align: center;">게시판 글쓰기 양식</th>
                        </tr>
                    </thead>
                    <tbody>
                            <tr>
                                <td colspan="2"><input type="text" class="form-control" placeholder="글 제목" name="bbsTitle" maxlength="50"></td>
                            </tr>
                            <tr>
                                <td colspan="2"><textarea class="form-control" name="bbsContent" placeholder="글 내용" maxlength="2048" style="height: 350px;"></textarea></td>
                            </tr>
                    </tbody>
                </table>
                <input type="submit" class="btn btn-primary pull-right" value="글쓰기">
            </form>
    </div>
    <jsp:include page="footer.jsp" flush="false" />
</body>
</html>

writeAction의 jsp 구성

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="bbs.BbsDAO"%>
<%@ page import="java.io.PrintWriter"%>
<%
    request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="bbs" class="bbs.Bbs" scope="page" >
     <jsp:setProperty name="bbs" property="bbsTitle" />
    <jsp:setProperty name="bbs" property="bbsContent" />
</jsp:useBean>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP 게시판 웹 사이트</title>
</head>
<body>
    <%
        String userID = null;
        if (session.getAttribute("userID") != null) {
            userID = (String) session.getAttribute("userID");
        }
        if(userID == null) {
            PrintWriter script = response.getWriter();
            script.println("<script>");
            script.println("alert('로그인을 하세요.');");
            script.println("location.href='login.jsp'");
            script.println("</script>");
            script.close();
        } else {
            if (bbs.getBbsTitle() == null || bbs.getBbsContent() == null) {
                PrintWriter script = response.getWriter();
                script.println("<script>");
                script.println("alert('입력이 안 된 사항이 있습니다.');");
                script.println("history.back();");
                script.println("</script>");
                script.close();
            } else {
                BbsDAO bbsDAO = new BbsDAO();
                int result = bbsDAO.write(bbs.getBbsTitle(), userID, bbs.getBbsContent());
                if (result == -1) {
                    PrintWriter script = response.getWriter();
                    script.println("<script>");
                    script.println("alert('글쓰기에 실패하였습니다.');");
                    script.println("history.back();");
                    script.println("</script>");
                    script.close();
                } else {
                    PrintWriter script = response.getWriter();
                    script.println("<script>");
                    script.println("location.href = 'board.jsp';");
                    script.println("</script>");
                    script.close();
                }
            }
        }
    %>
</body>
</html>
728x90