[210811] Spring 설정(1) - web.xml

해니·2021년 8월 11일
0

web.xml (WEB-INF 폴더)

서버의 시작지점
웹 애플리케이션의 기본적인 설정을 위해 작성하는 파일
WAS(Web Application Server) -> Tomcat 이 최초로 구성될 때,
WEB-INF 폴더에 존재하는 web.xml 파일을 읽고, 그에 해당하는 웹 애플리케이션을 설정

♣ 배포 서술자(DD, Deployment Descriptor)
웹 애플리케이션의 기본적인 설정을 위해 작성하는 파일
보통은 WEB-INF/web.xml 파일을 말함
EJB를 위한 ejb-jar.xml, 웹서비스를 위한 webservices.xml 등이 있음
JSP와 서블릿만으로 구성된 경우에는 web.xml 파일만 사용하면 됨

xml

XML 파일이고, 버전은 "1.0" 이며, UTF-8로 인코딩 한다는 설정

<?xml version="1.0" encoding="UTF-8"?>

web-app

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">

web.xml 파일의 루트 엘리먼트
모든 웹 애플리케이션의 설정은 < web-app> < /web-app> 태그의 사이에 존재해야 함

context-param

이름이나 객체를 바인딩하는 집합의 역할을 담당
-> 어떠한 객체를 핸들링하기 위한 접근 수단
어플리케이션의 초기화 파라미터(context의 파라미터)를 선언하는데 사용
-> 지역변수 같은 역할)
이클립스(STS)에서 기본적으로 제공해주는 설정 파일 외에 사용자가 직접 컨트롤하는 XML 파일을 지정해주는 역할을 함

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

<!--
<param-name> : (파라미터 이름) contextConfigLocation
<param-value> : (파라미터 값) /WEB-INF/spring/root-context.xml
-->

listener

모든 서블릿과 필터에 의해 공유되는 스프링 컨테이너를 생성함

	<!-- listener 태그로 ContextLoaderListener 클래스를 등록 -->
	<!-- 이 ContextLoaderListener 클래스가 생성하는 WebApplicationContext는 웹 애플리케이션에서 루트 컨텍스트가 됨-->
	<!-- 자식은 root가 제공하는 객체(Bean)을 사용할 수 있음 -->

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

♣ ContextLoaderListener
Controller가 공유하는 Bean들을 포함하는 Spring Container를 생성함
서블릿에서 제공하는 ServletContextListener를 확장하여 만든 리스너로, 웹 애플리케이션이 서블릿 컨테이너에 로딩될 때 실행되는 리스너
웹 애플리케이션이 로딩될 때, WebApplicationContext 를 만드는 역할을 함
-> 이렇게 생성된 WebApplicationContext 는 ContextConfigLocation 에서 정의한 Bean 설정 파일을 사용해서 웹 애플리케이션에서 사용할 객체를 관리해주는 역할을 함

servlet

애플리케이션의 요청을 처리하는 부분
클라이언트가 요청(Request)을 보내면 그에 따라 요청을 처리할 수 있는 곳으로 넘겨주고, 결과를 서버쪽 응답(Response)을 통해 클라이언트에게 넘겨주는 곳을 정함 -> DispatcherServlet이 이러한 과정을 진행함
Spring에서는 DispatcherServlet 이 모든 요청을 받고, 요청의 URL과 매핑하는 Controller 에게 위임 -> Front-Controller 패턴

	<servlet>
      		<!--
		<servlet-name>: 임의의 servelt 이름을 정의함 
		-->
      		<servlet-name>appServlet</servlet-name>
		<!--
		 <servlet-class>: 매핑할 클래스 파일명을 패키지명을 포함하여 입력해야 됨 
		스프링 MVC는 모든 요청(Request)을 받아 실제 작업은 다른 컴포넌트로 위임하는 
		DispatcherServlet 을 두어 프론트 컨트롤러 패턴으로 디자인되었음
		-->
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      		<!--
		<init-param>: init 파라미터는 지정해준 서블릿 내에서만 사용 가능. Private 지역변수와 유사함
		-->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<!--
	<servlet-mapping>: 상대 URL 경로를 좀 더 쉽게 다루기 위해 기본 URL을 변경할 때 사용
	-> <url-pattern> 에 설정된 값으로 요청이 들어왔을 때,
	 <servlet-name> 에 지정되어 있는 이름의 servlet(appServlet)을 호출하겠다는 의미

 	<url-pattern>: 정의된("/") url이 넘어오면 이 요청을 처리할 서블릿 객체를 넘겨줌 
 	-->

	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

filter

웹 애플리케이션 전반에 걸쳐 특정 URL이나 파일 요청 시 미리 로딩되어 사전에 처리할 작업을 수행(필터링) 하고 해당 요청을 처리하는 웹 애플리케이션의 유형 중 하나

	<filter>
		<filter-name>encoding</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>: 해당 필터를 적용할 name, servlet, url-pattern 등을 지정
	-->
	<filter-mapping>
		<filter-name>enoding</filter-name>
		<servlet-name>appServlet</servlet-name>
	</filter-mapping>

출처
[Spring/스프링] - DispatcherServlet(Front-Controller패턴), web.xml, root-context.xml, servlet-context.xml

0개의 댓글