手写mini版本tomcat

搭建工程 netty

http包下的 GpRequest GpResponse  GpServlet 类

public class GpRequest {

    private String  method;

    private String url;

    public GpRequest(InputStream inputStream) {

        //拿到http协议的内容

        String content ="";

        byte [] buff =new byte[1024];

        int len =0;

        try {

            if ((len = inputStream.read(buff)) >0) {

                    content =new String(buff, 0, len);

            }

            String line = content.split("\\n")[0];

            String [] arr = line.split("\\s");

            this.method = arr[0];

            this.url = arr[1].split("\\?")[0];

            System.out.println(method +"===" +url);

        } catch (IOException e) {

            e.printStackTrace();

        }

}

    public String getUrl() {

            return url;

        }

    public String    getMethod() {

        return method;

    }

}

public class GpResponse {

    private OutputStream    outputStream;

    public GpResponse(OutputStream outputStream){

            this.outputStream = outputStream;

        }

        public void write(String  str) throws IOException {

            StringBuilder sb = new StringBuilder();

                sb.append("HTTP/1.1 200 OK\n")

                    .append("Content-Type: text/html;\n")

                    .append("\r\n")

                    .append(str);

                    outputStream.write(sb.toString().getBytes());

    }

}

public abstract class GpServlet {

    public void service(GpRequest request, GpResponse response) throws IOException {

        if ("GET".equalsIgnoreCase(request.getMethod())) {

            doGet(request, response);

           } else {

                doPost(request, response);

        }

}

    protected abstract void doGet(GpRequest request, GpResponse response)throws IOException;

    protected abstract void doPost(GpRequest request, GpResponse response)throws IOException;

}

servlet 包下

public class FirstServletextends GpServlet {

    public void doGet(GpRequest request, GpResponse response) throws IOException {

        this.doPost(request, response);

    }

    public void doPost(GpRequest request, GpResponse response) throws IOException {

        response.write("This is FirstServlet");

    }

}

public class SecondServlet   extends GpServlet {

    public void doGet(GpRequest request, GpResponse response)throws IOException {

        this.doPost(request, response);

    }

    public void doPost(GpRequest request, GpResponse response)throws IOException {

            response.write("This is SecondServlet");

    }

}

 

package tomcat;

import tomcat.http.GpRequest;

import tomcat.http.GpResponse;

import tomcat.http.GpServlet;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

/**

* gptomcat

*/

public class GpTomcat {

    private int port =8080;

    private ServerSocket    serverSocket;

    private MapservletMaping = new HashMap();

    private Properties    webxml =    new Properties();

    private void init () {

        try {

                //加载web.xml文件 同时初始化ServletMapping对象

                String WEB_INF =    this.getClass().getResource("/").getPath();

                FileInputStream fileInputStream =new FileInputStream(WEB_INF +"web.properties");

                webxml.load(fileInputStream);

            for (Object k :webxml.keySet()) {

                    String key = k.toString();

                if (key.endsWith(".url"))  {

                    String servletName = key.replaceAll("\\.url$", "");

                    String url =   webxml.getProperty(key);

                    String className = webxml.getProperty(servletName +".className");

                    //单例多线程

                    GpServlet gpServlet = (GpServlet)Class.forName(className).newInstance();

                    servletMaping.put(url, gpServlet);

                }

    }

}    catch (Exception e) {

        e.printStackTrace();

       }

}

public void start(){

    //加载配置

        init();

        try {

            serverSocket =  new ServerSocket(this.port);

            System.out.println("Gp tomcat 端口已经启动 监听端口是 " +this.port);

            //2 等待用户请求 用一个死循环来等待用户请求

            while (true) {

                Socket client =serverSocket.accept();

                //4 处理用户http请求 发送得数据就是字符串 有规律的字符串(http协议)

                process(client);

            }

}    catch (Exception e) {

        e.printStackTrace();

        }

}

private void process(Socket client) {

try {

            InputStream inputStream = client.getInputStream();

            OutputStream outputStream = client.getOutputStream();

            GpRequest request =    new GpRequest(inputStream);

            GpResponse response =    new GpResponse(outputStream);

            //从协议中拿到url 把相应的servlet反射实例化

            String url =  request.getUrl();

            if (servletMaping.containsKey(url)) {

                servletMaping.get(url).service(request, response);

            }    else {

                response.write("404 - Not Fount");

            }

                outputStream.flush();

                outputStream.close();

                inputStream.close();

                client.close();

        }    catch (Exception e) {

                e.printStackTrace();

        }

}

public static void main(String[] args) {

            new GpTomcat().start();

    }

}

访问成功  tomcat本质基于ServerSocket

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值