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

📄 serve.java

📁 用java开发的
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    /// Returns the MIME type of the specified file.    // @param file file name whose MIME type is required	public String getMimeType( String file ) { 		   file = file.toUpperCase();		           if (file.endsWith(".HTML")        || file.endsWith(".HTM")) return "text/html";        if (file.endsWith(".TXT")) return "text/plain";        if (file.endsWith(".XML")) return "text/xml";        if (file.endsWith(".CSS")) return "text/css";        if (file.endsWith(".SGML")        || file.endsWith(".SGM")) return "text/x-sgml";        // Image        if (file.endsWith(".GIF")) return "image/gif";        if (file.endsWith(".JPG")        || file.endsWith(".JPEG")        || file.endsWith(".JPE")) return "image/jpeg";        if (file.endsWith(".PNG")) return "image/png";        if (file.endsWith(".TIF")        || file.endsWith(".TIFF")) return "image/tiff";        if (file.endsWith(".RGB")) return "image/x-rgb";        if (file.endsWith(".XPM")) return "image/x-xpixmap";        if (file.endsWith(".XBM")) return "image/x-xbitmap";        if (file.endsWith(".SVG")) return "image/svg-xml ";        if (file.endsWith(".SVGZ")) return "image/svg-xml ";        // Audio        if (file.endsWith(".AU")        || file.endsWith(".SND")) return "audio/basic";        if (file.endsWith(".MID")        || file.endsWith(".MIDI")        || file.endsWith(".RMI")        || file.endsWith(".KAR")) return "audio/mid";        if (file.endsWith(".MPGA")        || file.endsWith(".MP2")        || file.endsWith(".MP3")) return "audio/mpeg";        if (file.endsWith(".WAV")) return "audio/wav";        if (file.endsWith(".AIFF")        || file.endsWith(".AIFC")) return "audio/aiff";        if (file.endsWith(".AIF")) return "audio/x-aiff";        if (file.endsWith(".RA")) return "audio/x-realaudio";        if (file.endsWith(".RPM")) return "audio/x-pn-realaudio-plugin";        if (file.endsWith(".RAM")) return "audio/x-pn-realaudio";        if (file.endsWith(".SD2")) return "audio/x-sd2";        // Application        if (file.endsWith(".BIN")        || file.endsWith(".DMS")        || file.endsWith(".LHA")        || file.endsWith(".LZH")        || file.endsWith(".EXE")        || file.endsWith(".CLASS")) return "application/octet-stream";        if (file.endsWith(".HQX")) return "application/mac-binhex40";        if (file.endsWith(".PS")        || file.endsWith(".AI")        || file.endsWith(".EPS")) return "application/postscript";        if (file.endsWith(".PDF")) return "application/pdf";        if (file.endsWith(".RTF")) return "application/rtf";        if (file.endsWith(".DOC")) return "application/msword";        if (file.endsWith(".PPT")) return "application/powerpoint";        if (file.endsWith(".FIF")) return "application/fractals";        if (file.endsWith(".P7C")) return "application/pkcs7-mime";        // Application/x        if (file.endsWith(".JS")) return "application/x-javascript";        if (file.endsWith(".Z")) return "application/x-compress";        if (file.endsWith(".GZ")) return "application/x-gzip";        if (file.endsWith(".TAR")) return "application/x-tar";        if (file.endsWith(".TGZ")) return "application/x-compressed";        if (file.endsWith(".ZIP")) return "application/x-zip-compressed";        if (file.endsWith(".DIR")        || file.endsWith(".DCR")        || file.endsWith(".DXR")) return "application/x-director";        if (file.endsWith(".DVI")) return "application/x-dvi";        if (file.endsWith(".TEX")) return "application/x-tex";        if (file.endsWith(".LATEX")) return "application/x-latex";        if (file.endsWith(".TCL")) return "application/x-tcl";        if (file.endsWith(".CER")        || file.endsWith(".CRT")        || file.endsWith(".DER")) return "application/x-x509-ca-cert";        // Video        if (file.endsWith(".MPG")        || file.endsWith(".MPE")        || file.endsWith(".MPEG")) return "video/mpeg";        if (file.endsWith(".QT")        || file.endsWith(".MOV")) return "video/quicktime";        if (file.endsWith(".AVI")) return "video/x-msvideo";        if (file.endsWith(".MOVIE")) return "video/x-sgi-movie";        // Chemical        if (file.endsWith(".PDB")        || file.endsWith(".XYZ")) return "chemical/x-pdb";        // X-        if (file.endsWith(".ICE")) return "x-conference/x-cooltalk";        if (file.endsWith(".WRL")        || file.endsWith(".VRML")) return "x-world/x-vrml";        if (file.endsWith(".WML")) return  "text/vnd.wap.wml" ;        if (file.endsWith(".WMLC")) return  "application/vnd.wap.wmlc" ;        if (file.endsWith(".WMLS")) return  "text/vnd.wap.wmlscript" ;        if (file.endsWith(".WMLSC")) return  "application/vnd.wap.wmlscriptc" ;        if (file.endsWith(".WBMP")) return  "image/vnd.wap.wbmp" ;        return null;    }    /// Returns the name and version of the web server under which the servlet    // is running.    // Same as the CGI variable SERVER_SOFTWARE.    public String getServerInfo()    {        return Serve.Identification.serverName + " " + Serve.Identification.serverVersion +            " (" + Serve.Identification.serverUrl + ")";    }        /// Returns the value of the named attribute of the network service, or    // null if the attribute does not exist.  This method allows access to    // additional information about the service, not already provided by    // the other methods in this interface.    public Object getAttribute( String name )    {        // This server does not support attributes.        return attributes.get(name);    }        /////////////////// JSDK 2.1 extensions //////////////////////////    public void removeAttribute(String name)    {        attributes.remove(name);    }        public void setAttribute(String name, Object object)    {        attributes.put(name, object);    }        public Enumeration getAttributeNames()    {        return attributes.keys();    }        public ServletContext getContext(String uripath)    {        return this; // only root context supported    }        public int getMajorVersion()    {        return 2; // support 2.x    }        public int getMinorVersion()    {        return 3; // support 2.3    }    	// 2.3		/**	 * Returns a directory-like listing of all the paths to resources within the web application whose longest sub-path matches the supplied path argument. Paths indicating subdirectory paths end with a '/'. The returned paths are all relative to the root	 *  of the web application and have a leading '/'. For example, for a web application containing	 * <p>	 * /welcome.html <br>	 * /catalog/index.html<br>	 * /catalog/products.html<br>	 * /catalog/offers/books.html<br>	 * /catalog/offers/music.html<br>	 * /customer/login.jsp<br>	 * /WEB-INF/web.xml<br>	 * /WEB-INF/classes/com.acme.OrderServlet.class,	 * <p>	 * getResourcePaths("/") returns {"/welcome.html", "/catalog/", "/customer/", "/WEB-INF/"}<br>	 * getResourcePaths("/catalog/") returns {"/catalog/index.html", "/catalog/products.html", "/catalog/offers/"}.	 * <p>	 * 	 * @param the - partial path used to match the resources, which must start with a /	 * @return a Set containing the directory listing, or null if there are no resources in the web application whose path begins with the supplied path.	 * @since Servlet 2.3 	 * 	 */	public java.util.Set getResourcePaths(java.lang.String path) {		// TODO: implement		return null;	}		/**	 * Returns the name of this web application correponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element.     * @return The name of the web application or null if no name has been declared in the deployment descriptor.	 *	 * @since Servlet 2.3 	 */	public java.lang.String getServletContextName() {		return null;//"ROOT";	}		// only root relative in this implementation    public URL getResource(String path) throws MalformedURLException    {		return new URL("http", hostName, port, path);    }        public InputStream getResourceAsStream(String path)    {        return null; // we don't provide resources in this way    }    	public RequestDispatcher getRequestDispatcher(String urlpath)	{		return this; // we don't provide resource dispatching in this way	}		// no way to specify parameters for context	public String getInitParameter(String param) {		return null;	}		public Enumeration getInitParameterNames() {		return null;	}		public RequestDispatcher getNamedDispatcher(String name) {		return this;	}	synchronized String generateSessionId() {		return "-"+System.currentTimeMillis()+'-'+(uniqer++)+'-'+Math.round(Math.random()*1000);	}    	public void forward(ServletRequest _request, ServletResponse _response)		throws ServletException, java.io.IOException {	}		public void include(ServletRequest _request, ServletResponse _response)             throws ServletException, java.io.IOException {	}	    final static class Identification    {        public static final String serverName = "Rogatkin's JWS based on Acme.Serve";        public static final String serverVersion = "$Revision: 1.1 $";        public static final String serverUrl = "http://tjws.sourceforge.net";                /// Write a standard-format HTML address for this server.        public static void writeAddress( OutputStream o ) throws IOException        {            PrintStream p = new PrintStream( o );            p.println(                "<ADDRESS><A HREF=\"" + serverUrl + "\">" +                serverName + " " + serverVersion + "</A></ADDRESS>" );        }        public static void writeAddress( StringBuffer sb ) throws IOException        {            sb.append(                "<ADDRESS><A HREF=\"" + serverUrl + "\">" +                serverName + " " + serverVersion + "</A></ADDRESS>" );        }    }}class ServeConfig implements ServletConfig{        private ServletContext context;    private Hashtable init_params;	private String servletName;        public ServeConfig( ServletContext context )    {        this(context, null, "undefined");    }        public ServeConfig( ServletContext context, Hashtable initParams, String servletName )    {        this.context = context;        this.init_params = initParams;		this.servletName = servletName;    }        // Methods from ServletConfig.        /// Returns the context for the servlet.    public ServletContext getServletContext()    {        return context;    }        /// Gets an initialization parameter of the servlet.    // @param name the parameter name    public String getInitParameter( String name )    {        // This server supports servlet init params. :)        if (init_params != null)            return (String)init_params.get(name);        return null;    }        /// Gets the names of the initialization parameters of the servlet.    // @param name the parameter name    public Enumeration getInitParameterNames()    {        // This server does:) support servlet init params.        if (init_params != null)            return init_params.keys();        return new Vector().elements();    }	// 2.2	public String getServletName() {		return servletName;	}}////////////////////////////////////////////////////////////////////////** * provides request/response */class ServeConnection implements Runnable, HttpServletRequest, HttpServletResponse{        private Socket socket;    private Serve serve;        private ServletInputStream in;    private ServletOutputStream out;        private Hashtable formParameters;    private Hashtable attributes =  new Hashtable();        public final static String WWWFORMURLENCODE = "application/x-www-form-urlencoded";    public final static String TRANSFERENCODING="Transfer-Encoding";      public final static String CHUNKED = "chunked";     public final static String CONTENTLENGTH = "Content-Length";    public final static String CONTENTTYPE = "Content-Type";    public final static String SETCOOKIE = "Set-Cookie";	public final static String COOKIE = "Cookie";		public final static String SESSION_COOKIE_NAME = "JSESSIONID";// URL rewriting http://www.myserver.com/catalog/index.html;jsessionid=mysession1928// like: http://www.sun.com/2001-0227/sunblade/;$sessionid$AD5RQ0IAADJAZAMTA1LU5YQ        private String reqMethod; // == null by default    private String reqUriPath;    private String reqProtocol;	private String reqCharEncoding;    private String remoteUser;    private String authType;    private boolean oneOne;     // HTTP/1.1 or better    private boolean reqMime;    String reqQuery = null;    private Vector reqHeaderNames = new Vector();    private Vector reqHeaderValues = new Vector();	private Locale locale; // = java.util.Locale.getDefault();    private int uriLen;    private static final Hashtable EMPTYHASHTABLE = new Hashtable();		private Vector outCookies;	private Vector inCookies;		private String sessionCookieValue;        /// Constructor.    public ServeConnection( Socket socket, Serve serve )    {        // Save arguments.        this.socket = socket;        this.serve = serve;                // Start a separate thread to read and handle the request.        Thread thread = new Thread( this, "Request handler" );        thread.start();    }            // Methods from Runnable.    public void run()    {        try        {            // Get the streams.            in = new ServeInputStream( socket.getInputStream() );            out = new ServeOutputStream( socket.getOutputStream(), this );        }        catch ( IOException e )        {            problem( "Getting streams: " + e.getMessage(), SC_BAD_REQUEST );        }                parseRequest();        

⌨️ 快捷键说明

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