Demo entry 6685911

1

   

Submitted by anonymous on Dec 22, 2017 at 03:28
Language: JavaScript. Code size: 3.0 kB.

package jsp.board.action;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.ResourceBundle;

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

import jsp.common.action.Action;
import jsp.common.action.ActionForward;

public class BoardController extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	private HashMap<String,Action> commandMap;
	
	
	public void init(ServletConfig config) throws ServletException {	
        loadProperties("jsp/board/properties/BoardCommand");
    }
	
	
	private void loadProperties(String filePath) 
	{
		commandMap = new HashMap<String, Action>();
		
		ResourceBundle rb = ResourceBundle.getBundle(filePath);
		Enumeration<String> actionEnum = rb.getKeys();
		 
		while (actionEnum.hasMoreElements()) 
		{
			// 명령어
			String command = actionEnum.nextElement(); 
			// Actionclass
			String className = rb.getString(command); 
			
			try {
				 Class actionClass = Class.forName(className); // 클래스 생성
				 Action actionInstance = (Action)actionClass.newInstance(); // 객체를 생성
				 
				 if(className.equals("jsp.board.action.BoardFormChangeAction")){
					 BoardFormChangeAction bf = (BoardFormChangeAction)actionInstance;
					 bf.setCommand(command);
				 }
				 
				 
				 commandMap.put(command, actionInstance);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

		//get 방식
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
			doProcess(request,response);
	}  	
		
		//post 방식
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
			doProcess(request,response);
	}
	
	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		// 커맨드 추출
		String requestURI = request.getRequestURI();
		int cmdIdx = requestURI.lastIndexOf("/") + 1;
		String command = requestURI.substring(cmdIdx);
		
			ActionForward forward = null;
			Action action = null;
		
			try {
			
			action = commandMap.get(command);
			
			if (action == null) {
                System.out.println("명령어 : "+command+"는 잘못된 명령입니다.");
                return;
            }

			forward = action.execute(request, response);
			
			
			if(forward != null){
				if (forward.isRedirect()) {
					response.sendRedirect(forward.getNextPath());
				} else {
					RequestDispatcher dispatcher = request
							.getRequestDispatcher(forward.getNextPath());
					dispatcher.forward(request, response);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}	  	      	
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).