[Legacy 프로젝트 공부] web.xml 설정

JEONG SUJIN·2023년 2월 20일
0

web.xml이란 ?

web.xml은 WAS(Web Application Server)가 최초 구동될 때, WEB-INF 디렉토리에 존재하는 web.xml을 인식하도록 각 파일을 알려준다.

브라우저가 Java Servlet에 접근하기 위해서는 WAS에 필요한 정보를 알려줘야 해당하는 Servlet을 호출 할 수 있다.


Web.xml 에서는 크게 DispatcherServlet, ContextLoaderListener, Filter 설정을 한다.

  • appServlet의 경로 설정
  • 인코딩 필터(UTF-8) 한글깨짐 현상을 방지하기 위해 web.xml에 인코딩 filter를 설정해준다.

Spring MVC에서는 사용할 DispatchServlet을 재정의해줘야 하므로,
WEB-INF하위에 web.xml을 만들어야한다.
src/main/webapp/WEB-INF/web.xml
webapp이 아니라 WebContent으로 되어있는 경우도 있다. (버전차이)


웹어플리케이션에서 받아들이는 모든 요청에 대해 appServlet이라는 이름으로 되어있는 서블릿을
사용하겠다는 뜻이며,appServlet은 Spring MVC에서 제공하고 있는 기본 서블릿을 지정했다.

<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<init-param>의 contextConfigLocation은 Spring MVC의
DispatcherServlet에 있는 contextConfigLocation 파라미터로 지정하겠다는 의미로 사용. <servlet></servlet> 태그 안에 사용해야함.

<servlet>
  <!--중간생략-->
<init-param>
        	<param-name>contextConfigLocation</param-name>
        	<param-value>/WEB-INF/config/servlet-context.xml</param-value>
</init-param>
</servlet>

servlet mapping부분도 추가해준다.

<servlet-mapping></servlet-mapping>

root-context.xml과 servlet-context.xml 같은 설정 파일을 어디서 가져올 것인지를 설정해주는 설정을 위한 설정 파일이라고 할 수 있다.

web.xml

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

	<!-- 현재 웹 애플리케이션에서 받아들이는 모든 요청에 대해 appServlet이라는 이름으로 정의되어 있는 서블릿을 사용하겠다. -->
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<!-- 요청 정보를 분석해서 컨트롤러를 선택하는 서블릿을 지정한다. -->
	<servlet>
        <servlet-name>appServlet</servlet-name>
        <!-- Spring MVC에서 제공하고 있는 기본 서블릿을 지정한다. -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- Spring MVC 설정을 위한 xml 파일을 지정한다. -->
        <init-param>
        	<param-name>contextConfigLocation</param-name>
        	<param-value>/WEB-INF/config/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- Bean을 정의할 xml 파일을 지정한다. -->
    <context-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>/WEB-INF/config/root-context.xml</param-value>
    </context-param>
    
    <!-- 리스너설정 -->
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- 파라미터 인코딩 필터 설정 -->
    <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>
    	<init-param>
    		<param-name>forceEncoding</param-name>
    		<param-value>true</param-value>
    	</init-param>
    </filter>
    
    <filter-mapping>
    	<filter-name>encodingFilter</filter-name>
    	<url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>
profile
기록하기

0개의 댓글