Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use servlet 3.1 in spring mvc?

There are 2 different features available:

  1. servlet 3.0 allows to process request in a thread different from the container thread.

  2. servlet 3.1 allows to read/write into socket without blocking reading/writing thread

There are a lot of examples in the internet about servlet 3.0 feature. We can use it in Spring very easily. We just have to return DefferedResult or CompletableFuture

But I can't find example of usage servlet 3.1 in spring. As far as I know we have to register WriteListener and ReadListener and do dome dirty work inside. But I can't find the example of that Listeners. I believe it is not very easy.

Could you please provide example of servlet 3.1 feature in spring with explanation of Listener implementaion ?

like image 611
gstackoverflow Avatar asked Jul 01 '19 12:07

gstackoverflow


People also ask

Does Spring MVC use servlets?

Yes it is. Spring-MVC uses DispatcherServlet under the hood. Central dispatcher for HTTP request handlers/controllers, e.g. for web UI controllers or HTTP-based remote service exporters.

Which servlet is used in Spring MVC?

In Spring-MVC when you write annotation like @Controller, indirectly you are using a Servlet called Dispatcher Servlet. Dispatcher Servlet is defined in web. xml file with properties and class name which is mapped to . jsp pages and Controller part.

Can you deploy spring boot application to any servlet 3.1 compatible container?

You can also deploy Spring Boot applications to any Servlet 3.1+ compatible container.

What is the use of servlet xml in Spring MVC?

spring-servlet. xml. In Spring, a single front servlet takes the incoming requests and delegates them to appropriate controller methods. The front servlet, based on a Front controller design pattern, handles all the HTTP requests of a particular web application.


1 Answers

For servlet 3.1 you can support non-blocking I/O by using Reactive Streams bridge

Servlet 3.1+ Container

To deploy as a WAR to any Servlet 3.1+ container, you can extend and include {api-spring-framework}/web/server/adapter/AbstractReactiveWebInitializer.html[AbstractReactiveWebInitializer] in the WAR. That class wraps an HttpHandler with ServletHttpHandlerAdapter and registers that as a Servlet.

So you should extend AbstractReactiveWebInitializer which is adding async support

registration.setAsyncSupported(true);

And the support in ServletHttpHandlerAdapter

AsyncContext asyncContext = request.startAsync();
like image 88
user7294900 Avatar answered Oct 19 '22 11:10

user7294900