org.mortbay.jetty.servlet

Class ServletHandler

public class ServletHandler extends Container implements HttpHandler

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 when a full web application is not required. Specifically filters and request wrapping are not supported.

If a SessionManager is not added to the handler before it is initialized, then a HashSessionManager with a standard java.util.Random generator is created.

Version: $Id: ServletHandler.java,v 1.133 2006/03/15 14:43:00 gregwilkins Exp $

Author: Greg Wilkins

See Also: WebApplicationHandler

Field Summary
protected Map_attributes
protected ServletHandler.Context_context
protected Log_contextLog
protected String_formErrorPage
protected String_formLoginPage
protected HttpContext_httpContext
protected ClassLoader_loader
protected Map_nameMap
protected PathMap_servletMap
protected SessionManager_sessionManager
static String__DEFAULT_SERVLET
static String__J_S_CONTEXT_TEMPDIR
static String__J_S_ERROR_EXCEPTION
static String__J_S_ERROR_EXCEPTION_TYPE
static String__J_S_ERROR_MESSAGE
static String__J_S_ERROR_REQUEST_URI
static String__J_S_ERROR_SERVLET_NAME
static String__J_S_ERROR_STATUS_CODE
Constructor Summary
ServletHandler()
Constructor.
Method Summary
ServletHolderaddServlet(String name, String pathSpec, String servletClass, String forcedPath)
Add a servlet.
ServletHolderaddServlet(String name, String pathSpec, String servletClass)
Add a servlet.
ServletHolderaddServlet(String pathSpec, String servletClass)
Add a servlet instance to this handler and map it to a pathspec.
voidaddServletHolder(ServletHolder holder)
Register an existing ServletHolder with this handler.
voiddestroy()
protected voiddispatch(String pathInContext, HttpServletRequest request, HttpServletResponse response, ServletHolder servletHolder, int type)
Dispatch to a servletHolder.
protected voiddoStart()
protected voiddoStop()
protected voidfinalize()
voidformAuthInit(String formLoginPage, String formErrorPage)
ClassLoadergetClassLoader()
protected ObjectgetContextAttribute(String name)
Get context attribute.
protected EnumerationgetContextAttributeNames()
Get context attribute names.
StringgetErrorPage(int status, ServletHttpRequest request)
EntrygetHolderEntry(String pathInContext)
ServletHolder matching path.
HttpContextgetHttpContext()
HttpSessiongetHttpSession(String id)
StringgetName()
RequestDispatchergetNamedDispatcher(String name)
Get Named dispatcher.
StringgetRealPath(String path)
RequestDispatchergetRequestDispatcher(String uriInContext)
URLgetResource(String uriInContext)
Get a Resource.
InputStreamgetResourceAsStream(String uriInContext)
SetgetResourcePaths(String uriInContext)
ServletContextgetServletContext()
ServletHoldergetServletHolder(String name)
PathMapgetServletMap()
ServletHolder[]getServlets()
Get Servlets.
SessionManagergetSessionManager()
voidhandle(String pathInContext, String pathParams, HttpRequest httpRequest, HttpResponse httpResponse)
Handle request.
protected voidhandleOptions(HttpServletRequest request, HttpServletResponse response)
protected voidhandleTrace(HttpServletRequest request, HttpServletResponse response)
voidhandleTrace(HttpRequest request, HttpResponse response)
voidinitialize(HttpContext context)
voidinitializeServlets()
Initialize load-on-startup servlets.
booleanisAutoInitializeServlets()
booleanisUsingCookies()
ServletHoldermapPathToServlet(String pathSpec, String servletName)
Map a servlet to a pathSpec
HttpSessionnewHttpSession(HttpServletRequest request)
ServletHoldernewServletHolder(String name, String servletClass, String forcedPath)
ServletHoldernewServletHolder(String name, String servletClass)
protected voidnotFound(HttpServletRequest request, HttpServletResponse response)
protected voidremoveContextAttribute(String name)
voidsetAutoInitializeServlets(boolean b)
protected voidsetContextAttribute(String name, Object value)
voidsetDynamicInitParams(Map initParams)
Set dynamic servlet initial parameters.
voidsetDynamicServletPathSpec(String dynamicServletPathSpec)
Set the dynamic servlet path.
voidsetName(String name)
voidsetServeDynamicSystemServlets(boolean b)
Set serving dynamic system servlets.
voidsetSessionInactiveInterval(int seconds)
Set the session timeout interval in seconds.
voidsetSessionManager(SessionManager sm)
voidsetUsingCookies(boolean uc)

Field Detail

_attributes

protected Map _attributes

_context

protected transient ServletHandler.Context _context

_contextLog

protected transient Log _contextLog

_formErrorPage

protected String _formErrorPage

_formLoginPage

protected String _formLoginPage

_httpContext

protected transient HttpContext _httpContext

_loader

protected transient ClassLoader _loader

_nameMap

protected Map _nameMap

_servletMap

protected PathMap _servletMap

_sessionManager

protected SessionManager _sessionManager

__DEFAULT_SERVLET

public static final String __DEFAULT_SERVLET

__J_S_CONTEXT_TEMPDIR

public static final String __J_S_CONTEXT_TEMPDIR

__J_S_ERROR_EXCEPTION

public static final String __J_S_ERROR_EXCEPTION

__J_S_ERROR_EXCEPTION_TYPE

public static final String __J_S_ERROR_EXCEPTION_TYPE

__J_S_ERROR_MESSAGE

public static final String __J_S_ERROR_MESSAGE

__J_S_ERROR_REQUEST_URI

public static final String __J_S_ERROR_REQUEST_URI

__J_S_ERROR_SERVLET_NAME

public static final String __J_S_ERROR_SERVLET_NAME

__J_S_ERROR_STATUS_CODE

public static final String __J_S_ERROR_STATUS_CODE

Constructor Detail

ServletHandler

public ServletHandler()
Constructor.

Method Detail

addServlet

public ServletHolder addServlet(String name, String pathSpec, String servletClass, String forcedPath)
Add a servlet.

Parameters: name The servlet name. pathSpec A path specification to map this servlet to. servletClass The class name of the servlet. forcedPath If non null, the request attribute javax.servlet.include.servlet_path will be set to this path before service is called.

Returns: The ServletHolder for the servlet.

addServlet

public ServletHolder addServlet(String name, String pathSpec, String servletClass)
Add a servlet.

Parameters: name The servlet name. pathSpec A path specification to map this servlet to. servletClass The class name of the servlet.

Returns: The ServletHolder for the servlet.

addServlet

public ServletHolder addServlet(String pathSpec, String servletClass)
Add a servlet instance to this handler and map it to a pathspec.

Parameters: pathSpec The pathmapping servletClass The class of the servlet

Returns: The created ServletHolder

addServletHolder

public void addServletHolder(ServletHolder holder)
Register an existing ServletHolder with this handler.

Parameters: holder the ServletHolder to register.

destroy

public void destroy()

dispatch

protected void dispatch(String pathInContext, HttpServletRequest request, HttpServletResponse response, ServletHolder servletHolder, int type)
Dispatch to a servletHolder. This method may be specialized to insert extra handling in the dispatch of a request to a specific servlet. This is used by WebApplicatonHandler to implement dispatched filters. The default implementation simply calls ServletHolder.handle(request,response)

Parameters: pathInContext The path used to select the servlet holder. request response servletHolder type the type of dispatch as defined in the Dispatcher class.

Throws: ServletException UnavailableException IOException

doStart

protected void doStart()

doStop

protected void doStop()

finalize

protected void finalize()

formAuthInit

public void formAuthInit(String formLoginPage, String formErrorPage)

getClassLoader

public ClassLoader getClassLoader()

getContextAttribute

protected Object getContextAttribute(String name)
Get context attribute. Tries ServletHandler attributes and then delegated to HttpContext.

Parameters: name attribute name.

Returns: attribute

getContextAttributeNames

protected Enumeration getContextAttributeNames()
Get context attribute names. Combines ServletHandler and HttpContext attributes.

getErrorPage

public String getErrorPage(int status, ServletHttpRequest request)

getHolderEntry

public Entry getHolderEntry(String pathInContext)
ServletHolder matching path.

Parameters: pathInContext Path within context.

Returns: PathMap Entries pathspec to ServletHolder

getHttpContext

public HttpContext getHttpContext()

getHttpSession

public HttpSession getHttpSession(String id)

getName

public String getName()

getNamedDispatcher

public RequestDispatcher getNamedDispatcher(String name)
Get Named dispatcher.

Parameters: name The name of the servlet. If null or empty string, the containers default servlet is returned.

Returns: Request dispatcher for the named servlet.

getRealPath

public String getRealPath(String path)

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(String uriInContext)

getResource

public URL getResource(String uriInContext)
Get a Resource. If no resource is found, resource aliases are tried.

Parameters: uriInContext

Returns: URL of the resource.

Throws: MalformedURLException

getResourceAsStream

public InputStream getResourceAsStream(String uriInContext)

getResourcePaths

public Set getResourcePaths(String uriInContext)

getServletContext

public ServletContext getServletContext()

getServletHolder

public ServletHolder getServletHolder(String name)

getServletMap

public PathMap getServletMap()

getServlets

public ServletHolder[] getServlets()
Get Servlets.

Returns: Array of defined servlets

getSessionManager

public SessionManager getSessionManager()

handle

public void handle(String pathInContext, String pathParams, HttpRequest httpRequest, HttpResponse httpResponse)
Handle request.

Parameters: pathInContext pathParams httpRequest httpResponse

Throws: IOException

handleOptions

protected void handleOptions(HttpServletRequest request, HttpServletResponse response)

handleTrace

protected void handleTrace(HttpServletRequest request, HttpServletResponse response)

handleTrace

public void handleTrace(HttpRequest request, HttpResponse response)

initialize

public void initialize(HttpContext context)

initializeServlets

public void initializeServlets()
Initialize load-on-startup servlets. Called automatically from start if autoInitializeServlet is true.

isAutoInitializeServlets

public boolean isAutoInitializeServlets()

isUsingCookies

public boolean isUsingCookies()

mapPathToServlet

public ServletHolder mapPathToServlet(String pathSpec, String servletName)
Map a servlet to a pathSpec

Parameters: pathSpec The pathspec to map servletName The name of the servlet, which must have already been added.

Returns: The servlet holder of the mapped servlet.

newHttpSession

public HttpSession newHttpSession(HttpServletRequest request)

newServletHolder

public ServletHolder newServletHolder(String name, String servletClass, String forcedPath)

newServletHolder

public ServletHolder newServletHolder(String name, String servletClass)

notFound

protected void notFound(HttpServletRequest request, HttpServletResponse response)

removeContextAttribute

protected void removeContextAttribute(String name)

setAutoInitializeServlets

public void setAutoInitializeServlets(boolean b)

setContextAttribute

protected void setContextAttribute(String name, Object value)

setDynamicInitParams

public void setDynamicInitParams(Map initParams)

Deprecated: Use org.mortbay.jetty.servlet.Invoker

Set dynamic servlet initial parameters.

setDynamicServletPathSpec

public void setDynamicServletPathSpec(String dynamicServletPathSpec)

Deprecated: Use org.mortbay.jetty.servlet.Invoker

Set the dynamic servlet path.

setName

public void setName(String name)

setServeDynamicSystemServlets

public void setServeDynamicSystemServlets(boolean b)

Deprecated: Use org.mortbay.jetty.servlet.Invoker

Set serving dynamic system servlets.

setSessionInactiveInterval

public void setSessionInactiveInterval(int seconds)
Set the session timeout interval in seconds.

Parameters: seconds the length of the session timeout interval in seconds.

setSessionManager

public void setSessionManager(SessionManager sm)

setUsingCookies

public void setUsingCookies(boolean uc)

Parameters: uc If true, cookies are used for sessions

Copyright © 2004 Mortbay Consulting Pty. Ltd. All Rights Reserved.