본문 바로가기

Spring

Spring 1 : web.xml (배포서술자 /Deployment Descriptor)

Web.xml

Spring Framework에서 WAS 최초 구동시 읽어지는 파일로,
서버가 시작되면 web.xml파일이 읽혀 메모리에 올라감

=> client로부터 request를 받은 후 사용자의 요청내용이 web.xml에 등록되어있는 url과 일치하는 경우 dispatcher servlet이 요청을 가로챈 후 서비스 제공

프로젝트/src/main//WebApp/WEB-INF/web.xml 에 위치 

DispatcherServlet, ContextLoadListner, Filter 등을 설정 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>

동일 Web Application에 있는 Servlet이 공유하여 사용가능한 전역변수

서버 구동과 동시에 곧바로 읽어낼 문서인 root-context.xml(최상위 설정파일) 경로 등록

 

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	
	<!-- 서버 구동과 동시에 곧바로 읽어낼 문서인 root-context.xml(최상위 설정파일)의 경로를 등록한 부분 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
		/WEB-INF/spring/root-context.xml
		/WEB-INF/spring/spring-security.xml
		</param-value>
		
	</context-param>

<listner> 모든 서블릿과 필터에 공유될 Spring Container 생성

 

	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

<serlvet> client요청시 요청을 받아줄 DispatcherSerlvet 설정 

<!-- Processes application requests -->
	<!--listener가 귀를 잘 열고 있다가 서버가 켜지면 init-param을 읽는다  -->
	<!-- dispatcher-servlet이 구동될 때 init-param에 담긴 servlet-context.xml 파일을 읽겠다 -->
	<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>
		<load-on-startup>1</load-on-startup>
	</servlet>

<servlet-mapping>

contextPath + "/"로 시작하는 모든 request는 appServlet으로 등록된 DispatcherServlet이 받음

<!--/로 시작하는 모든 요청이 들어올 경우 appServlet에게 보내겠다  -->
	<!-- dispatcher servlet없이는 서버가 돌지 않는다  -->	
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

 

 

<filter>
controller 요청처리 전 공통적으로 실행되어야할 내용을 기술 
클라이언트의 요청을 Controller가 받기 전에가로채서 해당 코드가 선 수행할 수 있도록 함

* 스프링에서 제공해주는 인코딩 필터를 등록
org.springframework.web.filter.CharacterEncodingFilter
필터 등록시 filter태그와 filter-mapping태그를 함께 써야함
꼭 filter-name태그로 filter명 지정해야함

<filter>
	 	<filter-name>encodingFilter</filter-name>
	 	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	 	<!-- 옵션1: 인코딩 방식을 UTF-8로 지정 -->
	 	<init-param>
	 		<param-name>encoding</param-name>	
	 		<param-value>UTF-8</param-value>		 		
	 	</init-param>
	 	<!-- 옵션2: 혹시모를 충돌에 대비하여 강제인코딩 방식도 UTF-8로 지정 -->
	 	<init-param>
	 		<param-name>forceEncoding</param-name>
	 		<param-value>true</param-value>
	 	</init-param>
	</filter>
	
	<!-- 위에서 지정한 encoding필터를 모든 패턴에 적용 -->
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

 

'Spring' 카테고리의 다른 글

MVC (Model-View-Controller) 패턴  (0) 2023.03.29
Spring MVC프로젝트의 폴더 구조  (0) 2023.03.28
Spring2: root-context.xml  (0) 2022.12.29
Spring - Context Hierarchy  (0) 2022.12.29
Web.xml/ JSP Mapping / contextPath/ contextRoot  (0) 2022.12.21