如何在 web.xml 中包含其他配置文件 [英] How to include other configuration files in web.xml
问题描述
我需要定义很多servlet,但我不想把配置都写在web.xml中.
I need to define many servlets, but I don't want to write the configuration all in the web.xml.
我可以定义一些 servlet 配置文件,并将它们包含在 web.xml 中吗?或者有没有其他方法可以将 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?
推荐答案
Servlet 3.0 规范提供了一个新的注解,@WebServlet
,可用于在代码中声明 servlet,而无需web.xml.请参阅 Servlet 3.0 规范 的第 8.1.1 节并查看 javadoc 了解更多详情.
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 {
//...
}
此外,Servlet 3.0 引入了 Web 片段的概念,它解决了关于将 web.xml
拆分为多个文件的第二个问题.通过在 Web 模块的 中的 jar 文件中包含
目录.有关详细信息,请参阅 Servlet 3.0 规范的第 8.2 节.META-INF/web-fragment.xml
文件和/或 servlet 注释,这些片段可以包含 Web 部署描述符的一部分(或全部)WEB-INF/lib
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.
这篇关于如何在 web.xml 中包含其他配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!