Using Servlet Context in Quarkus

Quarkus includes the undertow extension, therefore it’s fully capable of delivering Servlet applications. In this example, we will show how to use the ServletContext in a REST Application. One challenge is that you cannot @Inject the ServletContext directly as you would do it in a Jave EE environment.

Nevertheless you can count on the JAX-RS javax.ws.rs.core.Context annotation to do that:

@Controller
@Path("/greeting/")
@RequestScoped
public class GreetingResource {

    @Context
    ServletContext servletContext;

}

 Or, as an alternative you can also do that:

@Controller
@Path("/rest/")
@RequestScoped
public class HelloResource {

    @GET
    @Path("/hello")
    @Produces({ MediaType.TEXT_PLAIN })
    public Response hello(@Context ServletContext servletContext) {
        // 
    }
}

 


Recommended Articles

Quarkus REST Client API Tutorial: MicroProfile REST Client Implementation

Learn how to develop Quarkus REST Client using MicroProfile REST Client with a basic example and configuration.

Query Quarkus REST Service with Ajax and jQuery

Learn how to create an Ajax front-end to a Quarkus REST application using jQuery and query a sample REST service running on Quarkus 0.16.1

Quarkus 3: Exploring New Reactive REST Features with Mutiny

Discover how Quarkus 3 leverages Mutiny for reactive REST services in modern applications. #Quarkus #ReactiveJava #Middleware #CloudNative

Create a Quarkus REST CRUD Application with Hibernate Panache and REST Data Panache - Tutorial

Learn how to create a robust REST CRUD application in Quarkus using Hibernate Panache and REST Data Panache. #Quarkus #HibernatePanache #RESTDataPanache