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
throughcreateServletService(DeploymentConfiguration)
and delegates handling of most requests to that.Since:
1.0
Author:
Vaadin Ltd
See Also:
-
-
Constructor Summary
Constructors Constructor Description VaadinServlet()
-
Method Summary
All Methods Modifier and Type Method Description protected DeploymentConfiguration
createDeploymentConfiguration()
Creates a deployment configuration to be used for the creation of a
VaadinService
.protected DeploymentConfiguration
createDeploymentConfiguration(Properties initParameters)
Creates a deployment configuration to be used for the creation of a
VaadinService
.protected VaadinServletService
createServletService()
Creates a vaadin servlet service.
protected VaadinServletService
createServletService(DeploymentConfiguration deploymentConfiguration)
Creates a vaadin servlet service.
protected StaticFileHandler
createStaticFileHandler(VaadinService vaadinService)
Creates a new instance of
StaticFileHandler
, that is responsible to find and serve static resources.protected VaadinServletRequest
createVaadinRequest(javax.servlet.http.HttpServletRequest request)
Creates a Vaadin request for a http servlet request.
void
destroy()
static VaadinServlet
getCurrent()
Gets the currently used Vaadin servlet.
protected static String
getLastPathParameter(String uri)
Finds any path parameter added to the last part of the uri.
VaadinServletService
getService()
Gets the Vaadin service for this servlet.
javax.servlet.ServletConfig
getServletConfig()
protected boolean
handleContextOrServletRootWithoutSlash(javax.servlet.http.HttpServletRequest request, javax.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(javax.servlet.ServletConfig servletConfig)
Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
protected boolean
serveStaticOrWebJarRequest(javax.servlet.http.HttpServletRequest request, javax.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(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Receives standard HTTP requests from the public service method and dispatches them.
protected void
servletInitialized()
-
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
-
-
-
-
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 interfacejavax.servlet.Servlet
Overrides:
init
in classjavax.servlet.GenericServlet
Parameters:
servletConfig
- the object containing the servlet's configuration and initialization parametersThrows:
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 interfacejavax.servlet.Servlet
Overrides:
getServletConfig
in classjavax.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 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 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 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 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 propertiesReturns:
the created deployment configuration
-
createServletService
protected VaadinServletService createServletService() throws javax.servlet.ServletException, ServiceException
Creates a vaadin servlet service. This method functions as a layer of indirection between
init(ServletConfig)
andcreateServletService(DeploymentConfiguration)
so dependency injection frameworks can callcreateDeploymentConfiguration()
when creating a vaadin servlet service lazily.Returns:
the created vaadin servlet service
Throws:
javax.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 usedReturns:
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 classjavax.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 servletresponse
- the HTTP servlet response object that contains the response the servlet returns to the clientReturns:
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(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 requestresponse
- the processed responseReturns:
true
if a redirect has been sent and the request should not be processed further;false
if the request should be processed as usualThrows:
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 URIReturns:
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 requestReturns:
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 interfacejavax.servlet.Servlet
Overrides:
destroy
in classjavax.servlet.GenericServlet
-
-