tomcat源码编写计划连载(2)

  上一次讲了一下server的基础实现,但是实现的只是简单的静态资源的请求,用过tomcat的童鞋肯定说要调用servlet啦,OK, 在上次的基础上我增加了一个请求servlet的功能。


servlet的生命周期不做多的介绍了,我主要实现两个方法init和 service ,结构目录如下:

服务器端:

HttpServer1 接收HTTP请求

Rquest 解析请求,给servlet使用

Response 返回信息,给Servlet使用

ServletProcesser1 处理servlet请求

ServletResourceProcessor 处理静态资源请求

客户端:

Client,java 发送http请求


ServletDemo1.java是我实现的Servlet,Servlet.java是我定义的servlet接口.进行规范


Client.java:

这段代码不发了,可以从上一章看到


HttpServer1:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by user1013 on 2016/7/1.
 */
public class HttpServer1 {
    /** WEB_ROOT is the directory where our HTML and other files reside.
     * For this package, WEB_ROOT is the "webroot" directory under the
     * working directory.
     * The working directory is the location in the file system
     * from where the java command was invoked.
     */
    public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webRoot" + "servletDemo"+File.separator +"src";
    // shutdown command
    private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
    // the shutdown command received
    private boolean shutdown = false;

    public static void main(String[] args) {
        HttpServer1 server = new HttpServer1();
        server.await();
    }

    public void await() {
        System.out.print("服务器启动。。。");
        ServerSocket serverSocket = null;
        int port = 8080;
        try {
            serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
        }
        catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        // Loop waiting for a request
        while (!shutdown) {
            Socket socket = null;
            InputStream input = null;
            OutputStream output = null;
            try {
                socket = serverSocket.accept();
                input = socket.getInputStream();
                output = socket.getOutputStream();
                // create Request object and parse
                Request request = new Request(input);
                request.parse();
                // create Response object
                Response response = new Response(output);
                response.setRequest(request);
                // check if this is a request for a servlet or
                // a static resource
                // a request for a servlet begins with "/servlet/"
                if (request.getUri().startsWith("/servlet/")) {
                    ServletProcesser1 processor = new ServletProcesser1();
                    processor.process(request, response);
                }else {
                    StaticResourceProcessor processor = new StaticResourceProcessor();
                    processor.process(request, response);
                }
                // Close the socket
                socket.close();
                //check if the previous URI is a shutdown command
                shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
            }
            catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }
}


Request.java

/**
 * Created by user1013 on 2016/7/1.
 */
import java.io.InputStream;
import java.io.IOException;

public class Request {
    private InputStream input;
    private String uri;
    public Request(InputStream input){
        this.input = input;
    }

    public String getUri() {
        return uri;
    }
    private String parseUri(String requestString) {
        int index1, index2;
        index1 = requestString.indexOf(' ');
        if (index1 != -1) {
            index2 = requestString.indexOf(' ', index1 + 1);
            if (index2 > index1)
                return requestString.substring(index1 + 1, index2);
        }
        return null;
    }

    public void parse() {
        // Read a set of characters from the socket
        StringBuffer request = new StringBuffer(2048);
        int i;
        byte[] buffer = new byte[2048];
        try {
            i = input.read(buffer);
        }
        catch (IOException e) {
            e.printStackTrace();
            i = -1;
        }
        for (int j=0; j<i; j++) {
            request.append((char) buffer[j]);
        }
        System.out.print(request.toString());
        this.uri = parseUri(request.toString());
    }
}
 Response.java

/**
 * Created by user1013 on 2016/7/1.
 */
import java.io.OutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Locale;
public class Response {
    private static final int BUFFER_SIZE = 1024;
    Request request;
    OutputStream output;
    PrintWriter writer;
    public Response(OutputStream output) {
        this.output = output;
    }
    public void setRequest(Request request) {
        this.request = request;
    }
    // 获取静态资源的方法
    /* This method is used to serve static pages */
    public void sendStaticResource() throws IOException {
        byte[] bytes = new byte[BUFFER_SIZE];
        FileInputStream fis = null;
        try {
            /* request.getUri has been replaced by request.getRequestURI */
            File file = new File(HttpServer1.WEB_ROOT, request.getUri());
            fis = new FileInputStream(file);
            /*
            HTTP Response = Status-Line
            *(( general-header | response-header | entity-header ) CRLF)
            CRLF
            [ message-body ]
            Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
            */
            int ch = fis.read(bytes, 0, BUFFER_SIZE);
            while (ch!=-1) {
                output.write(bytes, 0, ch);
                ch = fis.read(bytes, 0, BUFFER_SIZE);
            }
        }
        catch (FileNotFoundException e) {
            String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
                    "Content-Type: text/html\r\n" +
                    "Content-Length: 23\r\n" +
                    "\r\n" +
                    "<h1>File Not Found</h1>";
            output.write(errorMessage.getBytes());
        }
        finally {
            if (fis!=null)
                fis.close();
        }
    }

    /**
     * PrintWriter 类的构造方法的第二个参数是一个布尔值表明是否允许
     自动刷新。传递 true 作为第二个参数将会使任何 println 方法的调用都会刷新输出(output)。
     不过, print 方法不会刷新输出。
     * @return
     * @throws IOException
     */
    public PrintWriter getWriter() throws IOException {
        // autoflush is true, println() will flush,
        // but print() will not.
        writer = new PrintWriter(this.output, true);
        return writer;
    }
}

ServletProcesser1.java

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandler;

/**
 * 李文波
 * Created by user1013 on 2016/7/1.
 * ServletProcessor1 类用于处理 servlet 的 HTTP 请求。
 */
public class ServletProcesser1 {

    public void process(Request request, Response response) {
        //1. 初始化一些信息
        URLClassLoader loader = null;
            //1.获得请求的uri
            String uri = request.getUri();
            //2.获取uri中的请求的servlet参数
            System.out.println(uri);
            String servletName = uri.substring(uri.indexOf("/", 1) + 1);
        System.out.println(servletName);
        try {
            URL[] urls = new URL[1];
            URLStreamHandler urlStreamHandler = null;
            File classPath = new File(HttpServer1.WEB_ROOT);
            String repository = (new URL("file", null, classPath.getCanonicalPath()+ File.separator)).toString();
//            String repository = "E:\\test\\servletDemo\\src";
            System.out.println(repository);
            urls[0] = new URL(null, repository, urlStreamHandler);
            loader = new URLClassLoader(urls);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        //2.加載servlet
        Class myClass = null;
        try {
            myClass = loader.loadClass(servletName);
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }


        //3.运行servlet的service方法
        Servlet servlet = null;
        try {
            servlet = (Servlet) myClass.newInstance();
            servlet.init();
            servlet.service(request, response);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) throws IOException {
//        File classPath = new File(HttpServer1.WEB_ROOT);
//        String repository = (new URL("file", null, classPath.getCanonicalPath()+ File.separator)).toString();
//        System.out.println(repository);
//    }
}

StaticResourceProcessor.java
import java.io.IOException;

/**
 * Created by user1013 on 2016/7/1.
 */
public class StaticResourceProcessor {

    public void process(Request request, Response response){
        try {
            response.sendStaticResource();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


ServletDemo1.java

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by user1013 on 2016/7/4.
 */
public class ServletDemo1 implements Servlet{
    @Override
    public void init() {
        System.out.print("init method");
    }

    @Override
    public void service(Request request, Response response) throws IOException {
        PrintWriter writer = response.getWriter();
        writer.println("servlet Demo1");
        writer.flush();
    }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值