⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultservlet.java

📁 是离开的肌肤了卡机是离开的就富利卡及是了的开发及拉考试及的福利科技阿斯利康的肌肤莱卡及时的离开福建阿斯顿发
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
// ========================================================================// Copyright 199-2004 Mort Bay Consulting Pty. Ltd.// ------------------------------------------------------------------------// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// ========================================================================package org.mortbay.jetty.servlet;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.MalformedURLException;import java.util.Enumeration;import java.util.List;import java.util.Map.Entry;import javax.servlet.RequestDispatcher;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.UnavailableException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.mortbay.io.Buffer;import org.mortbay.io.ByteArrayBuffer;import org.mortbay.io.WriterOutputStream;import org.mortbay.io.nio.DirectNIOBuffer;import org.mortbay.io.nio.IndirectNIOBuffer;import org.mortbay.io.nio.NIOBuffer;import org.mortbay.jetty.Connector;import org.mortbay.jetty.HttpConnection;import org.mortbay.jetty.HttpContent;import org.mortbay.jetty.HttpFields;import org.mortbay.jetty.HttpHeaderValues;import org.mortbay.jetty.HttpHeaders;import org.mortbay.jetty.HttpMethods;import org.mortbay.jetty.InclusiveByteRange;import org.mortbay.jetty.MimeTypes;import org.mortbay.jetty.ResourceCache;import org.mortbay.jetty.Response;import org.mortbay.jetty.handler.ContextHandler;import org.mortbay.jetty.nio.NIOConnector;import org.mortbay.log.Log;import org.mortbay.resource.FileResource;import org.mortbay.resource.Resource;import org.mortbay.resource.ResourceFactory;import org.mortbay.util.IO;import org.mortbay.util.MultiPartOutputStream;import org.mortbay.util.TypeUtil;import org.mortbay.util.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. :                           * <PRE>                                                                       *   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. *                     *  * </PRE> *                                                                     * * @author Greg Wilkins (gregw) * @author Nigel Canonizado */public class DefaultServlet extends HttpServlet implements ResourceFactory{       private ContextHandler.SContext _context;        private boolean _acceptRanges=true;    private boolean _dirAllowed=true;    private boolean _welcomeServlets=false;    private boolean _redirectWelcome=false;    private boolean _gzip=true;        private Resource _resourceBase;    private NIOResourceCache _nioCache;    private ResourceCache _bioCache;        private MimeTypes _mimeTypes;    private String[] _welcomes;    private boolean _aliases=false;    private boolean _useFileMappedBuffer=false;    ByteArrayBuffer _cacheControl;            /* ------------------------------------------------------------ */    public void init()        throws UnavailableException    {        ServletContext config=getServletContext();        _context = (ContextHandler.SContext)config;        _mimeTypes = _context.getContextHandler().getMimeTypes();                _welcomes = _context.getContextHandler().getWelcomeFiles();        if (_welcomes==null)            _welcomes=new String[] {"index.jsp","index.html"};                _acceptRanges=getInitBoolean("acceptRanges",_acceptRanges);        _dirAllowed=getInitBoolean("dirAllowed",_dirAllowed);        _welcomeServlets=getInitBoolean("welcomeServlets", _welcomeServlets);        _redirectWelcome=getInitBoolean("redirectWelcome",_redirectWelcome);        _gzip=getInitBoolean("gzip",_gzip);                _aliases=getInitBoolean("aliases",_aliases);        if (!_aliases && !FileResource.getCheckAliases())            throw new IllegalStateException("Alias checking disabled");        if (_aliases)            config.log("Aliases are enabled");                _useFileMappedBuffer=getInitBoolean("useFileMappedBuffer",_useFileMappedBuffer);                String rrb = getInitParameter("relativeResourceBase");        if (rrb!=null)        {            try            {                _resourceBase = _context.getContextHandler().getResource(URIUtil.SLASH).addPath(rrb);            }            catch (Exception e)             {                Log.warn(Log.EXCEPTION,e);                throw new UnavailableException(e.toString());             }        }                String rb=getInitParameter("resourceBase");        if (rrb != null && rb != null)            throw new  UnavailableException("resourceBase & relativeResourceBase");                    if (rb!=null)        {            try{_resourceBase=Resource.newResource(rb);}            catch (Exception e)             {                Log.warn(Log.EXCEPTION,e);                throw new UnavailableException(e.toString());             }        }                String t=getInitParameter("cacheControl");        if (t!=null)            _cacheControl=new ByteArrayBuffer(t);                try        {            if (_resourceBase==null)                _resourceBase = _context.getContextHandler().getResource(URIUtil.SLASH);            String cache_type =getInitParameter("cacheType");            int max_cache_size=getInitInt("maxCacheSize", -2);            int max_cached_file_size=getInitInt("maxCachedFileSize", -2);            int max_cached_files=getInitInt("maxCachedFiles", -2);            if (cache_type==null || "nio".equals(cache_type)|| "both".equals(cache_type))            {                if (max_cache_size==-2 || max_cache_size>0)                {                    _nioCache=new NIOResourceCache(_mimeTypes);                    if (max_cache_size>0)                        _nioCache.setMaxCacheSize(max_cache_size);                        if (max_cached_file_size>=-1)                        _nioCache.setMaxCachedFileSize(max_cached_file_size);                        if (max_cached_files>=-1)                        _nioCache.setMaxCachedFiles(max_cached_files);                    _nioCache.start();                }            }            if ("bio".equals(cache_type)|| "both".equals(cache_type))            {                if (max_cache_size==-2 || max_cache_size>0)                {                    _bioCache=new ResourceCache(_mimeTypes);                    if (max_cache_size>0)                        _bioCache.setMaxCacheSize(max_cache_size);                        if (max_cached_file_size>=-1)                        _bioCache.setMaxCachedFileSize(max_cached_file_size);                        if (max_cached_files>=-1)                        _bioCache.setMaxCachedFiles(max_cached_files);                    _bioCache.start();                }            }            if (_nioCache==null)                _bioCache=null;                   }        catch (Exception e)         {            Log.warn(Log.EXCEPTION,e);            throw new UnavailableException(e.toString());         }                if (Log.isDebugEnabled()) Log.debug("resource base = "+_resourceBase);    }    /* ------------------------------------------------------------ */    public String getInitParameter(String name)    {        String value=getServletContext().getInitParameter("org.mortbay.jetty.servlet.Default."+name);	if (value==null)	    value=super.getInitParameter(name);	return value;    }        /* ------------------------------------------------------------ */    private boolean getInitBoolean(String name, boolean dft)    {        String value=getInitParameter(name);        if (value==null || value.length()==0)            return dft;        return (value.startsWith("t")||                value.startsWith("T")||                value.startsWith("y")||                value.startsWith("Y")||                value.startsWith("1"));    }        /* ------------------------------------------------------------ */    private int getInitInt(String name, int dft)    {        String value=getInitParameter(name);	if (value==null)            value=getInitParameter(name);        if (value!=null && value.length()>0)            return Integer.parseInt(value);        return dft;    }        /* ------------------------------------------------------------ */    /** get Resource to serve.     * Map a path to a resource. The default implementation calls     * HttpContext.getResource but derived servlets may provide     * their own mapping.     * @param pathInContext The path to find a resource for.     * @return The resource to serve.     */    public Resource getResource(String pathInContext)    {        if (_resourceBase==null)            return null;        Resource r=null;        try        {            r = _resourceBase.addPath(pathInContext);            if (!_aliases && r.getAlias()!=null)            {                if (r.exists())                    Log.warn("Aliased resource: "+r+"=="+r.getAlias());                return null;            }            if (Log.isDebugEnabled()) Log.debug("RESOURCE="+r);        }        catch (IOException e)        {            Log.ignore(e);        }        return r;    }        /* ------------------------------------------------------------ */    protected void doGet(HttpServletRequest request, HttpServletResponse response)    	throws ServletException, IOException    {        String servletPath=null;        String pathInfo=null;        Enumeration reqRanges = null;        Boolean included =(Boolean)request.getAttribute(Dispatcher.__INCLUDE_JETTY);        if (included!=null && included.booleanValue())        {            servletPath=(String)request.getAttribute(Dispatcher.__INCLUDE_SERVLET_PATH);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -