웹개발/BackEnd
web.xml이란?
토킹포테토
2022. 2. 19. 00:00
728x90
1. web.xml이란?
: web application의 설정을 위한 파일.
2. web.xml의 역할
- Servlet 정보 설정
- 브라우저가 JAVA Servlet에 접근하기 위해서는 WAS에 필요한 정보를 알려줘야 해당하는 Servlet 호출을 할 수 있는데,
이때 참조하는 것이 web.xml
3. web.xml의 내용
- 배포할 Servlet이 무엇인지
- 해당 Servlet이 어떤 url에 매핑되는지
* (예시) spring MVC에서의 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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<!--아래 설정 파일에 등록된 bean들은 모두 공유가 가능-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*-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>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name> //servlet-name은 반드시 동일해야 한다
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 매핑-->
<servlet-mapping>
<servlet-name>appServlet</servlet-name> //servlet-name은 반드시 동일해야 한다
<url-pattern>/</url-pattern> //클라이언트의 요청 url에서 프로젝트 이름 뒤에 오는 부분으로, 슬래시로 시작해야 한다
</servlet-mapping>
</web-app>
* (참고) servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<!--어노테이션 활성화-->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!--정적인 데이터 위치 매핑
- 컨트롤러가 처리할 필요 없이 해당 위치의 디렉터리에서 바로 접근 가능.
- http get요청에서의 정적인 데이터에 바로 매핑 가능
-->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!--
컴포넌트 패키지 설정
: com.spring.example 패키지를 스캔하며 annotation이 달린 것을 bean으로 생성
-->
<context:component-scan base-package="com.spring.example" />
</beans:beans>