我是靠谱客的博主 专一帅哥,最近开发中收集的这篇文章主要介绍(demo)实现简易的tomcat代码运行结果,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

项目代码在github的地址:https://github.com/Lirtson/MyTomcat

代码

1.请求对象

import java.io.IOException;
import java.io.InputStream;
//请求对象
public class MyRequest {
    private String url;
    private String method;
    public MyRequest(InputStream inputStream) throws IOException{
        String httpRequest="";
        byte[] httpRequestBytes=new byte[1024];
        int length=0;
        if((length=inputStream.read(httpRequestBytes))>0){
            httpRequest=new String(httpRequestBytes,0,length);
        }
        //对HTTP请求协议进行解析
        String httpHead=httpRequest.split("n")[0];
        url=httpHead.split("\s")[1];
        method=httpHead.split("\s")[0];
        System.out.println(this);
    }

    public String getUrl() {
        return url;
    }

    public String getMethod() {
        return method;
    }
}

2.响应对象

import java.io.IOException;
import java.io.OutputStream;

//响应对象
public class MyResponse {
    private OutputStream outputStream;
    public MyResponse(OutputStream outputStream){
        this.outputStream=outputStream;
    }
    public void write(String content) throws IOException{
        StringBuffer httpResponse=new StringBuffer();
        httpResponse.append("HTTP/1.1 200 OKn")
                .append("Content-Type: text/htmln")
                .append("rn")
                .append("<html><body>")
                .append(content)
                .append("</body></html>");
        outputStream.write(httpResponse.toString().getBytes());
        outputStream.close();
    }
}

3.Servlet请求处理基类

public abstract class MyServlet {
    public abstract void doGet(MyRequest myRequest,MyResponse myResponse);
    public abstract void doPost(MyRequest myRequest,MyResponse myResponse);
    public void service(MyRequest myRequest,MyResponse myResponse){
        if(myRequest.getMethod().equalsIgnoreCase("POST")){
            doPost(myRequest, myResponse);
        }
        else if(myRequest.getMethod().equalsIgnoreCase("GET")){
            doGet(myRequest, myResponse);
        }
    }
}

4.MyServlet的实现类

import java.io.IOException;
//MyServlet的实现类
public class HelloWorldServlet extends MyServlet{

    @Override
    public void doGet(MyRequest myRequest, MyResponse myResponse) {
        try{
            myResponse.write("get world...");
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    @Override
    public void doPost(MyRequest myRequest, MyResponse myResponse) {
        try{
            myResponse.write("post world...");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

5.Servlet配置

public class ServletMapping {
    private String servletName;
    private String url;
    private String clazz;

    public ServletMapping(String servletName, String url, String clazz) {
        this.servletName = servletName;
        this.url = url;
        this.clazz = clazz;
    }

    public String getServletName() {
        return servletName;
    }

    public void setServletName(String servletName) {
        this.servletName = servletName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }
}

import java.util.ArrayList;
import java.util.List;

public class ServletMappingConfig {
    public static List<ServletMapping> servletMappingList=new ArrayList<>();
    static {
        servletMappingList.add(new ServletMapping("helloworld","/world","HelloWorldServlet"));
    }
}

6.启动类

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class MyTomcat {
    private int port=8080;
    private Map<String,String> urlServletMap=new HashMap<>();
    public MyTomcat(int port){
        this.port=port;
    }
    public void start(){
        initServletMapping();
        ServerSocket serverSocket=null;
        try{
            serverSocket=new ServerSocket(port);
            System.out.println("Mytomcat is start...");
            while(true){
                Socket socket=serverSocket.accept();
                InputStream inputStream=socket.getInputStream();
                OutputStream outputStream=socket.getOutputStream();
                MyRequest myRequest=new MyRequest(inputStream);
                MyResponse myResponse=new MyResponse(outputStream);
                dispatch(myRequest,myResponse);
                socket.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){
                try{
                    serverSocket.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

    }

    private void initServletMapping() {
        for(ServletMapping servletMapping:ServletMappingConfig.servletMappingList){
            urlServletMap.put(servletMapping.getUrl(),servletMapping.getClazz());
        }
    }

    private void dispatch(MyRequest myRequest, MyResponse myResponse) {
        String clazz=urlServletMap.get(myRequest.getUrl());
        try{
            if(clazz!=null){
                Class<MyServlet> myServletClass=(Class<MyServlet>)Class.forName(clazz);
                MyServlet myServlet=myServletClass.newInstance();
                myServlet.service(myRequest,myResponse);
            }
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }catch (InstantiationException e){
            e.printStackTrace();
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new MyTomcat(8080).start();
    }
}

运行结果

在这里插入图片描述

最后

以上就是专一帅哥为你收集整理的(demo)实现简易的tomcat代码运行结果的全部内容,希望文章能够帮你解决(demo)实现简易的tomcat代码运行结果所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部