Top Description Inners Fields Constructors Methods
org.mortbay.jetty.servlet

public Class DefaultServlet

extends HttpServlet
implements ResourceFactory
Class Inheritance
All Implemented Interfaces
org.mortbay.resource.ResourceFactory
Imports
java.io.File, .IOException, .InputStream, .OutputStream, java.net.MalformedURLException, java.util.Enumeration, .List, .Map, .Map.Entry, javax.servlet.RequestDispatcher, .ServletContext, .ServletException, .UnavailableException, javax.servlet.http.HttpServlet, .HttpServletRequest, .HttpServletResponse, org.mortbay.io.Buffer, .ByteArrayBuffer, .WriterOutputStream, org.mortbay.io.nio.DirectNIOBuffer, .IndirectNIOBuffer, .NIOBuffer, org.mortbay.jetty.Connector, .HttpConnection, .HttpContent, .HttpFields, .HttpHeaderValues, .HttpHeaders, .HttpMethods, .InclusiveByteRange, .MimeTypes, .ResourceCache, .Response, org.mortbay.jetty.handler.ContextHandler, org.mortbay.jetty.nio.NIOConnector, org.mortbay.log.Log, org.mortbay.resource.FileResource, .Resource, .ResourceFactory, org.mortbay.util.IO, .MultiPartOutputStream, .TypeUtil, .URIUtil

The default servlet. This servlet, normally mapped to /, provides the handling for static content, OPTION and TRACE methods for the context. The following initParameters are supported, these can be set either on the servlet itself or as ServletContext initParameters with a prefix of org.mortbay.jetty.servlet.Default. :
                                                                      
  acceptRanges     If true, range requests and responses are         
                   supported                                         

  dirAllowed       If true, directory listings are returned if no    
                   welcome file is found. Else 403 Forbidden.        

  welcomeServlets  If true, attempt to dispatch to welcome files 
                   that are servlets, but only after no matching static 
                   resources could be found. 

                   This must be false if you want directory listings,
                   but have index.jsp in your welcome file list.

  redirectWelcome  If true, welcome files are redirected rather than
                   forwarded to.

  gzip             If set to true, then static content will be served as 
                   gzip content encoded if a matching resource is 
                   found ending with ".gz"

 resourceBase      Set to replace the context resource base

 relativeResourceBase    
                   Set with a pathname relative to the base of the
                   servlet context root. Useful for only serving static content out
                   of only specific subdirectories.

 aliases           If True, aliases of resources are allowed (eg. symbolic
                   links and caps variations). May bypass security constraints.

 maxCacheSize      The maximum total size of the cache or 0 for no cache.
 maxCachedFileSize The maximum size of a file to cache
 maxCachedFiles    The maximum number of files to cache
 cacheType         Set to "bio", "nio" or "both" to determine the type resource cache. 
                   A bio cached buffer may be used by nio but is not as efficient as an
                   nio buffer.  An nio cached buffer may not be used by bio.    

 useFileMappedBuffer 
                   If set to true, it will use mapped file buffer to serve static content
                   when using NIO connector. Setting this value to false means that
                   a direct buffer will be used instead of a mapped file buffer. 
                   By default, this is set to true.

 cacheControl      If set, all static content will have this value set as the cache-control
                   header.


Authors
Greg Wilkins (gregw), Nigel Canonizado

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
private class

Field Summary

Modifier and TypeField and Description
private boolean
private boolean
private ResourceCache
pack-priv ByteArrayBuffer
private ContextHandler.SContext
private ServletHolder
private boolean
private boolean
private MimeTypes
private DefaultServlet.NIOResourceCache
private boolean
private Resource
private ServletHandler
private boolean
private String[]
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
destroy()

Overrides javax.servlet.GenericServlet.destroy.

Implements javax.servlet.Servlet.destroy.

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
protected void
doGet(HttpServletRequest
an HttpServletRequest object that contains the request the client has made of the servlet
request
,
HttpServletResponse
an HttpServletResponse object that contains the response the servlet sends to the client
response
)

Overrides javax.servlet.http.HttpServlet.doGet.

Called by the server (via the service method) to allow a servlet to handle a GET request.
protected void
doPost(HttpServletRequest
an HttpServletRequest object that contains the request the client has made of the servlet
request
,
HttpServletResponse
an HttpServletResponse object that contains the response the servlet sends to the client
response
)

Overrides javax.servlet.http.HttpServlet.doPost.

Called by the server (via the service method) to allow a servlet to handle a POST request.
protected void
doTrace(HttpServletRequest
the HttpServletRequest object that contains the request the client made of the servlet
req
,
HttpServletResponse
the HttpServletResponse object that contains the response the servlet returns to the client
resp
)

Overrides javax.servlet.http.HttpServlet.doTrace.

Called by the server (via the service method) to allow a servlet to handle a TRACE request.
private boolean
getInitBoolean(String name, boolean dft)

private int
getInitInt(String name, int dft)

public String
getInitParameter(String
a String specifying the name of the initialization parameter
name
)

Overrides javax.servlet.GenericServlet.getInitParameter.

Implements javax.servlet.ServletConfig.getInitParameter.

Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
public Resource

Returns:

The resource to serve.
getResource
(String
The path to find a resource for.
pathInContext
)

Implements org.mortbay.resource.ResourceFactory.getResource.

get Resource to serve.
private String

Returns:

The path of the matching welcome file in context
getWelcomeFile
(String
The path in Context in which we are looking for a welcome
pathInContext
)

Finds a matching welcome file for the supplied Resource.

public void
init()

Overrides javax.servlet.GenericServlet.init.

A convenience method which can be overridden so that there's no need to call super.init(config).
protected boolean
protected void
sendData(HttpServletRequest request, HttpServletResponse response, boolean include, Resource resource, HttpContent content, Enumeration<E> reqRanges)

protected void
sendDirectory(HttpServletRequest request, HttpServletResponse response, Resource resource, boolean parent)

protected void
writeHeaders(HttpServletResponse response, HttpContent content, long count)

protected void
protected void
Inherited from javax.servlet.http.HttpServlet:
doDeletedoHeaddoOptionsdoPutgetLastModifiedserviceservice

Field Detail

_acceptRangesback to summary
private boolean _acceptRanges
_aliasesback to summary
private boolean _aliases
_bioCacheback to summary
private ResourceCache _bioCache
_cacheControlback to summary
pack-priv ByteArrayBuffer _cacheControl
_contextback to summary
private ContextHandler.SContext _context
_defaultHolderback to summary
private ServletHolder _defaultHolder
_dirAllowedback to summary
private boolean _dirAllowed
_gzipback to summary
private boolean _gzip
_mimeTypesback to summary
private MimeTypes _mimeTypes
_nioCacheback to summary
private DefaultServlet.NIOResourceCache _nioCache
_redirectWelcomeback to summary
private boolean _redirectWelcome
_resourceBaseback to summary
private Resource _resourceBase
_servletHandlerback to summary
private ServletHandler _servletHandler
_useFileMappedBufferback to summary
private boolean _useFileMappedBuffer
_welcomesback to summary
private String[] _welcomes
_welcomeServletsback to summary
private boolean _welcomeServlets

Constructor Detail

DefaultServletback to summary
public DefaultServlet()

Method Detail

destroyback to summary
public void destroy()

Overrides javax.servlet.GenericServlet.destroy.

Implements javax.servlet.Servlet.destroy.

Doc from javax.servlet.GenericServlet.destroy.

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. See Servlet#destroy.

doGetback to summary
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

Overrides javax.servlet.http.HttpServlet.doGet.

Doc from javax.servlet.http.HttpServlet.doGet.

Called by the server (via the service method) to allow a servlet to handle a GET request.

Overriding this method to support a GET request also automatically supports an HTTP HEAD request. A HEAD request is a GET request that returns no body in the response, only the request header fields.

When overriding this method, read the request data, write the response headers, get the response's writer or output stream object, and finally, write the response data. It's best to include content type and encoding. When using a PrintWriter object to return the response, set the content type before accessing the PrintWriter object.

The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body.

Where possible, set the Content-Length header (with the javax.servlet.ServletResponse#setContentLength method), to allow the servlet container to use a persistent connection to return its response to the client, improving performance. The content length is automatically set if the entire response fits inside the response buffer.

When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header.

The GET method should be safe, that is, without any side effects for which users are held responsible. For example, most form queries have no side effects. If a client request is intended to change stored data, the request should use some other HTTP method.

The GET method should also be idempotent, meaning that it can be safely repeated. Sometimes making a method safe also makes it idempotent. For example, repeating queries is both safe and idempotent, but buying a product online or modifying data is neither safe nor idempotent.

If the request is incorrectly formatted, doGet returns an HTTP "Bad Request" message.

Parameters
request:HttpServletRequest

an HttpServletRequest object that contains the request the client has made of the servlet

response:HttpServletResponse

an HttpServletResponse object that contains the response the servlet sends to the client

Exceptions
ServletException:
if the request for the GET could not be handled
IOException:
if an input or output error is detected when the servlet handles the GET request
doPostback to summary
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

Overrides javax.servlet.http.HttpServlet.doPost.

Doc from javax.servlet.http.HttpServlet.doPost.

Called by the server (via the service method) to allow a servlet to handle a POST request. The HTTP POST method allows the client to send data of unlimited length to the Web server a single time and is useful when posting information such as credit card numbers.

When overriding this method, read the request data, write the response headers, get the response's writer or output stream object, and finally, write the response data. It's best to include content type and encoding. When using a PrintWriter object to return the response, set the content type before accessing the PrintWriter object.

The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body.

Where possible, set the Content-Length header (with the javax.servlet.ServletResponse#setContentLength method), to allow the servlet container to use a persistent connection to return its response to the client, improving performance. The content length is automatically set if the entire response fits inside the response buffer.

When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header.

This method does not need to be either safe or idempotent. Operations requested through POST can have side effects for which the user can be held accountable, for example, updating stored data or buying items online.

If the HTTP POST request is incorrectly formatted, doPost returns an HTTP "Bad Request" message.

Parameters
request:HttpServletRequest

an HttpServletRequest object that contains the request the client has made of the servlet

response:HttpServletResponse

an HttpServletResponse object that contains the response the servlet sends to the client

Exceptions
ServletException:
if the request for the POST could not be handled
IOException:
if an input or output error is detected when the servlet handles the request
doTraceback to summary
protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

Overrides javax.servlet.http.HttpServlet.doTrace.

Doc from javax.servlet.http.HttpServlet.doTrace.

Called by the server (via the service method) to allow a servlet to handle a TRACE request. A TRACE returns the headers sent with the TRACE request to the client, so that they can be used in debugging. There's no need to override this method.

Parameters
req:HttpServletRequest

the HttpServletRequest object that contains the request the client made of the servlet

resp:HttpServletResponse

the HttpServletResponse object that contains the response the servlet returns to the client

Exceptions
ServletException:
if the request for the TRACE cannot be handled
IOException:
if an input or output error occurs while the servlet is handling the TRACE request
getInitBooleanback to summary
private boolean getInitBoolean(String name, boolean dft)
getInitIntback to summary
private int getInitInt(String name, int dft)
getInitParameterback to summary
public String getInitParameter(String name)

Overrides javax.servlet.GenericServlet.getInitParameter.

Implements javax.servlet.ServletConfig.getInitParameter.

Doc from javax.servlet.GenericServlet.getInitParameter.

Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist. See ServletConfig#getInitParameter.

This method is supplied for convenience. It gets the value of the named parameter from the servlet's ServletConfig object.

Parameters
name:String

a String specifying the name of the initialization parameter

Returns:String

String a String containing the value of the initialization parameter

getResourceback to summary
public Resource getResource(String pathInContext)

Implements org.mortbay.resource.ResourceFactory.getResource.

get Resource to serve. Map a path to a resource. The default implementation calls HttpContext.getResource but derived servlets may provide their own mapping.

Parameters
pathInContext:String

The path to find a resource for.

Returns:Resource

The resource to serve.

getWelcomeFileback to summary
private String getWelcomeFile(String pathInContext) throws MalformedURLException, IOException

Finds a matching welcome file for the supplied Resource. This will be the first entry in the list of configured welcome files that existing within the directory referenced by the Resource. If the resource is not a directory, or no matching file is found, then it may look for a valid servlet mapping. If there is none, then null is returned. The list of welcome files is read from the ContextHandler for this servlet, or "index.jsp" , "index.html" if that is null.

Parameters
pathInContext:String

The path in Context in which we are looking for a welcome

Returns:String

The path of the matching welcome file in context

initback to summary
public void init() throws UnavailableException

Overrides javax.servlet.GenericServlet.init.

Doc from javax.servlet.GenericServlet.init.

A convenience method which can be overridden so that there's no need to call super.init(config).

Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig.

passConditionalHeadersback to summary
protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, Resource resource, HttpContent content) throws IOException
sendDataback to summary
protected void sendData(HttpServletRequest request, HttpServletResponse response, boolean include, Resource resource, HttpContent content, Enumeration<E> reqRanges) throws IOException
sendDirectoryback to summary
protected void sendDirectory(HttpServletRequest request, HttpServletResponse response, Resource resource, boolean parent) throws IOException
writeHeadersback to summary
protected void writeHeaders(HttpServletResponse response, HttpContent content, long count) throws IOException
writeOptionHeadersback to summary
protected void writeOptionHeaders(HttpFields fields) throws IOException
writeOptionHeadersback to summary
protected void writeOptionHeaders(HttpServletResponse response) throws IOException
org.mortbay.jetty.servlet back to summary

pack-priv Class DefaultServlet.NIOResourceCache

extends ResourceCache
Class Inheritance

Field Summary

Inherited from org.mortbay.jetty.ResourceCache:
_cache_cachedFiles_cachedSize_leastRecentlyUsed_mostRecentlyUsed

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
Inherited from org.mortbay.jetty.ResourceCache:
doStartdoStopflushCachegetCachedFilesgetCachedSizegetMaxCachedFilesgetMaxCachedFileSizegetMaxCacheSizelookuplookupsetMaxCachedFilessetMaxCachedFileSizesetMaxCacheSize

Constructor Detail

NIOResourceCacheback to summary
public NIOResourceCache(MimeTypes mimeTypes)

Method Detail

fillback to summary
protected void fill(ResourceCache.Content content) throws IOException

Overrides org.mortbay.jetty.ResourceCache.fill.

org.mortbay.jetty.servlet back to summary

private Class DefaultServlet.UnCachedContent

extends Object
implements HttpContent
Class Inheritance
All Implemented Interfaces
org.mortbay.jetty.HttpContent

Field Summary

Modifier and TypeField and Description
pack-priv Resource

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Buffer
public long
public Buffer
public InputStream
public Buffer
public Resource
public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_resourceback to summary
pack-priv Resource _resource

Constructor Detail

UnCachedContentback to summary
pack-priv UnCachedContent(Resource resource)

Method Detail

getBufferback to summary
public Buffer getBuffer()

Implements org.mortbay.jetty.HttpContent.getBuffer.

getContentLengthback to summary
public long getContentLength()

Implements org.mortbay.jetty.HttpContent.getContentLength.

getContentTypeback to summary
public Buffer getContentType()

Implements org.mortbay.jetty.HttpContent.getContentType.

getInputStreamback to summary
public InputStream getInputStream() throws IOException

Implements org.mortbay.jetty.HttpContent.getInputStream.

getLastModifiedback to summary
public Buffer getLastModified()

Implements org.mortbay.jetty.HttpContent.getLastModified.

getResourceback to summary
public Resource getResource()

Implements org.mortbay.jetty.HttpContent.getResource.

releaseback to summary
public void release()

Implements org.mortbay.jetty.HttpContent.release.