С Servlet 3.0, http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html
мы можем определить класс с аннотацией WebServlet:
package com.example;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet({"/hello"})
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
response.getWriter().println("hello world");
}
}
и определите файл web.xml без сопоставления сервлета:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>
таким образом, запрос на http://test.com/hello будет правильно печатать "hello world".
это работает, потому что Servlet 3.0 сканирует все классы на наличие аннотации WebServlet.
Есть ли способ ограничить это сканирование классами внутри данного пакета (например, com.example.*)?