Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and when use dynamic servlet registration?

I'm investigating a Spring Boot project generated by JHipster and found out that its request mappings aren't done via web.xml nor via Spring's @RequestMapping but like so:

ServletRegistration.Dynamic someServlet =
                servletContext.addServlet("someServlet", new SomeServlet());
someServlet.addMapping("/someUrl");
someServlet.setAsyncSupported(true); 

My questions are:

  1. Are there any reasonable advantages of dynamic registration instead of classic mapping?
  2. Is it spring-boot's standard of registering mappings or it's just a will of jhipster's owner?
  3. Is someServlet.setAsyncSupported(true) just another way of making response.setHeader("Access-Control-Allow-Origin", "*")?
like image 787
Baurzhan Avatar asked Nov 06 '14 12:11

Baurzhan


People also ask

What is ServletRegistration?

ServletRegistration.DynamicInterface through which a Servlet registered via one of the addServlet methods on ServletContext may be further configured.

What is setLoadOnStartup?

setLoadOnStartup(int loadOnStartup) Sets the loadOnStartup priority on the Servlet represented by this dynamic ServletRegistration. void. setMultipartConfig(MultipartConfigElement multipartConfig) Sets the MultipartConfigElement to be applied to the mappings defined for this ServletRegistration .


1 Answers

  1. Is there any reasonable advantages of dynamic registration instead of classic mapping?

Dynamic servlet registration Servlet 3+ way of registering servlets. In Servlets 3 you can avoid creating web.xml and configure application in pure Java. It gives you some advantages like compile time check if everything is fine there and what's more important since you do it in Java code, you can do some additional checks or conditions - for example register particular servlet only if environment property is set or class is available on the classpath.

It's not a replacement for @RequestMapping. In case of Spring Boot you will use it most probably when you want to register some 3rd party servlet - like Dropwizard Metrics servlet in case of JHipster.

  1. Is it spring-boot's standard of registering mappings or it's just a will of jhipster's owner?

There are at least 2 ways of registering additional servlets in Spring Boot. See answers here: How can I register a secondary servlet with Spring Boot?.

Your own controllers you map as usual with @RequestMapping.

  1. Is someServlet.setAsyncSupported(true) just another way of making response.setHeader("Access-Control-Allow-Origin", "*")?

Nope. For setting this header you use usually CORSFilter (read more: Enabling Cross Origin Requests for a RESTful Web Service). asyncSupported flag is used to make servlet able to process request asynchronously.

like image 107
Maciej Walkowiak Avatar answered Sep 19 '22 10:09

Maciej Walkowiak