手写一个简单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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值