webserver Server Optimization

Code directly:

cn.tedu.core Package; 

Import java.io.IOException; 
Import a java.io.OutputStream; 
Import java.io.PrintStream; 
Import java.io.PrintWriter; 
Import the java.net.ServerSocket; 
Import the java.net.Socket; 
Import java.util.concurrent.ExecutorService; 
Import java.util.concurrent.Executors; 

/ * * ZHEzhe this class is used to read the server program 
 * life ServerSocket object 1. 
 * 2. complete ServerSocket object constructor initializes the 
 * 3 provide start method, the client receives the request and the corresponding 
 * 4. provide main method, start the server 
 * * / 
public  class WebServer {
     // statement serversocket objects ,, he is standing on the service side of the 
    Private ServerSocket serverSocket; 

    // life a thread pool 
    privateThreadPool ExecutorService; 

    // done in the constructor initializes 
    public   WebServer () {
         the try { 
            serverSocket = new new the ServerSocket ( 8080 ); 

            // initialize the thread pool object, creating a fixed number of thread pool, the maximum number of 100 
            ThreadPool = Executors.newFixedThreadPool ( 100 ); 
        } the catch (IOException E) { 
            e.printStackTrace (); 
        } 
    } 

    // tigongstartfangfa 
    public  void Start () {
         the try {
             the while ( to true ) {
                // continue receiving client requests 
                the Socket socket = serverSocket.accept (); 

                // execution thread class 
                threadPool.execute ( new new ClientHandler (socket)); 


                // return to the browser 
                / * organization's data format does not comply with the http protocol 
                OutputStream outputStream Socket.getOutputStream = (); 
                OutputStream.write ( "the Hello" .getBytes ()); 
                outputStream.flush (); * / 

                // transform output format 
                / * PrintWriter output character 
                * PrintStream output in addition to the character can picture what the * / 
                / * PrintStream PrintStream new new PS = (Socket.getOutputStream ()); 
                // status line splice
                ps.println ( "the HTTP / 1.1 200 is the OK"); 
                // response header, the respective content: page type 
                ps.println ( "Context-Type: text / html"); 
                the length of the corresponding content // 
                String data = "hello server ~ "; 
                ps.println (" the content-the Length: "+ data.length ()); 

                // blank lines 
                ps.println (); 

                // spliced entity corresponding content 
                ps.write (data.getBytes ()); 
                PS. the flush (); 
                the Socket.close (); * / 

            } 

        } the catch (IOException E) { 
            e.printStackTrace (); 
        } 
    } 


    // provide a method for starting main
    public  static  voidmain (String [] args) { 
        WebServer webServer = new new WebServer ();
         // receives the request and corresponding 
        webServer.start (); 
    } 

}

 

cn.tedu.core Package; 

Import java.io.BufferedInputStream The; 
Import java.io.BufferedReader; 
Import java.io.File; 
Import a java.io.FileInputStream; 
Import java.io.IOException; 
Import a java.io.InputStream; 
Import the java.io.InputStreamReader; 
Import java.io.PrintStream; 
Import java.io.PrintWriter; 
Import the java.net.Socket; 
Import java.util.Arrays; 
Import java.util.Spliterator; 

Import com.sun.corba.se. impl.orb.ParserTable.TestAcceptor1; 
Import com.sun.org.apache.xpath. Internal .operations.Bool; 

Import sun.java2d.pipe.SpanClipRenderer; 


/ * * class this class as a thread, the code extraction method sTART * / 

public class ClientHandler the implements the Runnable { 

    // . 1 Statement socket object. 4 
    Private the Socket socket;
     // 2zao socket object passed in the paparazzi function, and stores the class 
    public ClientHandler (the Socket socket) {
         // the this indicates that this class object 
        the this .socket = Socket; 
    } 

    // . 3 Run rewriting method, extraction Start 
    @Override
     public  void RUN () {
         // corresponding code 
        the try { 

            // get data request line
             // BufferdReader row of data can be read, the character stream
             // the InputStreamReader words it can converted into a character stream throttle
             //             Socket.getInputStream () is a stream of bytes
            BufferedReader bReader = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
            //获取第一行数据
            String readLine = bReader.readLine();
            System.out.println(readLine);

            if (readLine!=null) {
                String[] split = readLine.split(" ");
                System.out.println(Arrays.toString(split));
                String url = split[1];
                if(!url.equals("index.html " ) || url.equals (! " the test.html " )) { 
                    URL = " index.html " ; 
                } 
                    

                PrintStream PS =
                         new new PrintStream (Socket.getOutputStream ());
                 // guaranteed only promises made in response to the first a 
                Boolean = LL to false ;
                 IF (LL!) { // if not sends a transmission
                     // respective states 
                    ps.println ( " the HTTP /: 200 is the OK " );
                     //xiang respective head 
                    ps.println ( " the Context-the Type: text / HTML " ); 
                    String Data = " IS OK !!! " ; 
                    File File = new new File ( " the WebContent / " + URL); 
                    ps.println ( " Content- the Length " + file.length ());
                     // blank lines 
                    ps.println ();
                     // respective entity content
                     //             ps.write (data.getBytes ());
                    BufferedInputStream bi = 
                            new BufferedInputStream(new FileInputStream(file));
                    byte[] b = new byte[(int) file.length()];
                    bi.read(b);
                    ps.write(b);
                    ps.flush();
                    socket.close();
                    
                    ll=true;
                }
                
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

 

Guess you like

Origin www.cnblogs.com/gxlaqj/p/11354218.html