Package org.eclipse.jetty.servlet
Class ServletHandler
- All Implemented Interfaces:
Handler
,HandlerContainer
,Container
,Destroyable
,Dumpable
,Dumpable.DumpableContainer
,LifeCycle
Servlet HttpHandler.
This handler maps requests to servlets that implement the javax.servlet.http.HttpServlet API.
This handler does not implement the full J2EE features and is intended to
be used directly when a full web application is not required. If a Web application is required,
then this handler should be used as part of a org.eclipse.jetty.webapp.WebAppContext
.
Unless run as part of a ServletContextHandler
or derivative, the initialize()
method must be called manually after start().
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
(package private) static class
static class
Nested classes/interfaces inherited from class org.eclipse.jetty.server.handler.AbstractHandler
AbstractHandler.ErrorDispatchHandler
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
private boolean
protected final ConcurrentMap<String,
javax.servlet.FilterChain>[] private ServletContextHandler
private boolean
private boolean
private FilterMapping[]
private final Map
<String, FilterHolder> private MultiMap
<FilterMapping> private List
<FilterMapping> private FilterHolder[]
private IdentityService
private boolean
private ListenerHolder[]
private int
private int
private int
private javax.servlet.ServletContext
private ServletMapping[]
private final Map
<String, ServletHolder> private PathMappings
<ServletHolder> private ServletHolder[]
private boolean
private List
<FilterMapping> private static final Logger
Fields inherited from class org.eclipse.jetty.server.handler.ScopedHandler
_nextScope, _outerScope
Fields inherited from class org.eclipse.jetty.server.handler.HandlerWrapper
_handler
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDeprecated.void
addFilter
(FilterHolder filter) Convenience method to add a preconstructed FilterHoldervoid
addFilter
(FilterHolder filter, FilterMapping filterMapping) Convenience method to add a filter and mappingvoid
addFilterMapping
(FilterMapping mapping) Convenience method to add a preconstructed FilterMappingaddFilterWithMapping
(Class<? extends javax.servlet.Filter> filter, String pathSpec, int dispatches) Convenience method to add a filter.addFilterWithMapping
(Class<? extends javax.servlet.Filter> filter, String pathSpec, EnumSet<javax.servlet.DispatcherType> dispatches) Convenience method to add a filter.addFilterWithMapping
(String className, String pathSpec, int dispatches) Convenience method to add a filter.addFilterWithMapping
(String className, String pathSpec, EnumSet<javax.servlet.DispatcherType> dispatches) Convenience method to add a filter.void
addFilterWithMapping
(FilterHolder holder, String pathSpec, int dispatches) Convenience method to add a filter.void
addFilterWithMapping
(FilterHolder holder, String pathSpec, EnumSet<javax.servlet.DispatcherType> dispatches) Convenience method to add a filter.void
addListener
(ListenerHolder listener) Add a holder for a listenervoid
addServlet
(ServletHolder holder) Convenience method to add a pre-constructed ServletHolder.void
addServletMapping
(ServletMapping mapping) Convenience method to add a pre-constructed ServletMapping.addServletWithMapping
(Class<? extends javax.servlet.Servlet> servlet, String pathSpec) Convenience method to add a servlet.addServletWithMapping
(String className, String pathSpec) Convenience method to add a servlet.void
addServletWithMapping
(ServletHolder servlet, String pathSpec) Convenience method to add a servlet.protected boolean
containsFilterHolder
(FilterHolder holder) protected boolean
containsServletHolder
(ServletHolder holder) (package private) void
destroyFilter
(javax.servlet.Filter filter) (package private) void
destroyListener
(EventListener listener) (package private) void
destroyServlet
(javax.servlet.Servlet servlet) void
doHandle
(String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Do the handler work within the scope.void
doScope
(String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Scope the handlerprotected void
doStart()
Starts the managed lifecycle beans in the order they were added.protected void
doStop()
Stops the managed lifecycle beans in the reverse order they were added.void
dump
(Appendable out, String indent) Dump this object (and children) into an Appendable using the provided indent after any new lines.protected javax.servlet.FilterChain
getFilterChain
(Request baseRequest, String pathInContext, ServletHolder servletHolder) getHolderEntry
(String target) Deprecated.protected IdentityService
getMappedServlet
(String target) ServletHolder matching path.int
getServlet
(String name) javax.servlet.ServletContext
getServletMapping
(String pathSpec) Get the ServletMapping matching the pathgetServlets
(Class<?> clazz) void
Initialize filters and load-on-startup servlets.protected void
initializeHolders
(BaseHolder<?>[] holders) protected FilterMapping[]
insertFilterMapping
(FilterMapping mapping, int pos, boolean before) Insert a filtermapping in the listprotected void
boolean
boolean
boolean
isDumpable
(Object o) boolean
boolean
boolean
boolean
protected javax.servlet.FilterChain
newFilterChain
(FilterHolder filterHolder, javax.servlet.FilterChain chain) Create a FilterChain that calls the passed filter with the passed chainnewFilterHolder
(Source source) newListenerHolder
(Source source) newServletHolder
(Source source) Add a new servlet holderprotected void
notFound
(Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) void
prependFilterMapping
(FilterMapping mapping) Convenience method to add a preconstructed FilterMappingvoid
setAllowDuplicateMappings
(boolean allowDuplicateMappings) void
setEnsureDefaultServlet
(boolean ensureDefaultServlet) void
setFilterChainsCached
(boolean filterChainsCached) void
setFilterMappings
(FilterMapping[] filterMappings) void
setFilters
(FilterHolder[] holders) void
setListeners
(ListenerHolder[] listeners) void
setMaxFilterChainsCacheSize
(int maxFilterChainsCacheSize) Set the maximum filter chain cache size.void
setServletMappings
(ServletMapping[] servletMappings) void
setServlets
(ServletHolder[] holders) Set Servlets.setServletSecurity
(javax.servlet.ServletRegistration.Dynamic registration, javax.servlet.ServletSecurityElement servletSecurityElement) void
setStartWithUnavailable
(boolean start) protected void
Starts the given lifecycle.protected void
Stops the given lifecycle.protected void
protected void
Methods inherited from class org.eclipse.jetty.server.handler.ScopedHandler
handle, nextHandle, nextScope
Methods inherited from class org.eclipse.jetty.server.handler.HandlerWrapper
destroy, expandChildren, getHandler, getHandlers, insertHandler, setHandler
Methods inherited from class org.eclipse.jetty.server.handler.AbstractHandlerContainer
doShutdown, expandHandler, findContainerOf, getChildHandlerByClass, getChildHandlers, getChildHandlersByClass, setServer
Methods inherited from class org.eclipse.jetty.server.handler.AbstractHandler
doError, getServer
Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, dump, dump, dump, dump, dumpBeans, dumpObject, dumpObjects, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, unmanage, updateBean, updateBean, updateBeans
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle
addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
-
Field Details
-
LOG
-
__DEFAULT_SERVLET
- See Also:
-
_contextHandler
-
_servletContext
private javax.servlet.ServletContext _servletContext -
_filters
-
_filterMappings
-
_matchBeforeIndex
private int _matchBeforeIndex -
_matchAfterIndex
private int _matchAfterIndex -
_filterChainsCached
private boolean _filterChainsCached -
_maxFilterChainsCacheSize
private int _maxFilterChainsCacheSize -
_ensureDefaultServlet
private boolean _ensureDefaultServlet -
_identityService
-
_allowDuplicateMappings
private boolean _allowDuplicateMappings -
_servlets
-
_servletMappings
-
_filterNameMap
-
_filterPathMappings
-
_filterNameMappings
-
_wildFilterNameMappings
-
_servletNameMap
-
_servletPathMap
-
_listeners
-
_initialized
private boolean _initialized -
_chainCache
-
-
Constructor Details
-
ServletHandler
public ServletHandler()Constructor.
-
-
Method Details
-
isDumpable
-
dump
Description copied from interface:Dumpable
Dump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.- Specified by:
dump
in interfaceDumpable
- Overrides:
dump
in classContainerLifeCycle
- Parameters:
out
- The appendable to dump toindent
- The indent to apply after any new lines.- Throws:
IOException
- if unable to write to Appendable
-
doStart
Description copied from class:ContainerLifeCycle
Starts the managed lifecycle beans in the order they were added.- Overrides:
doStart
in classScopedHandler
- Throws:
Exception
- See Also:
-
isEnsureDefaultServlet
public boolean isEnsureDefaultServlet()- Returns:
- true if ServletHandler always has a default servlet, using
ServletHandler.Default404Servlet
if no other default servlet is configured.
-
setEnsureDefaultServlet
public void setEnsureDefaultServlet(boolean ensureDefaultServlet) - Parameters:
ensureDefaultServlet
- true if ServletHandler always has a default servlet, usingServletHandler.Default404Servlet
if no other default servlet is configured.
-
start
Description copied from class:ContainerLifeCycle
Starts the given lifecycle.- Overrides:
start
in classContainerLifeCycle
- Parameters:
l
- the lifecycle to start- Throws:
Exception
- if unable to start lifecycle
-
stop
Description copied from class:ContainerLifeCycle
Stops the given lifecycle.- Overrides:
stop
in classContainerLifeCycle
- Parameters:
l
- the lifecycle to stop- Throws:
Exception
- if unable to stop the lifecycle
-
doStop
Description copied from class:ContainerLifeCycle
Stops the managed lifecycle beans in the reverse order they were added.- Overrides:
doStop
in classAbstractHandler
- Throws:
Exception
-
getIdentityService
-
getFilterMappings
-
getFilters
-
getHolderEntry
Deprecated.ServletHolder matching path.- Parameters:
target
- Path within _context or servlet name- Returns:
- PathMap Entries pathspec to ServletHolder
-
getServletContext
public javax.servlet.ServletContext getServletContext() -
getServletContextHandler
-
getServletMappings
@ManagedAttribute(value="mappings of servlets", readonly=true) public ServletMapping[] getServletMappings() -
getServletMapping
Get the ServletMapping matching the path- Parameters:
pathSpec
- the path spec- Returns:
- the servlet mapping for the path spec (or null if not found)
-
getServlets
-
getServlets
-
getServlet
-
doScope
public void doScope(String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException Description copied from class:ScopedHandler
Scope the handlerDerived implementations should call
ScopedHandler.nextScope(String, Request, HttpServletRequest, HttpServletResponse)
- Overrides:
doScope
in classScopedHandler
- Parameters:
target
- The target of the request - either a URI or a name.baseRequest
- The original unwrapped request object.request
- The request either as theRequest
object or a wrapper of that request. The
method can be used access the Request object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getRequest()
response
- The response as theResponse
object or a wrapper of that request. The
method can be used access the Response object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getResponse()
- Throws:
IOException
- if unable to handle the request or response processingjavax.servlet.ServletException
- if unable to handle the request or response due to underlying servlet issue
-
doHandle
public void doHandle(String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException Description copied from class:ScopedHandler
Do the handler work within the scope.Derived implementations should call
ScopedHandler.nextHandle(String, Request, HttpServletRequest, HttpServletResponse)
- Specified by:
doHandle
in classScopedHandler
- Parameters:
target
- The target of the request - either a URI or a name.baseRequest
- The original unwrapped request object.request
- The request either as theRequest
object or a wrapper of that request. The
method can be used access the Request object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getRequest()
response
- The response as theResponse
object or a wrapper of that request. The
method can be used access the Response object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getResponse()
- Throws:
IOException
- if unable to handle the request or response processingjavax.servlet.ServletException
- if unable to handle the request or response due to underlying servlet issue
-
getMappedServlet
ServletHolder matching path.- Parameters:
target
- Path within _context or servlet name- Returns:
- MappedResource to the ServletHolder. Named servlets have a null PathSpec
-
getFilterChain
protected javax.servlet.FilterChain getFilterChain(Request baseRequest, String pathInContext, ServletHolder servletHolder) -
newFilterChain
protected javax.servlet.FilterChain newFilterChain(FilterHolder filterHolder, javax.servlet.FilterChain chain) Create a FilterChain that calls the passed filter with the passed chain- Parameters:
filterHolder
- The filter to invokechain
- The chain to pass to the filter- Returns:
- A FilterChain that invokes the filter with the chain
-
invalidateChainsCache
protected void invalidateChainsCache() -
isAvailable
public boolean isAvailable()- Returns:
- true if the handler is started and there are no unavailable servlets
-
isAllowDuplicateMappings
public boolean isAllowDuplicateMappings()- Returns:
- the allowDuplicateMappings
-
setAllowDuplicateMappings
public void setAllowDuplicateMappings(boolean allowDuplicateMappings) - Parameters:
allowDuplicateMappings
- the allowDuplicateMappings to set
-
initialize
Initialize filters and load-on-startup servlets.- Throws:
Exception
- if unable to initialize
-
isInitialized
public boolean isInitialized()- Returns:
- true if initialized has been called, false otherwise
-
initializeHolders
-
isFilterChainsCached
public boolean isFilterChainsCached()- Returns:
- whether the filter chains are cached.
-
addListener
Add a holder for a listener- Parameters:
listener
- the listener for the holder
-
getListeners
-
setListeners
-
newListenerHolder
-
newServletHolder
Add a new servlet holder- Parameters:
source
- the holder source- Returns:
- the servlet holder
-
addServletWithMapping
Convenience method to add a servlet.- Parameters:
className
- the class namepathSpec
- the path spec- Returns:
- The servlet holder.
-
addServletWithMapping
public ServletHolder addServletWithMapping(Class<? extends javax.servlet.Servlet> servlet, String pathSpec) Convenience method to add a servlet.- Parameters:
servlet
- the servlet classpathSpec
- the path spec- Returns:
- The servlet holder.
-
addServletWithMapping
Convenience method to add a servlet.- Parameters:
servlet
- servlet holder to addpathSpec
- servlet mappings for the servletHolder
-
addServlet
Convenience method to add a pre-constructed ServletHolder.- Parameters:
holder
- the servlet holder
-
addServletMapping
Convenience method to add a pre-constructed ServletMapping.- Parameters:
mapping
- the servlet mapping
-
setServletSecurity
-
newFilterHolder
-
getFilter
-
addFilterWithMapping
public FilterHolder addFilterWithMapping(Class<? extends javax.servlet.Filter> filter, String pathSpec, EnumSet<javax.servlet.DispatcherType> dispatches) Convenience method to add a filter.- Parameters:
filter
- class of filter to createpathSpec
- filter mappings for filterdispatches
- seeFilterMapping.setDispatches(int)
- Returns:
- The filter holder.
-
addFilterWithMapping
public FilterHolder addFilterWithMapping(String className, String pathSpec, EnumSet<javax.servlet.DispatcherType> dispatches) Convenience method to add a filter.- Parameters:
className
- of filterpathSpec
- filter mappings for filterdispatches
- seeFilterMapping.setDispatches(int)
- Returns:
- The filter holder.
-
addFilterWithMapping
public void addFilterWithMapping(FilterHolder holder, String pathSpec, EnumSet<javax.servlet.DispatcherType> dispatches) Convenience method to add a filter.- Parameters:
holder
- filter holder to addpathSpec
- filter mappings for filterdispatches
- seeFilterMapping.setDispatches(int)
-
addFilterWithMapping
public FilterHolder addFilterWithMapping(Class<? extends javax.servlet.Filter> filter, String pathSpec, int dispatches) Convenience method to add a filter.- Parameters:
filter
- class of filter to createpathSpec
- filter mappings for filterdispatches
- seeFilterMapping.setDispatches(int)
- Returns:
- The filter holder.
-
addFilterWithMapping
Convenience method to add a filter.- Parameters:
className
- of filterpathSpec
- filter mappings for filterdispatches
- seeFilterMapping.setDispatches(int)
- Returns:
- The filter holder.
-
addFilterWithMapping
Convenience method to add a filter.- Parameters:
holder
- filter holder to addpathSpec
- filter mappings for filterdispatches
- seeFilterMapping.setDispatches(int)
-
addFilter
@Deprecated public FilterHolder addFilter(String className, String pathSpec, EnumSet<javax.servlet.DispatcherType> dispatches) Deprecated.useaddFilterWithMapping(Class, String, EnumSet)
insteadConvenience method to add a filter with a mapping- Parameters:
className
- the filter class namepathSpec
- the path specdispatches
- the dispatcher types for this filter- Returns:
- the filter holder created
-
addFilter
Convenience method to add a filter and mapping- Parameters:
filter
- the filter holderfilterMapping
- the filter mapping
-
addFilter
Convenience method to add a preconstructed FilterHolder- Parameters:
filter
- the filter holder
-
addFilterMapping
Convenience method to add a preconstructed FilterMapping- Parameters:
mapping
- the filter mapping
-
prependFilterMapping
Convenience method to add a preconstructed FilterMapping- Parameters:
mapping
- the filter mapping
-
insertFilterMapping
Insert a filtermapping in the list- Parameters:
mapping
- the FilterMapping to addpos
- the position in the existing arry at which to add itbefore
- if true, insert before pos, if false insert after it- Returns:
- the new FilterMappings post-insert
-
updateNameMappings
protected void updateNameMappings() -
updateMappings
protected void updateMappings() -
notFound
protected void notFound(Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException - Throws:
IOException
javax.servlet.ServletException
-
containsFilterHolder
-
containsServletHolder
-
setFilterChainsCached
public void setFilterChainsCached(boolean filterChainsCached) - Parameters:
filterChainsCached
- The filterChainsCached to set.
-
setFilterMappings
- Parameters:
filterMappings
- The filterMappings to set.
-
setFilters
-
setServletMappings
- Parameters:
servletMappings
- The servletMappings to set.
-
setServlets
Set Servlets.- Parameters:
holders
- Array of servlets to define
-
getMaxFilterChainsCacheSize
public int getMaxFilterChainsCacheSize()- Returns:
- The maximum entries in a filter chain cache.
-
setMaxFilterChainsCacheSize
public void setMaxFilterChainsCacheSize(int maxFilterChainsCacheSize) Set the maximum filter chain cache size. Filter chains are cached ifisFilterChainsCached()
is true. If the max cache size is greater than zero, then the cache is flushed whenever it grows to be this size.- Parameters:
maxFilterChainsCacheSize
- the maximum number of entries in a filter chain cache.
-
destroyServlet
void destroyServlet(javax.servlet.Servlet servlet) -
destroyFilter
void destroyFilter(javax.servlet.Filter filter) -
destroyListener
-
addFilterWithMapping(Class, String, EnumSet)
instead