org.mortbay.jetty.servlet
Class ServletHandler

java.lang.Object
  extended by org.mortbay.util.Container
      extended by org.mortbay.jetty.servlet.ServletHandler
All Implemented Interfaces:
java.io.Serializable, HttpHandler, EventProvider, LifeCycle
Direct Known 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.133 2006/03/15 14:43:00 gregwilkins Exp $
Author:
Greg Wilkins
See Also:
WebApplicationHandler, Serialized Form

Field Summary
static java.lang.String __DEFAULT_SERVLET
           
static java.lang.String __J_S_CONTEXT_TEMPDIR
           
static java.lang.String __J_S_ERROR_EXCEPTION
           
static java.lang.String __J_S_ERROR_EXCEPTION_TYPE
           
static java.lang.String __J_S_ERROR_MESSAGE
           
static java.lang.String __J_S_ERROR_REQUEST_URI
           
static java.lang.String __J_S_ERROR_SERVLET_NAME
           
static java.lang.String __J_S_ERROR_STATUS_CODE
           
protected  java.util.Map _attributes
           
protected  org.mortbay.jetty.servlet.ServletHandler.Context _context
           
protected  org.apache.commons.logging.Log _contextLog
           
protected  java.lang.String _formErrorPage
           
protected  java.lang.String _formLoginPage
           
protected  HttpContext _httpContext
           
protected  java.lang.ClassLoader _loader
           
protected  java.util.Map _nameMap
           
protected  PathMap _servletMap
           
protected  SessionManager _sessionManager
           
 
Constructor Summary
ServletHandler()
          Constructor.
 
Method Summary
 ServletHolder addServlet(java.lang.String pathSpec, java.lang.String servletClass)
          Add a servlet instance to this handler and map it to a pathspec.
 ServletHolder addServlet(java.lang.String name, java.lang.String pathSpec, java.lang.String servletClass)
          Add a servlet.
 ServletHolder addServlet(java.lang.String name, java.lang.String pathSpec, java.lang.String servletClass, java.lang.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(java.lang.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(java.lang.String formLoginPage, java.lang.String formErrorPage)
           
 java.lang.ClassLoader getClassLoader()
           
protected  java.lang.Object getContextAttribute(java.lang.String name)
          Get context attribute.
protected  java.util.Enumeration getContextAttributeNames()
          Get context attribute names.
 java.lang.String getErrorPage(int status, ServletHttpRequest request)
           
 java.util.Map.Entry getHolderEntry(java.lang.String pathInContext)
          ServletHolder matching path.
 HttpContext getHttpContext()
           
 HttpSession getHttpSession(java.lang.String id)
           
 java.lang.String getName()
          Get the name of the handler.
 RequestDispatcher getNamedDispatcher(java.lang.String name)
          Get Named dispatcher.
 java.lang.String getRealPath(java.lang.String path)
           
 RequestDispatcher getRequestDispatcher(java.lang.String uriInContext)
           
 java.net.URL getResource(java.lang.String uriInContext)
          Get a Resource.
 java.io.InputStream getResourceAsStream(java.lang.String uriInContext)
           
 java.util.Set getResourcePaths(java.lang.String uriInContext)
           
 ServletContext getServletContext()
           
 ServletHolder getServletHolder(java.lang.String name)
           
 PathMap getServletMap()
           
 ServletHolder[] getServlets()
          Get Servlets.
 SessionManager getSessionManager()
           
 void handle(java.lang.String pathInContext, java.lang.String pathParams, HttpRequest httpRequest, HttpResponse httpResponse)
          Handle request.
protected  void handleOptions(HttpServletRequest request, HttpServletResponse response)
           
 void handleTrace(HttpRequest request, HttpResponse response)
           
protected  void handleTrace(HttpServletRequest request, HttpServletResponse response)
           
 void initialize(HttpContext context)
           
 void initializeServlets()
          Initialize load-on-startup servlets.
 boolean isAutoInitializeServlets()
           
 boolean isUsingCookies()
           
 ServletHolder mapPathToServlet(java.lang.String pathSpec, java.lang.String servletName)
          Map a servlet to a pathSpec
 HttpSession newHttpSession(HttpServletRequest request)
           
 ServletHolder newServletHolder(java.lang.String name, java.lang.String servletClass)
           
 ServletHolder newServletHolder(java.lang.String name, java.lang.String servletClass, java.lang.String forcedPath)
           
protected  void notFound(HttpServletRequest request, HttpServletResponse response)
           
protected  void removeContextAttribute(java.lang.String name)
           
 void setAutoInitializeServlets(boolean b)
           
protected  void setContextAttribute(java.lang.String name, java.lang.Object value)
           
 void setDynamicInitParams(java.util.Map initParams)
          Deprecated. Use org.mortbay.jetty.servlet.Invoker
 void setDynamicServletPathSpec(java.lang.String dynamicServletPathSpec)
          Deprecated. Use org.mortbay.jetty.servlet.Invoker
 void setName(java.lang.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, getComponents, isFailed, isStarted, isStarting, isStopping, removeComponent, removeEventListener, start, stop
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.mortbay.util.LifeCycle
isStarted, start, stop
 

Field Detail

__DEFAULT_SERVLET

public static final java.lang.String __DEFAULT_SERVLET
See Also:
Constant Field Values

__J_S_CONTEXT_TEMPDIR

public static final java.lang.String __J_S_CONTEXT_TEMPDIR
See Also:
Constant Field Values

__J_S_ERROR_EXCEPTION

public static final java.lang.String __J_S_ERROR_EXCEPTION
See Also:
Constant Field Values

__J_S_ERROR_EXCEPTION_TYPE

public static final java.lang.String __J_S_ERROR_EXCEPTION_TYPE
See Also:
Constant Field Values

__J_S_ERROR_MESSAGE

public static final java.lang.String __J_S_ERROR_MESSAGE
See Also:
Constant Field Values

__J_S_ERROR_REQUEST_URI

public static final java.lang.String __J_S_ERROR_REQUEST_URI
See Also:
Constant Field Values

__J_S_ERROR_SERVLET_NAME

public static final java.lang.String __J_S_ERROR_SERVLET_NAME
See Also:
Constant Field Values

__J_S_ERROR_STATUS_CODE

public static final java.lang.String __J_S_ERROR_STATUS_CODE
See Also:
Constant Field Values

_servletMap

protected PathMap _servletMap

_nameMap

protected java.util.Map _nameMap

_attributes

protected java.util.Map _attributes

_formLoginPage

protected java.lang.String _formLoginPage

_formErrorPage

protected java.lang.String _formErrorPage

_sessionManager

protected SessionManager _sessionManager

_context

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

_loader

protected transient java.lang.ClassLoader _loader

_contextLog

protected transient org.apache.commons.logging.Log _contextLog

_httpContext

protected transient HttpContext _httpContext
Constructor Detail

ServletHandler

public ServletHandler()
Constructor.

Method Detail

setName

public void setName(java.lang.String name)

getName

public java.lang.String getName()
Description copied from interface: HttpHandler
Get the name of the handler.

Specified by:
getName in interface HttpHandler
Returns:
The name of the handler used for logging and reporting.

getHttpContext

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

initialize

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

formAuthInit

public void formAuthInit(java.lang.String formLoginPage,
                         java.lang.String formErrorPage)

setSessionManager

public void setSessionManager(SessionManager sm)

getSessionManager

public SessionManager getSessionManager()

getServletContext

public ServletContext getServletContext()

getServletMap

public PathMap getServletMap()

isUsingCookies

public boolean isUsingCookies()

setDynamicServletPathSpec

public void setDynamicServletPathSpec(java.lang.String dynamicServletPathSpec)
Deprecated. Use org.mortbay.jetty.servlet.Invoker

Set the dynamic servlet path.


setDynamicInitParams

public void setDynamicInitParams(java.util.Map initParams)
Deprecated. Use org.mortbay.jetty.servlet.Invoker

Set dynamic servlet initial parameters.


setServeDynamicSystemServlets

public void setServeDynamicSystemServlets(boolean b)
Deprecated. Use org.mortbay.jetty.servlet.Invoker

Set serving dynamic system servlets.


getClassLoader

public java.lang.ClassLoader getClassLoader()

setUsingCookies

public void setUsingCookies(boolean uc)
Parameters:
uc - If true, cookies are used for sessions

newServletHolder

public ServletHolder newServletHolder(java.lang.String name,
                                      java.lang.String servletClass,
                                      java.lang.String forcedPath)

newServletHolder

public ServletHolder newServletHolder(java.lang.String name,
                                      java.lang.String servletClass)

getServletHolder

public ServletHolder getServletHolder(java.lang.String name)

mapPathToServlet

public ServletHolder mapPathToServlet(java.lang.String pathSpec,
                                      java.lang.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.

addServlet

public ServletHolder addServlet(java.lang.String name,
                                java.lang.String pathSpec,
                                java.lang.String servletClass,
                                java.lang.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(java.lang.String name,
                                java.lang.String pathSpec,
                                java.lang.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(java.lang.String pathSpec,
                                java.lang.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.

isAutoInitializeServlets

public boolean isAutoInitializeServlets()

setAutoInitializeServlets

public void setAutoInitializeServlets(boolean b)

doStart

protected void doStart()
                throws java.lang.Exception
Description copied from class: Container
Do start operations. This abstract method is called by start to perform the actual start operations.

Specified by:
doStart in class Container
Throws:
java.lang.Exception

getServlets

public ServletHolder[] getServlets()
Get Servlets.

Returns:
Array of defined servlets

initializeServlets

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

Throws:
java.lang.Exception

doStop

protected void doStop()
               throws java.lang.Exception
Description copied from class: Container
Do stop operations. This abstract method is called by stop to perform the actual stop operations.

Specified by:
doStop in class Container
Throws:
java.lang.Exception

getHttpSession

public HttpSession getHttpSession(java.lang.String id)

newHttpSession

public HttpSession newHttpSession(HttpServletRequest request)

setSessionInactiveInterval

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

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

handle

public void handle(java.lang.String pathInContext,
                   java.lang.String pathParams,
                   HttpRequest httpRequest,
                   HttpResponse httpResponse)
            throws java.io.IOException
Handle request.

Specified by:
handle in interface HttpHandler
Parameters:
pathInContext -
pathParams -
httpRequest -
httpResponse -
Throws:
java.io.IOException

dispatch

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

getHolderEntry

public java.util.Map.Entry getHolderEntry(java.lang.String pathInContext)
ServletHolder matching path.

Parameters:
pathInContext - Path within context.
Returns:
PathMap Entries pathspec to ServletHolder

getResourcePaths

public java.util.Set getResourcePaths(java.lang.String uriInContext)

getResource

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

Parameters:
uriInContext -
Returns:
URL of the resource.
Throws:
java.net.MalformedURLException

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String uriInContext)

getRealPath

public java.lang.String getRealPath(java.lang.String path)

getRequestDispatcher

public RequestDispatcher getRequestDispatcher(java.lang.String uriInContext)

getNamedDispatcher

public RequestDispatcher getNamedDispatcher(java.lang.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.

notFound

protected void notFound(HttpServletRequest request,
                        HttpServletResponse response)
                 throws java.io.IOException
Throws:
java.io.IOException

handleTrace

protected void handleTrace(HttpServletRequest request,
                           HttpServletResponse response)
                    throws java.io.IOException
Throws:
java.io.IOException

handleOptions

protected void handleOptions(HttpServletRequest request,
                             HttpServletResponse response)
                      throws java.io.IOException
Throws:
java.io.IOException

getErrorPage

public java.lang.String getErrorPage(int status,
                                     ServletHttpRequest request)

getContextAttribute

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

Parameters:
name - attribute name.
Returns:
attribute

getContextAttributeNames

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


setContextAttribute

protected void setContextAttribute(java.lang.String name,
                                   java.lang.Object value)

removeContextAttribute

protected void removeContextAttribute(java.lang.String name)

handleTrace

public void handleTrace(HttpRequest request,
                        HttpResponse response)
                 throws java.io.IOException
Throws:
java.io.IOException

destroy

public void destroy()
Description copied from class: Container
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 class Container

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable


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