Class ServletRegistrationBean<T extends Servlet>

Type Parameters:
T - the type of the Servlet to register
All Implemented Interfaces:
ServletContextInitializer, Ordered
Direct Known Subclasses:
DispatcherServletRegistrationBean

public class ServletRegistrationBean<T extends Servlet> extends DynamicRegistrationBean<ServletRegistration.Dynamic>
A ServletContextInitializer to register Servlets in a Servlet 3.0+ container. Similar to the registration features provided by ServletContext but with a Spring Bean friendly design.

The servlet must be specified before calling RegistrationBean.onStartup(jakarta.servlet.ServletContext). URL mapping can be configured used setUrlMappings(java.util.Collection<java.lang.String>) or omitted when mapping to '/*' (unless alwaysMapUrl is set to false). The servlet name will be deduced if not specified.

Since:
1.4.0
Author:
Phillip Webb
See Also:
  • Constructor Details

    • ServletRegistrationBean

      public ServletRegistrationBean()
      Create a new ServletRegistrationBean instance.
    • ServletRegistrationBean

      public ServletRegistrationBean(T servlet, String... urlMappings)
      Create a new ServletRegistrationBean instance with the specified Servlet and URL mappings.
      Parameters:
      servlet - the servlet being mapped
      urlMappings - the URLs being mapped
    • ServletRegistrationBean

      public ServletRegistrationBean(T servlet, boolean alwaysMapUrl, String... urlMappings)
      Create a new ServletRegistrationBean instance with the specified Servlet and URL mappings.
      Parameters:
      servlet - the servlet being mapped
      alwaysMapUrl - if omitted URL mappings should be replaced with '/*'
      urlMappings - the URLs being mapped
  • Method Details

    • setServlet

      public void setServlet(T servlet)
      Sets the servlet to be registered.
      Parameters:
      servlet - the servlet
    • getServlet

      public T getServlet()
      Return the servlet being registered.
      Returns:
      the servlet
    • setUrlMappings

      public void setUrlMappings(Collection<String> urlMappings)
      Set the URL mappings for the servlet. If not specified the mapping will default to '/'. This will replace any previously specified mappings.
      Parameters:
      urlMappings - the mappings to set
      See Also:
    • getUrlMappings

      public Collection<String> getUrlMappings()
      Return a mutable collection of the URL mappings, as defined in the Servlet specification, for the servlet.
      Returns:
      the urlMappings
    • addUrlMappings

      public void addUrlMappings(String... urlMappings)
      Add URL mappings, as defined in the Servlet specification, for the servlet.
      Parameters:
      urlMappings - the mappings to add
      See Also:
    • setLoadOnStartup

      public void setLoadOnStartup(int loadOnStartup)
      Sets the loadOnStartup priority. See ServletRegistration.Dynamic.setLoadOnStartup(int) for details.
      Parameters:
      loadOnStartup - if load on startup is enabled
    • setMultipartConfig

      public void setMultipartConfig(MultipartConfigElement multipartConfig)
      Parameters:
      multipartConfig - the multi-part configuration to set or null
    • getMultipartConfig

      public MultipartConfigElement getMultipartConfig()
      Returns the multi-part configuration to be applied or null.
      Returns:
      the multipart config
    • getDescription

      protected String getDescription()
      Description copied from class: RegistrationBean
      Return a description of the registration. For example "Servlet resourceServlet"
      Specified by:
      getDescription in class RegistrationBean
      Returns:
      a description of the registration
    • addRegistration

      protected ServletRegistration.Dynamic addRegistration(String description, ServletContext servletContext)
      Specified by:
      addRegistration in class DynamicRegistrationBean<ServletRegistration.Dynamic>
    • configure

      protected void configure(ServletRegistration.Dynamic registration)
      Configure registration settings. Subclasses can override this method to perform additional configuration if required.
      Overrides:
      configure in class DynamicRegistrationBean<ServletRegistration.Dynamic>
      Parameters:
      registration - the registration
    • getServletName

      public String getServletName()
      Returns the servlet name that will be registered.
      Returns:
      the servlet name
    • toString

      public String toString()
      Overrides:
      toString in class Object