Class VaadinServlet

  • All Implemented Interfaces:
    Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

    public class VaadinServlet
    extends javax.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:
    Serialized Form
    • Field Detail

      • INTERNAL_VAADIN_SERVLET_VITE_DEV_MODE_FRONTEND_PATH

        public static final String INTERNAL_VAADIN_SERVLET_VITE_DEV_MODE_FRONTEND_PATH
        See Also:
        Constant Field Values
    • Constructor Detail

      • VaadinServlet

        public VaadinServlet()
    • Method Detail

      • init

        public void init​(javax.servlet.ServletConfig servletConfig)
                  throws javax.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 javax.servlet.Servlet
        Overrides:
        init in class javax.servlet.GenericServlet
        Parameters:
        servletConfig - the object containing the servlet's configuration and initialization parameters
        Throws:
        javax.servlet.ServletException - if an exception has occurred that interferes with the servlet's normal operation.
      • getServletConfig

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

        protected void servletInitialized()
                                   throws javax.servlet.ServletException
        Throws:
        javax.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 javax.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:
        javax.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​(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​(javax.servlet.http.HttpServletRequest request,
                               javax.servlet.http.HttpServletResponse response)
                        throws javax.servlet.ServletException,
                               IOException
        Receives standard HTTP requests from the public service method and dispatches them.
        Overrides:
        service in class javax.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:
        javax.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​(javax.servlet.http.HttpServletRequest request,
                                                     javax.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​(javax.servlet.http.HttpServletRequest request,
                                                                 javax.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​(javax.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 javax.servlet.Servlet
        Overrides:
        destroy in class javax.servlet.GenericServlet
      • getFrontendMapping

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