How to include other configuration files in web.xml

蹲街弑〆低调 提交于 2019-12-10 13:28:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!