[JAVA]
[java] servlet 등록 방법
handr95
2020. 11. 4. 00:42
반응형
1. @webservlet
import javax.servlet.annotation.WebServlet;
@WebServlet("/")
public class MainApi extends MainServlet {
}
2. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="management" version="3.0">
<servlet>
<servlet-name>ServletTest</servlet-name>
<servlet-class>MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
1과 2 모두 /로 시작 하는 api요청시 MainServlet을 통해 처리됨.
1과 2 함께 등록 되면 에러 발생 -> 동일한 url-mapping을 사용할 경우 둘 중 하나만 등록해줘야함.
Caused by: java.lang.IllegalArgumentException: The servlets named [ServletTest] and [MainApi] are both mapped to the url-pattern [/] which is not permitted
반응형