본문 바로가기
Spring/Spring

[Spring] 프레임워크 Web.xml 세팅방법

1. Web.xml 이란?

 

Web.xml은 웹페이지의 환경설정을 위한 파일입니다. 이 파일은 WAS가 최초로 구동될 때 web.xml을 읽고 메모리에 로딩이 되며 모든 Spring 웹어플리케이션은 반드시 하나의 web.xml파일을 가져야만 합니다. 이러한 web.xml 파일의 내부에는 다시 세분화된 설정파일들(dispacherServlet.xml 등등)을 설정하거나 웹에서 사용하는 설정들에 대한 값들이 들어 있습니다. 자세한 내용은 아래의 예시를 통해서 알아보겠습니다.

 

 

 

 

 

2. Web.xml 의 구조

 

아래는 spring프로젝트를 만들면 생성되는 기본 web.xml 파일 입니다. 구역마다 나누어서 한번 살펴보도록 하겠으며 기본으로 생성되는 web.xml파일임으로 이 외에도 자주사용하는 설정들에 대하여 추가적으로 알아보겠습니다.

 

<?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>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Servlet의 환경설정 -->
	<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>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

 

 

 

 

 

 

3. Servlet의 환경설정

 

우선 Servlet의 환경설정 부분을 살펴보겠습니다. 아래의 코드는 가장 대표적인 Servlet인 DispatcherServlet에 대한 설정입니다. 아래의 코드는 *.do로 끝나는 url의 호출이 있을 경우 Servlet-name에 지정되어 있는 이름의 Servlet을 호출하겠다는 의미입니다. 아래의 코드대로라면 *.do에 해당하는 요청이 있을경우 /WEB-INF/spring/appServlet/servlet-context.xml가 호출이 됩니다.

 

	<!-- Servlet의 환경설정 -->
	<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>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

 

 

 

 

 

4. root-context.xml 설정

 

이번에는 root-context.xml 입니다. 정확하게 구분되어진것은 아니지만 servlet에 관련된 설정은 servlet-context에 servlet외의 설정에 관련된 내용은 root-cotext에 적어줍니다.

 

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

 

 

 

 

5. Filter

 

필터는 웹 어플리케이션 전반에 걸쳐 특정 URL이나 파일 요청 시 먼저 로딩되어 사전에 처리할 작업을 수행합니다. 예를들어 UTF-8로 텍스트를 인코딩하거나 스프링 시큐리티관련 내용을 작성할때도 필터를 사용합니다.

 

	<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>/*</url-pattern>
	</filter-mapping>

 

 

 

 

 

6. Session-config

 

web.xml에서 세견에 대한 설정도 가능합니다.

 

<!-- Session 시간설정 (단위:분) -->
<session-config>
    <session-timeout>180</session-timeout>
</session-config>

 

 

 

 

 

7. error-page

 

에러코드에 따른 에러페이지를 설정할 수 있습니다.

 

<error-page>
	<error-code>401</error-code>
	<location>/resources/commons/error/serverError.jsp</location>
</error-page>

 

 

 

 

8. Welcome-file-list

 

웹 어플리케이션 요청 시 시작파일을 지정할 수 있습니다.

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

 

 

 

 

 

댓글