Java implementation HTTP Server (ii)

Source: Internet
Author: User

In the previous section, the JDK comes with the httpserver component, the implementation method is about 30 classes, the following attempts to understand the implementation of the idea.

Because Java source code has a lot of comments, paste up to see the laborious, write a program to eliminate comments.

Import Java.io.bufferedreader;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import java.io.inputstreamreader;/** * @author author E-Mail: * @version created: 2015-10-30 02:38:17 class Description Office Note from the JDK */public class test{public    static void Main (string[] args) throws IOException    {        FileInputStream InputStream = new FileInputStream ("source");        FileOutputStream outputstream = new FileOutputStream ("res");        BufferedReader br = new BufferedReader (new InputStreamReader (InputStream));        Final StringBuilder builder = new StringBuilder ();        String tempstr = null;        while ((TempStr = Br.readline ()) = null)        {            if (tempstr.indexof (' * ') = =-1)            {                builder.append (tempstr + ' \ n ');}        }        Outputstream.write (Builder.tostring (). GetBytes ("GBK"));}    }

The classes and interfaces under the Com.sun.net.httpserver package provide a range of standards

Sun.net.httpserver a series of implementations according to the standard

Httpserver.java

Package Com.sun.net.httpserver;import Com.sun.net.httpserver.spi.httpserverprovider;public Abstract class Httpserver {protected httpserver () {}//default create Httpserver object public static Httpserver create () throws Ioexcept    Ion {return Create (null, 0); }//Create Httpserver object according to Inetsocketaddress object and backlog object public static Httpserver create (inetsocketaddress addr, int Backl OG) throws IOException {//httpserver Instance service provider Httpserverprovider Httpserverprovider Provider = Httpserverp        Rovider.provider ();    The Httpserver object is created by the service provider return Provider.createhttpserver (addr, backlog);    }//BIND network address interface public abstract void bind (inetsocketaddress addr, int backlog) throws IOException;    Start Httpserver interface public abstract void Start ();//Set thread pool public abstract void Setexecutor (Executor Executor);    Public abstract Executor getexecutor ();    public abstract void Stop (int delay); Specifies the URL and the corresponding processing handler public abstract HttpContext Createcontext (String Path, HttpHandler handler);    Public abstract HttpContext Createcontext (String path);    public abstract void Removecontext (String path) throws IllegalArgumentException;    public abstract void Removecontext (HttpContext context); Public abstract inetsocketaddress getaddress ();}

  

Package Com.sun.net.httpserver.spi;import Java.io.filedescriptor;import Java.io.ioexception;import Java.security.accesscontroller;import Java.security.privilegedaction;import Java.util.iterator;import Sun.misc.service;import Sun.misc.serviceconfigurationerror;import Sun.security.action.getpropertyaction;public Abstract class Httpserverprovider {public abstract httpserver createhttpserver (inetsocketaddress addr, int backlog) T    Hrows IOException;    Public abstract Httpsserver Createhttpsserver (inetsocketaddress addr, int backlog) throws IOException;    Private static Final Object lock = new Object ();    private static Httpserverprovider Provider = NULL;        Protected Httpserverprovider () {SecurityManager sm = System.getsecuritymanager ();    if (SM! = null) sm.checkpermission (new Runtimepermission ("Httpserverprovider")); } private static Boolean Loadproviderfromproperty () {String cn = System.getproperty ("Com.sun.net.httpserver.Htt Pserverprovider ");        if (CN = = NULL) return false;             try {Class c = Class.forName (CN, True, Classloader.getsystemclassloader ());            Provider = (Httpserverprovider) c.newinstance ();        return true;        } catch (ClassNotFoundException x) {throw new Serviceconfigurationerror (x);        } catch (Illegalaccessexception x) {throw new Serviceconfigurationerror (x);        } catch (Instantiationexception x) {throw new Serviceconfigurationerror (x);        } catch (SecurityException x) {throw new Serviceconfigurationerror (x);                                        }} private static Boolean Loadproviderasservice () {Iterator i = service.providers (Httpserverprovider.class,        Classloader.getsystemclassloader ()); for (;;)                {try {if (!i.hasnext ()) return false; Provider = (Httpserverprovider) i.next ();               return true;                    } catch (Serviceconfigurationerror sce) {if (Sce.getcause () instanceof SecurityException) {                Ignore the security exception, try the next provider continue;            } throw SCE; }}} public static Httpserverprovider provider () {synchronized (lock) {if (Provider! =            NULL) return provider;                        Return (Httpserverprovider) Accesscontroller. doprivileged (New privilegedaction<object> () {                                Public Object Run () {if (Loadproviderfromproperty ())                            return provider;                            if (Loadproviderasservice ()) return provider;                            Provider = new Sun.net.httpserver.DefaultHttpServerProvider ();                       return provider; }                    }); }    }}

  

Java implementation HTTP Server (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.