21.01.07 servlet & JSP 연계 연습(게시판별 게시물 리스팅, 상세보기, 생성, 수정 , 삭제 등)

2021. 1. 7. 21:32JAVA/JSP Community 사이트 프로젝트

<ArticleListServlet.java>

package com.sbs.example.jspCommunity;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sbs.example.mysqlutil.MysqlUtil;
import com.sbs.example.mysqlutil.SecSql;


@WebServlet("/jsp/usr/article/list")
public class ArticleListServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String code = "notice";
		
		if (request.getParameter("code") == null) {
			response.getWriter().append("게시판 code를 입력해주세요.");
			return;
		}
		if (request.getParameter("code") != null) { code =
		request.getParameter("code"); }
		 
		
		//DB 서버 연결
		MysqlUtil.setDBInfo("127.0.0.1", "sbsst", "sbs123414", "jspCommunity");
				
		SecSql sql1 = new SecSql();
		sql1.append("SELECT * FROM board ");
		sql1.append("WHERE code = ?", code);
				
		Map<String, Object> boardMap = MysqlUtil.selectRow(sql1);
		if (boardMap.isEmpty()) {
			response.getWriter().append("code를 확인해 주세요.");
			return;
		}
		
		
		int boardId = (int) boardMap.get("id");
		String boardName = (String) boardMap.get("name");
		String boardCode = (String) boardMap.get("code");
		
		SecSql sql2 = new SecSql();
		sql2.append("SELECT * FROM article ");
		sql2.append("WHERE boardId = ?", boardId);
		
		List<Map<String, Object>> articleMapList = MysqlUtil.selectRows(sql2);
		
		
		
		//DB 서버 연결 종료
		MysqlUtil.closeConnection();
		
		request.setAttribute("articleMapList", articleMapList);
		request.setAttribute("boardId", boardId);
		request.setAttribute("boardName", boardName);
		request.setAttribute("boardCode", boardCode);
		
		request.getRequestDispatcher("/jsp/usr/article/list.jsp").forward(request, response);

	}
}

 

<list.jsp>

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.List"%>
<%
List<Map<String, Object>> articleMapList = (List<Map<String, Object>>) request.getAttribute("articleMapList");
int boardId = (int) request.getAttribute("boardId");
String boardName = (String) request.getAttribute("boardName");
String boardCode = (String) request.getAttribute("boardCode");
%>
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<title>게시물 리스트</title>
</head>
<body>
	<h1><%= boardName %> 게시판 게시물 리스트</h1>
	<%
	for (Map<String, Object> articleMap : articleMapList) {
	%>
	<div style="font-weight:bold;">
		번호 :
		<%=articleMap.get("id")%>
		<br />
		제목 :
		<a style="text-decoration:none; color:inherit;" href="http://localhost:8083/jspCommunity/jsp/usr/article/detail?id=<%=articleMap.get("id")%>"><%=articleMap.get("title")%></a>
		<br />
		내용 :
		<%=articleMap.get("body")%>
		<br>
		<a href="http://localhost:8083/jspCommunity/jsp/usr/article/doModify?id=<%=articleMap.get("id")%>">수정</a>
		<a href="http://localhost:8083/jspCommunity/jsp/usr/article/doDelete?id=<%=articleMap.get("id")%>">삭제</a>
		<hr />
	</div>
	<%
	}
	%>
</body>
</html>

 

<결과>