org.mortbay.jetty.servlet

Class ServletHandler

Implemented Interfaces:
EventProvider, HttpHandler, LifeCycle, Serializable
Known Direct Subclasses:
WebApplicationHandler

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.132 2005/12/04 06:26:04 janb Exp $
Author:
Greg Wilkins
See Also:
WebApplicationHandler

Field Summary

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
protected Map
_attributes
protected org.mortbay.jetty.servlet.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

Constructor Summary

ServletHandler()
Constructor.

Method Summary

ServletHolder
addServlet(String pathSpec, String servletClass)
Add a servlet instance to this handler and map it to a pathspec.
ServletHolder
addServlet(String name, String pathSpec, String servletClass)
Add a servlet.
ServletHolder
addServlet(String name, String pathSpec, String servletClass, String forcedPath)
Add a servlet.
void
addServletHolder(ServletHolder holder)
Register an existing ServletHolder with this handler.
void
destroy()
Destroy a stopped server.
protected void
dispatch(String pathInContext, HttpServletRequest request, HttpServletResponse response, ServletHolder servletHolder, int type)
Dispatch to a servletHolder.
protected void
doStart()
Do start operations.
protected void
doStop()
Do stop operations.
protected void
finalize()
void
formAuthInit(String formLoginPage, String formErrorPage)
ClassLoader
getClassLoader()
protected Object
getContextAttribute(String name)
Get context attribute.
protected Enumeration
getContextAttributeNames()
Get context attribute names.
String
getErrorPage(int status, ServletHttpRequest request)
Map.Entry
getHolderEntry(String pathInContext)
ServletHolder matching path.
HttpContext
getHttpContext()
HttpSession
getHttpSession(String id)
String
getName()
Get the name of the handler.
RequestDispatcher
getNamedDispatcher(String name)
Get Named dispatcher.
String
getRealPath(String path)
RequestDispatcher
getRequestDispatcher(String uriInContext)
URL
getResource(String uriInContext)
Get a Resource.
InputStream
getResourceAsStream(String uriInContext)
Set
getResourcePaths(String uriInContext)
ServletContext
getServletContext()
ServletHolder
getServletHolder(String name)
PathMap
getServletMap()
ServletHolder[]
getServlets()
Get Servlets.
SessionManager
getSessionManager()
void
handle(String pathInContext, String pathParams, HttpRequest httpRequest, HttpResponse httpResponse)
Handle request.
protected void
handleOptions(HttpServletRequest request, HttpServletResponse response)
protected void
handleTrace(HttpServletRequest request, HttpServletResponse response)
void
handleTrace(HttpRequest request, HttpResponse response)
void
initialize(HttpContext context)
void
initializeServlets()
Initialize load-on-startup servlets.
boolean
isAutoInitializeServlets()
boolean
isUsingCookies()
ServletHolder
mapPathToServlet(String pathSpec, String servletName)
Map a servlet to a pathSpec
HttpSession
newHttpSession(HttpServletRequest request)
ServletHolder
newServletHolder(String name, String servletClass)
ServletHolder
newServletHolder(String name, String servletClass, String forcedPath)
protected void
notFound(HttpServletRequest request, HttpServletResponse response)
protected void
removeContextAttribute(String name)
void
setAutoInitializeServlets(boolean b)
protected void
setContextAttribute(String name, Object value)
void
setDynamicInitParams(Map initParams)
Deprecated. Use org.mortbay.jetty.servlet.Invoker
void
setDynamicServletPathSpec(String dynamicServletPathSpec)
Deprecated. Use org.mortbay.jetty.servlet.Invoker
void
setName(String name)
void
setServeDynamicSystemServlets(boolean b)
Deprecated. Use org.mortbay.jetty.servlet.Invoker
void
setSessionInactiveInterval(int seconds)
Set the session timeout interval in seconds.
void
setSessionManager(SessionManager sm)
void
setUsingCookies(boolean uc)

Methods inherited from class org.mortbay.util.Container

addComponent, addEventListener, destroy, doStart, doStop, getComponents, isStarted, isStarting, isStopping, removeComponent, removeEventListener, start, stop

Field Details

__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

_attributes

protected Map _attributes

_context

protected org.mortbay.jetty.servlet.ServletHandler.Context _context

_contextLog

protected Log _contextLog

_formErrorPage

protected String _formErrorPage

_formLoginPage

protected String _formLoginPage

_httpContext

protected HttpContext _httpContext

_loader

protected ClassLoader _loader

_nameMap

protected Map _nameMap

_servletMap

protected PathMap _servletMap

_sessionManager

protected SessionManager _sessionManager

Constructor Details

ServletHandler

public ServletHandler()
Constructor.

Method Details

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

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

addServletHolder

public void addServletHolder(ServletHolder holder)
Register an existing ServletHolder with this handler.
Parameters:
holder - the ServletHolder to register.

destroy

public void destroy()
Destroy a stopped server. Remove all components and send notifications to all event listeners. The HttpServer must be stopped before it can be destroyed.
Overrides:
destroy in interface Container

dispatch

protected void dispatch(String pathInContext,
                        HttpServletRequest request,
                        HttpServletResponse response,
                        ServletHolder servletHolder,
                        int type)
            throws ServletException,
                   UnavailableException,
                   IOException
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.

doStart

protected void doStart()
            throws Exception
Do start operations. This abstract method is called by start to perform the actual start operations.
Overrides:
doStart in interface Container

doStop

protected void doStop()
            throws Exception
Do stop operations. This abstract method is called by stop to perform the actual stop operations.
Overrides:
doStop in interface Container

finalize

protected void finalize()
            throws Throwable

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 Map.Entry getHolderEntry(String pathInContext)
ServletHolder matching path.
Parameters:
pathInContext - Path within context.
Returns:
PathMap Entries pathspec to ServletHolder

getHttpContext

public HttpContext getHttpContext()
Specified by:
getHttpContext in interface HttpHandler

getHttpSession

public HttpSession getHttpSession(String id)

getName

public String getName()
Get the name of the handler.
Specified by:
getName in interface HttpHandler
Returns:
The name of the handler used for logging and reporting.

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)
            throws MalformedURLException
Get a Resource. If no resource is found, resource aliases are tried.
Parameters:
uriInContext -
Returns:
URL of the resource.

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)
            throws IOException
Handle request.
Specified by:
handle in interface HttpHandler
Parameters:
pathInContext -
pathParams -
httpRequest -
httpResponse -

handleOptions

protected void handleOptions(HttpServletRequest request,
                             HttpServletResponse response)
            throws IOException

handleTrace

protected void handleTrace(HttpServletRequest request,
                           HttpServletResponse response)
            throws IOException

handleTrace

public void handleTrace(HttpRequest request,
                        HttpResponse response)
            throws IOException

initialize

public void initialize(HttpContext context)
Specified by:
initialize in interface HttpHandler

initializeServlets

public void initializeServlets()
            throws Exception
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)

newServletHolder

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

notFound

protected void notFound(HttpServletRequest request,
                        HttpServletResponse response)
            throws IOException

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.