com.vaadin.flow.server.

Class VaadinServlet

  • java.lang.Object
    • javax.servlet.GenericServlet
      • javax.servlet.http.HttpServlet
        • com.vaadin.flow.server.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

    • 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.

      • 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(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 javax.servlet.ServletException,
                                                     IOException

        Handles a request by serving a static file or a file from a WebJar.

        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; oterwise false

        Throws:

        IOException - if an input or output error occurs while the servlet is handling the HTTP request

        javax.servlet.ServletException - if the HTTP request cannot be handled

      • 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

      • getApplicationUrl

        @Deprecated
        protected URL getApplicationUrl(javax.servlet.http.HttpServletRequest request)
                                             throws MalformedURLException

        Deprecated. As of 7.0. Will likely change or be removed in a future version

        Gets the current application URL from request.

        Parameters:

        request - the HTTP request.

        Returns:

        current application URL

        Throws:

        MalformedURLException - if the application is denied access to the persistent data store represented by the given URL.

      • destroy

        public void destroy()

        Specified by:

        destroy in interface javax.servlet.Servlet

        Overrides:

        destroy in class javax.servlet.GenericServlet

      • safeEscapeForHtml

        @Deprecated
        public static String safeEscapeForHtml(String unsafe)

        Deprecated. As of 7.0. Will likely change or be removed in a future version

        Escapes characters to html entities. An exception is made for some "safe characters" to keep the text somewhat readable.

        Parameters:

        unsafe - non-escaped string

        Returns:

        a safe string to be added inside an html tag

      • getWebJarServer

        protected Optional<WebJarServer> getWebJarServer()

        Gets the web jar server.

        Returns:

        the web jar server or an empty optional if no web jar server is used