ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • day49 - JSP(model2)
    KIC/JSP 2021. 8. 25. 01:27
    반응형

    web.xml 에서

        <servlet-mapping>
        <servlet-name>controllerExt</servlet-name>
        <url-pattern>*.do</url-pattern>
        </servlet-mapping>

     

    를 통해 *.do를 받으면 controllerExt와 연동한다.

     

     

     

    [controllerExt.properties]

    -> hello.do를 입력할 시

    /hello.do=handler.HelloHandler
    /list.do=handler.ListHandler
    /memberInput.do=handler.MemberInputHandler

    [helloHandler.java]

    -> /view/hello.jsp 가 실행되고,

    package handler;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import controller.CommandHandler;
    
    
    
    public class HelloHandler implements CommandHandler{
    
    	@Override
    	public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
    		// TODO Auto-generated method stub
    		request.setAttribute("hello", "hello 테스트 입니다.");
    		
    		return "/view/hello.jsp";
    	}
    
    }

     

     

    [hello.jsp]

    -> 가 실행된다.

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    ${hello} <br>
    hello 입니다.
    </body>
    </html>

     

     

     

     

     

     

     

    [ControllerPathMethod.java]

    package controller;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Properties;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class ControllerExt
     */
    
    public class ControllerPathMethod extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public ControllerPathMethod() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		processPro(request, response);
    	}
    
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		processPro(request, response);
    	}
    	
    	
    	public void processPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    		String command = request.getRequestURI();
    		System.out.println("1:"+command);
    		if(command.indexOf(request.getContextPath())==0) {
    			command = command.substring(request.getContextPath().length());
    			command = command.substring(command.lastIndexOf("/") + 1);
    		}
    		
    		
    		System.out.println("2:"+command);
    		String viewPage = null;
    		Class[] cParam = {HttpServletRequest.class, HttpServletResponse.class};
    		
    		try {
    			viewPage = (String)this.getClass()
    					.getMethod(command, cParam)
    					.invoke(this, request, response);
    		}catch(Exception e) {
    			e.printStackTrace();
    		}
    		
    		
    		
    		if(viewPage !=null) {
    			RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
    			dispatcher.forward(request, response);
    		}
    		
    	}
    	
    	public String InputForm(HttpServletRequest request, HttpServletResponse response) throws Exception {
    		// TODO Auto-generated method stub
    		request.setAttribute("InputForm", "InputForm 테스트 입니다.");
    		
    		return "/view/InputForm.jsp";
    	}
    
    }
    300x250

    'KIC > JSP' 카테고리의 다른 글

    day51 - JSP(model2, login)  (0) 2021.08.26
    day50 - JSP(model2, login, DBconnection)  (0) 2021.08.26
    day48 - JSP(model2)  (0) 2021.08.24
    day47 - JSP(AJAX, JSON, jQuery)  (0) 2021.08.20
    day46_2 - JSP(AJAX)  (0) 2021.08.20

    댓글

Designed by Tistory.