Class VaadinServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
com.vaadin.flow.server.VaadinServlet
All Implemented Interfaces:
jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable
Direct Known Subclasses:
SpringServlet

public class VaadinServlet extends jakarta.servlet.http.HttpServlet
The main servlet, which handles all incoming requests to the application.

This servlet is typically subclassed in all applications to provide servlet mappings and init parameters. Together with a web.xml file, it is also possible to use this class directly.

Internally sets up a VaadinService through createServletService(DeploymentConfiguration) and delegates handling of most requests to that.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Field Details

    • INTERNAL_VAADIN_SERVLET_VITE_DEV_MODE_FRONTEND_PATH

      public static final String INTERNAL_VAADIN_SERVLET_VITE_DEV_MODE_FRONTEND_PATH
      See Also:
  • Constructor Details

    • VaadinServlet

      public VaadinServlet()
  • Method Details

    • init

      public void init(jakarta.servlet.ServletConfig servletConfig) throws jakarta.servlet.ServletException
      Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
      Specified by:
      init in interface jakarta.servlet.Servlet
      Overrides:
      init in class jakarta.servlet.http.HttpServlet
      Parameters:
      servletConfig - the object containing the servlet's configuration and initialization parameters
      Throws:
      jakarta.servlet.ServletException - if an exception has occurred that interferes with the servlet's normal operation.
    • whenFrontendMappingAvailable

      public static void whenFrontendMappingAvailable(Runnable runnable)
      Runs the given runnable when frontend mapping is available.
      Parameters:
      runnable - the runnable to run
    • getServletConfig

      public jakarta.servlet.ServletConfig getServletConfig()
      Specified by:
      getServletConfig in interface jakarta.servlet.Servlet
      Overrides:
      getServletConfig in class jakarta.servlet.GenericServlet
    • createStaticFileHandler

      protected StaticFileHandler createStaticFileHandler(VaadinService vaadinService)
      Creates a new instance of StaticFileHandler, that is responsible to find and serve static resources. By default it returns a StaticFileServer instance.
      Parameters:
      vaadinService - the vaadinService created at createServletService()
      Returns:
      the file server to be used by this servlet, not null
    • servletInitialized

      protected void servletInitialized() throws jakarta.servlet.ServletException
      Throws:
      jakarta.servlet.ServletException
    • getCurrent

      public static VaadinServlet getCurrent()
      Gets the currently used Vaadin servlet. The current servlet is automatically defined when initializing the servlet and when processing requests to the server (see ThreadLocal) and in VaadinSession.access(Command) and UI.access(Command). In other cases, (e.g. from background threads), the current servlet is not automatically defined.

      The current servlet is derived from the current service using VaadinService.getCurrent()

      Returns:
      the current Vaadin servlet instance if available, otherwise null
    • createDeploymentConfiguration

      protected DeploymentConfiguration createDeploymentConfiguration() throws jakarta.servlet.ServletException
      Creates a deployment configuration to be used for the creation of a VaadinService. Intended to be used by dependency injection frameworks.
      Returns:
      the created deployment configuration
      Throws:
      jakarta.servlet.ServletException
    • createDeploymentConfiguration

      protected DeploymentConfiguration createDeploymentConfiguration(Properties initParameters)
      Creates a deployment configuration to be used for the creation of a VaadinService. Override this if you want to override certain properties.
      Parameters:
      initParameters - the context-param and init-param values as properties
      Returns:
      the created deployment configuration
    • createServletService

      protected VaadinServletService createServletService() throws jakarta.servlet.ServletException, ServiceException
      Creates a vaadin servlet service. This method functions as a layer of indirection between init(ServletConfig) and createServletService(DeploymentConfiguration) so dependency injection frameworks can call createDeploymentConfiguration() when creating a vaadin servlet service lazily.
      Returns:
      the created vaadin servlet service
      Throws:
      jakarta.servlet.ServletException - if creating a deployment configuration fails
      ServiceException - if creating the vaadin servlet service fails
    • createServletService

      protected VaadinServletService createServletService(DeploymentConfiguration deploymentConfiguration) throws ServiceException
      Creates a vaadin servlet service.
      Parameters:
      deploymentConfiguration - the deployment configuration to be used
      Returns:
      the created vaadin servlet service
      Throws:
      ServiceException - if creating the vaadin servlet service fails
    • service

      protected void service(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      Receives standard HTTP requests from the public service method and dispatches them.
      Overrides:
      service in class jakarta.servlet.http.HttpServlet
      Parameters:
      request - the object that contains the request the client made of the servlet.
      response - the object that contains the response the servlet returns to the client.
      Throws:
      jakarta.servlet.ServletException - if an input or output error occurs while the servlet is handling the TRACE request.
      IOException - if the request for the TRACE cannot be handled.
    • serveStaticOrWebJarRequest

      protected boolean serveStaticOrWebJarRequest(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException
      Handles a request by serving a static file from Webpack when in npm-dev-mode, or from a WebJar when in bower-dev-mode or from the file-system when in production. It's not done via VaadinService handlers because static requests do not need a established session.
      Parameters:
      request - the HTTP servlet request object that contains the request the client made of the servlet
      response - the HTTP servlet response object that contains the response the servlet returns to the client
      Returns:
      true if the request was handled a response written; otherwise false
      Throws:
      IOException - if an input or output error occurs while the servlet is handling the HTTP request
    • handleContextOrServletRootWithoutSlash

      protected boolean handleContextOrServletRootWithoutSlash(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException
      Invoked for every request to this servlet to potentially send a redirect to avoid problems with requests to the context root with no trailing slash.
      Parameters:
      request - the processed request
      response - the processed response
      Returns:
      true if a redirect has been sent and the request should not be processed further; false if the request should be processed as usual
      Throws:
      IOException - If an input or output exception occurs
    • getLastPathParameter

      protected static String getLastPathParameter(String uri)
      Finds any path parameter added to the last part of the uri. A path parameter is any string separated by ";" from the path and ends in / or at the end of the string.

      For example the uri http://myhost.com/foo;a=1/bar;b=1 contains two path parameters, a=1 related to /foo and b=1 related to /bar.

      For http://myhost.com/foo;a=1/bar;b=1 this method will return ;b=1

      Parameters:
      uri - a URI
      Returns:
      the last path parameter of the uri including the semicolon or an empty string. Never null.
    • createVaadinRequest

      protected VaadinServletRequest createVaadinRequest(jakarta.servlet.http.HttpServletRequest request)
      Creates a Vaadin request for a http servlet request. This method can be overridden if the Vaadin request should have special properties.
      Parameters:
      request - the original http servlet request
      Returns:
      a Vaadin request for the original request
    • getService

      public VaadinServletService getService()
      Gets the Vaadin service for this servlet.
      Returns:
      the Vaadin service
    • destroy

      public void destroy()
      Specified by:
      destroy in interface jakarta.servlet.Servlet
      Overrides:
      destroy in class jakarta.servlet.GenericServlet
    • getFrontendMapping

      public static String getFrontendMapping()
      For internal use only.
      Returns:
      the vaadin servlet used for frontend files in development mode