I haven't been working on real world web projects. At university we used both Servlets and Spring for Java web development. In both projects we were given web.xml files already configured and we were doing only minor changes in them. Now I need to build a web app from a scratch. I created new Servlet class in Eclipse and it didn't automatically create any web.xml. Then I googled, and I read from several resources that web.xml is not really needed, but that reasoning was put in couple of sentences, so I am not sure if using annotations instead of web.xml will be no problem. I will be really glad if there is no need to configure web.xml, because I haven't configured one by myself and I want to focus more on the business logic.
Thank you in advance!
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. For example: the doGet() method for HTTP GET requests.
Spring Boot takes an opinionated view of the Spring platform and third-party libraries, allowing us to get started with a minimal configuration. For example we can develop a Jave EE based web application without any configuration files. Not even a web. xml file is required!
Whenever we create a new jsp file, do we need to make an entry in web. xml file? No, you don't need, jsp file can be directly invoked by URL.
web. xml is used as a starting point while tomcat container starts. If you want to bypass this, you can write a simple Java program and inside main() function just call Spring to load application context and provide the path of your apps applicationContext.
You don't need a web.xml
file if you have a container that supports the latest j2ee specs. Here is a link to an simple servlet example that use an annotation and here you can find the same for Spring MVC; I post the example here for you convenience
public class MyWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet()); registration.setLoadOnStartup(1); registration.addMapping("/example/*"); } }
Here is another link that show how to use the other annotations available(@ServletFilter, @WebServletContextListener); you can download the specs form here in order to get a more detailed view of the annotations available via j2ee.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With