Package com.vaadin.flow.server
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 Summary
Fields inherited from class jakarta.servlet.http.HttpServlet
LEGACY_DO_HEAD
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected DeploymentConfiguration
Creates a deployment configuration to be used for the creation of aVaadinService
.protected DeploymentConfiguration
createDeploymentConfiguration
(Properties initParameters) Creates a deployment configuration to be used for the creation of aVaadinService
.protected VaadinServletService
Creates a vaadin servlet service.protected VaadinServletService
createServletService
(DeploymentConfiguration deploymentConfiguration) Creates a vaadin servlet service.protected StaticFileHandler
createStaticFileHandler
(VaadinService vaadinService) Creates a new instance ofStaticFileHandler
, that is responsible to find and serve static resources.protected VaadinServletRequest
createVaadinRequest
(jakarta.servlet.http.HttpServletRequest request) Creates a Vaadin request for a http servlet request.void
destroy()
static VaadinServlet
Gets the currently used Vaadin servlet.static String
For internal use only.protected static String
Finds any path parameter added to the last part of the uri.Gets the Vaadin service for this servlet.jakarta.servlet.ServletConfig
protected boolean
handleContextOrServletRootWithoutSlash
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) 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.void
init
(jakarta.servlet.ServletConfig servletConfig) Called by the servlet container to indicate to a servlet that the servlet is being placed into service.protected boolean
serveStaticOrWebJarRequest
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) 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.protected void
service
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Receives standard HTTP requests from the public service method and dispatches them.protected void
static void
whenFrontendMappingAvailable
(Runnable runnable) Runs the given runnable when frontend mapping is available.Methods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
Methods inherited from class jakarta.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletContext, getServletInfo, getServletName, init, log, log
-
Field Details
-
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 interfacejakarta.servlet.Servlet
- Overrides:
init
in classjakarta.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
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 interfacejakarta.servlet.Servlet
- Overrides:
getServletConfig
in classjakarta.servlet.GenericServlet
-
createStaticFileHandler
Creates a new instance ofStaticFileHandler
, that is responsible to find and serve static resources. By default it returns aStaticFileServer
instance.- Parameters:
vaadinService
- the vaadinService created atcreateServletService()
- 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
Gets the currently used Vaadin servlet. The current servlet is automatically defined when initializing the servlet and when processing requests to the server (seeThreadLocal
) and inVaadinSession.access(Command)
andUI.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.ServletExceptionCreates a deployment configuration to be used for the creation of aVaadinService
. Intended to be used by dependency injection frameworks.- Returns:
- the created deployment configuration
- Throws:
jakarta.servlet.ServletException
-
createDeploymentConfiguration
Creates a deployment configuration to be used for the creation of aVaadinService
. 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, ServiceExceptionCreates a vaadin servlet service. This method functions as a layer of indirection betweeninit(ServletConfig)
andcreateServletService(DeploymentConfiguration)
so dependency injection frameworks can callcreateDeploymentConfiguration()
when creating a vaadin servlet service lazily.- Returns:
- the created vaadin servlet service
- Throws:
jakarta.servlet.ServletException
- if creating a deployment configuration failsServiceException
- 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 classjakarta.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 viaVaadinService
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 servletresponse
- 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; otherwisefalse
- 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 requestresponse
- 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
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
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
Gets the Vaadin service for this servlet.- Returns:
- the Vaadin service
-
destroy
public void destroy()- Specified by:
destroy
in interfacejakarta.servlet.Servlet
- Overrides:
destroy
in classjakarta.servlet.GenericServlet
-
getFrontendMapping
For internal use only.- Returns:
- the vaadin servlet used for frontend files in development mode
-