Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include other configuration files in web.xml

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?

like image 880
Freewind Avatar asked Dec 05 '11 02:12

Freewind


People also ask

Can we have multiple configuration files?

Yes, in large projects, having multiple Spring configurations increase maintainability and modularity. You can also upload one XML file that will contain all configs.

Can we have two WEB xml files for a Web application?

Only one web. xml inside WEB-INF folder.

What should be included in WEB xml?

web. xml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method (e.g. the doGet() method for HTTP GET requests).


2 Answers

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.

like image 178
shelley Avatar answered Sep 28 '22 08:09

shelley


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.

like image 27
ziesemer Avatar answered Sep 28 '22 07:09

ziesemer