본문 바로가기

jsp 프로젝트 V2 post

JSP게시판 만들기 3강 - MVC세팅하기

1. 패키지 구조 생성

1) controller

2) action

3) model

4) dao(repository)

5) dto

6) db

7) util

 

2. 정적 폴더 구조 생성 WebContent 구조 생성

1) static

- css

- js

- image

2) member

3) post

 

3. 팩토리 세팅

1) com.cos.apple.action.Action.java

package com.cos.apple.db.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Action {
	void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

}

 

2) com.cos.apple.action.post.PostListAction.java

package com.cos.apple.db.action.post;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cos.apple.db.action.Action;

public class PostListAction implements Action{

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dis = request.getRequestDispatcher("index.jsp");
		dis.forward(request, response);
	}

}

 

3) PostController.java

- 주소 매핑(post)

package com.cos.apple.db.controller;

import java.io.IOException;

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.cos.apple.db.action.Action;
import com.cos.apple.db.action.post.PostListAction;

//http://localhost:8000/apple/post
@WebServlet("/post")
public class PostController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public PostController() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doProcess(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doProcess(request, response);
	}

	protected void doProcess(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1. request utf-8 세팅 = web.xml 필터 등록함.
		// 2. response utf-8 세팅
		response.setCharacterEncoding("utf-8"); // 내가 상대한테 응답 시 바디에 담긴 거 인코딩.
		response.setContentType("text/html; charset=utf-8"); // text/html은 default타입이에요.

		String cmd = request.getParameter("cmd");
		Action action = router(cmd);
		action.execute(request, response);

	}

	private Action router(String cmd) {
		if (cmd.equals("list")) {
			return new PostListAction();
		}
		return null;
	}
}

4. 동작 테스트

index.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>
	<h1>게시글 목록 페이지</h1>
	<hr />
</body>
</html>