手写一个简单tomcat服务

1.流程图空空空

2.创建一个socket

3.启用线程

任性直接上第二,第三代码,解决一切问题

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketException;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class MinoTomcat {

    private ServerSocketChannel ssc;

    private ThreadPoolTaskConfig pool;

    public static void main(String[] args)throws Exception {
        new MinoTomcat().start();
    }


    public void start()throws Exception{
        try {
            create();
            channel();
        }catch (Exception e){
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 建立socket链接
     * @throws SocketException
     */
    public void create()throws SocketException{
        try {

            ssc = ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ServerSocket serverSocket = ssc.socket();
            serverSocket.bind(new InetSocketAddress(10001));
            //线程池处理请求
            pool = new ThreadPoolTaskConfig(200);

        }catch (SocketException e){
            throw new SocketException("create socket is error !");
        }catch (IOException e){
            throw new SocketException("io is error !");
        }
    }

    /**
     * 线程池处理请求
     * @throws Exception
     */
    public void channel()throws Exception{
        while (true){
            try{
                //获得请求
                SocketChannel channel = ssc.accept();
                //新建线程处理请求
                if(null != channel){
                    pool.getExecutor().execute(new HttpHandle(channel));
                }
            }catch (IOException e){
                throw new IOException(" accept is error !");
            }
        }
    }
}

 

4.处理请求

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class HttpHandle implements Runnable {

    private SocketChannel channel;

    HttpHandle(SocketChannel channel) {
        this.channel = channel;
    }

    @Override
    public void run() {
        try{
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            channel.read(buffer);
            buffer.flip();
            while (buffer.hasRemaining()){
                char b = (char)(buffer.get());
                String s = String.valueOf(b);
                System.out.print(s);
            }
            ByteBuffer reBuffer = ByteBuffer.allocate(1024);
            //添加的响应头
            String response = "HTTP/1.1\n";
            //添加的响应头
            response += "Content-type:text/html\n\n";
            response += "我是MinoTomat";
            reBuffer.put(new String(response).getBytes("GBK"));
            reBuffer.flip();
            channel.write(reBuffer);
            channel.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

5.线程池

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTaskConfig {

    private ExecutorService poolExecutor;

    ThreadPoolTaskConfig(int pool){
        poolExecutor = Executors.newFixedThreadPool(pool);
    }

    public ExecutorService getExecutor(){
        return poolExecutor;
    }

}

6.启动类

public class Application {

    public static void main(String[] args) throws Exception{
        new MiniTomcat().start();
    }

}

7.请求

请求http://127.0.0.1:10001

就会在控制台和页面看到相应的内容

你可能感兴趣的:(手写tomcat,手写tomcat服务,java,tomcat)