In Spring Boot you can register Servlets, Filters, Listeners by using the ServletRegistrationBean, FilterRegistrationBean, and ServletListenerRegistrationBean bean definitions.
For example, consider the following Servlet example:
package com.example; import org.springframework.stereotype.Component; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component public class DemoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("called Servlet!"); } }
We can register DemoServlet using ServletRegistrationBean and map it to the URL pattern /myServlet, as shown in this example:
package com.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Autowired private FilterServlet myServlet; @Bean public ServletRegistrationBean<DemoServlet> myServletBean() { ServletRegistrationBean<DemoServlet> servlet = new ServletRegistrationBean<>(); servlet.setServlet(myServlet); servlet.addUrlMappings("/myServlet"); return servlet; } }
You can check that the DemoServlet is invoked, if you try to request http://localhost:8080/myServlet
called Servlet!
By using this approach, you can take advantage of Spring’s dependency injection for servlets, filters, and listeners.
Implementing a Servlet Filter
In order to implement a Servlet Filter for your Requests, you can register the Filter on the org.springframework.boot.web.servlet.FilterRegistrationBean. this allows choosing the URL Patterns that binds the filter to the request. For example, here is how to activate the doFilter method when the URL pattern /demo is requested:
package com.example; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class RequestFilter implements Filter { @Bean public FilterRegistrationBean<RequestFilter> loggingFilter() { FilterRegistrationBean<RequestFilter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new RequestFilter()); registrationBean.addUrlPatterns("/demo"); return registrationBean; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; System.out.println("Called request " + req.getRequestURI()); chain.doFilter(request, response); System.out.println("Response :" + res.getContentType()); } }
Please note that you can also annotate this class with @Component instead of @Bean but in this case the Filter will be execute for every request:
@Component public class RequestFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; System.out.println("Called request " + req.getRequestURI()); chain.doFilter(request, response); System.out.println("Response :" + res.getContentType()); } }Found the article helpful? if so please follow us on Socials