问题
I need to define many servlets, but I don't want to write the configuration all in the web.xml.
Can I define some servlet configuration files, and include them in web.xml? Or is there any other way to split a web.xml to multi files?
回答1:
The Servlet 3.0 specification provides a new annotation, @WebServlet
, that may be used to declare servlets in the code without the need for the web.xml. See Section 8.1.1 of the Servlet 3.0 specification and review the javadoc for more details.
@WebServlet("/myServlet")
public class MyServlet extends HttpServlet {
//...
}
Additionally, Servlet 3.0 introduced the concept of web fragments, which addresses your second question about splitting the web.xml
into multiple files. These fragments can contain a portion (or all) of the web deployment descriptor by including a META-INF/web-fragment.xml
file and/or servlet annotations in jar files within your web module's WEB-INF/lib
directory. See Section 8.2 of the Servlet 3.0 specification for more details.
回答2:
The Servlet 3.0 specification allows for declaring servlets through Java annotations - so no entries required within the web.xml
file. Other than that, I'm not aware of any "include" functionality.
来源:https://stackoverflow.com/questions/8380440/how-to-include-other-configuration-files-in-web-xml