Back-end/Spring-핵심& webMVC

Web.xml, DispatcherServlet, ContextLoaderListener 각각의 역할 및 관계

prden 2022. 12. 30. 13:48

1. DispatcherServlet :

   - FirstController로 한 개의 WebApplicationContext를 가지며, 또한 여러 개의 DispatcherServlet 가질 수 있다.

   - 그러나, DispatcherServlet 간(각각 별도의*. xml파일 설정)에는 객체 서로 공유되지 않는다.

   - 순서는 <load-on-startup>순서</load-on-startup>으로 준다.

2. ContextLoaderListener : 

    -   DispatcherServlet에서 공통 빈을 필요로 하는 경우 ContextLoaderListener에 설정해주면 된다.(아래 예시)

    -  통상적으로 여기에는 datasource, springsecurity 등을 설정해준다.

 

 

<? xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/appServlet/root-context.xml
            /WEB-INF/spring/appServlet/security-context.xml
        </param-value>
    </context-param>

<!--DispathcerServlet이 사용할 수 있도록 ApplicationContext=IOC컨테이너 만들어서 서블릿 콘텍스트에 등록해주는 것,
서블릿 콘텍스트의 라이프사이클에 맞춰서 스프링이 제공하는 ApplicationContext를 연동해주는 리스너이다.
여기서 만든 것은 다른 서블릿에서 공유해서 쓸 수 있다. 여기가 RooteWebApplicationContext임-->
    <listener>
    <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
    </listener>

<!--DispathcerServlet = 스프링이 구현한 firstController, 다른 dispatcherServlet에서는 이거 못 가져다 씀,
새로운 WebApplicationContext 만들면서 RootWebApplication 상속 받음 -->

    <servlet>
    	<servlet-name> appServlet </servlet-name>
    	<servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
    	<init-param>
    		<param-name> contextConfigLocation </param-name>
    		<param-value>
    		/WEB-INF/spring/appServlet/servlet-context.xml
    		</param-value>
    	</init-param>
    <!-- 톰켓이 로드될 때 servlet메모리에 가장 먼저 1번째로 올려라는 표현 -->
    	<load-on-startup>1 </load-on-startup>
    <!-- 서블릿을 비동기적으로 로드시켜라 -->
    	<async-supported> true </async-supported>
    </servlet>

    <servlet-mapping>
    	<servlet-name> appServlet </servlet-name>
    	<!--/밑으로 들어오는 모든 요청 dispathcerServlet으로 들어옴-->
    	<url-pattern>/</url-pattern>
    </servlet-mapping>

    <error-page>
    	<error-code>404 </error-code>
    	<location>/404.html </location>
    </error-page>

    <filter>
    	<filter-name> encoding-filter </filter-name>
    	<filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class>
    	<init-param>
    		<param-name> encoding </param-name>
    		<param-value> UTF-8 </param-value>
    	</init-param>
    	<init-param>
            <param-name> forceEncoding </param-name>
            <param-value> true </param-value>
    	</init-param>
    </filter>
    
    <filter-mapping>
        <filter-name> encoding-filter </filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- security filter가 먼저 작동하면 앞에 encoding filter 작동 안 함 -->
    <filter>
        <filter-name> springSecurityFilterChain </filter-name>
        <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class>
    </filter>

    <filter-mapping>
    	<filter-name> springSecurityFilterChain </filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

 

spring-servlet관련 동영상 : https://www.youtube.com/watch?v=calGCwG_B4Y&t=47s 

3. web.xml의 action 설정에서 url-pattern .do

(eGovFramework)의 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>egov.sample</display-name>
    
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
    
	<filter>
		<filter-name>HTMLTagFilter</filter-name>
		<filter-class>egovframework.rte.ptl.mvc.filter.HTMLTagFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HTMLTagFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
    
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:egovframework/spring/context-*.xml</param-value>
	</context-param>
    
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
    
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/egovframework/springmvc/dispatcher-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
    
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
    
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
    
	<login-config>
		<auth-method>BASIC</auth-method>
	</login-config>
    
	<error-page>
		<exception-type>java.lang.Throwable</exception-type>
		<location>/common/error.jsp</location>
	</error-page>
	<error-page>
		<error-code>404</error-code>
		<location>/common/error.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/common/error.jsp</location>
	</error-page>
    
</web-app>

action을 통해 들어온 URL 중에 .do를 갖는 URL을 처리하겠다는 의미이다. 즉, .do로 끝나는 url pattern은 action이라는 디스패쳐 서블릿으로 요청을 보내겠다는 것이다. 톰갯은 구동하면서 web.xml을 가장먼저 읽고 -> dispatcher-servelet.xml 을 읽어서 처리한다.