記錄

Servlet) 기본 흐름, web.xml 매핑 본문

Web/Servlet

Servlet) 기본 흐름, web.xml 매핑

surhommejk 2018. 3. 28. 10:49

web.xml을 통한 매핑


<!-- name값을 임의 지정하고 class에서 제대로 경로 지정 -->
<servlet>
  <servlet-name>simplecontroller</servlet-name>
  <servlet-class>com.SimpleController</servlet-class>
</servlet>

<!-- 위에서 정한 name값으로 심플한 url-pattern을 임의 지정 -->
<servlet-mapping>
  <servlet-name>simplecontroller</servlet-name>
  <url-pattern>/simple</url-pattern>
</servlet-mapping>
</web-app>



<servlet>
<servlet-name>path</servlet-name>
<servlet-class>com.PathServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>path</servlet-name>
<url-pattern>/java/study/*</url-pattern>
</servlet-mapping>

먼저 이름과 경로를 설정해주고 매핑(mapping) 단계에서 '지정해 주는 url'을 '이름'으로 가도록 설정한다.

여기서는 /java/study/~    의 모든 파일(심지어 존재하지 않는 주소라 해도) 결국 path로 가게 된다.






Servlet 기본적 사항


protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        System.out.println("클라이언트 요청");
        
        //1. 사용자의 요청 파악 (요청 값 받기)
        String type = request.getParameter("type");
        
        //2. 요청에 따른 업무 수행 (service 실행)
        Object resultobj = null;
        if(type == null || type.equals("greeting")){
            resultobj = "hello world";
        } else if(type.equals("data")) {
            resultobj = new java.util.Date();
        }else {
            resultobj = "invalid date type";
        }
        
        //3. 요청 완료에 따라서 그 결과를 요청한 사용자에게 전달
        //정보 저장 : request, session, application
        request.setAttribute("result", resultobj);
        
        //4. 결과 보여주기> 필요한 view 지정
        //화면을 출력할 페이지를 정하고 출력할 데이터를 넘겨 주어야 한다(forward 방식)
        RequestDispatcher dis = request.getRequestDispatcher("/simpleview.jsp");
        dis.forward(request, response); // forward된 페이지에 request 주소값 전달
            
    }



javax.servlet 

Interface RequestDispatcher

public interface RequestDispatcher


Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.


This interface is intended to wrap servlets, but a servlet container can create RequestDispatcher objects to wrap any type of resource.


forward

public void forward (ServletRequest request, ServletResponse response)

                                                                                 throws ServletException, java.io.IOException


Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.

For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.


forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.


The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the ServletRequestWrapper or ServletResponseWrapper classes that wrap them.


Parameters:

request - a ServletRequest object that represents the request the client makes of the servlet

response - a ServletResponse object that represents the response the servlet returns to the client


Comments