简体   繁体   English

以编程方式设置web.xml配置

[英]Set web.xml configuration programmatically

Since Servlet 3.0 were introduced the annotations: 自从Servlet 3.0引入了注释:

  • @WebListener
  • @WebServlet
  • @WebFilter
  • @WebInitParam

So is it possible to completely remove the web.xml and set all cofigurations contained in it programmatically? 那么是否可以完全删除web.xml并以编程方式设置其中包含的所有配置? If this is true, is it a good practice or should it be avoided? 如果这是真的,这是一个好的做法还是应该避免?

edit: In the link provided by farrellmr is used org.springframework.web.WebApplicationInitializer; 编辑:在farrellmr提供的链接中使用org.springframework.web.WebApplicationInitializer; , but I'm looking for a general approach without the use of a particular framework. ,但我正在寻找一种不使用特定框架的通用方法。

Q: So is it possible to completely remove the web.xml and set all cofigurations contained in it programmatically? 问:那么可以完全删除web.xml并以编程方式设置其中包含的所有配置吗?

Yes, starting with Servlet 3.0, you can have a Web application without web.xml file. 是的,从Servlet 3.0开始,您可以拥有一个没有web.xml文件的Web应用程序。

Q: If this is true, is it a good practice or should it be avoided? 问:如果这是真的,这是一个好的做法还是应该避免?

It depends. 这取决于。 A drawback of having web.xml in a large Web application with many servlets is that this file becomes larger and larger, to the point that is hard to maintain. 在具有许多servlet的大型Web应用程序中使用web.xml一个缺点是该文件变得越来越大,难以维护。 On the other hand, you may have a specific role in your team, a deployer, who does not need to know how to program. 另一方面,您可能在您的团队中有一个特定的角色,一个部署者,他不需要知道如何编程。 With web.xml , he can easily change eg URL of the servlet by changing the content of the file and simply redeploying the application. 使用web.xml ,他可以通过更改文件的内容并简单地重新部署应用程序来轻松更改servlet的URL。 Finally, be aware that Servlet 3.0 is supported only by Tomcat7+ and other, "newer", application servers / servlet containers. 最后,请注意,只有Tomcat7 +和其他“更新”的应用程序服务器/ servlet容器才支持Servlet 3.0。 Our team once had a client with Tomcat6 and they complained about some strange errors, which you can guess, were caused by using annotations instead of web.xml . 我们的团队曾经有一个Tomcat6的客户端,他们抱怨一些奇怪的错误,你可以猜测,这是由使用注释而不是web.xml引起的。 That said, I would still prefer annotations over an additional configuration file :) 也就是说,我仍然更喜欢注释而不是额外的配置文件:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM