最简单的web服务器实现(一)

tomcat作为web服务器,想必大家做过web开发的都离不开tomcat了,值得庆幸的是tomcat也是开放源代码的, 最近准备好好琢磨琢磨tomcat的源码,还没开始就已经感觉到不少的未知恐惧了,慢慢来把。
可能我的学习方式比较急功近利,但是这种方式收效也快,自己记得在<>里作者写过一个最简单的web服务器实现,自己在网上也比较了一下其它的版本,还是感觉那本书里的版本比较好,在此分享出来,因为时间紧,照着书敲了一遍代码,竟然发现里面有一些很细小的错误,自己准备在这个基础上好好改进一把。
首先来看看web服务器的一些基本原理,我们的实验是基于socket的,开放了一个指定的端口,然后会启用对应的线程来处理浏览器中的请求。如果文件不存在,会报出404错误,否则会解析文件的内容。
HttpServer类是后台服务类,会开放对应的端口和socket来处理浏览发出的请求。
HttpThread类是接受浏览器发送请求的类,通过Receive和Answer来接受和处理浏览器发送的请求,然后返回到客户端中。

点击(此处)折叠或打开

  1. package new_test;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class HttpServer { 
    public static String ROOT="./wwwroot";
    public static String defaultPage="index.html";

        public static void main(String[] args) throws IOException{ 
            
                ServerSocket ss = new ServerSocket(8080); 
                while(true){
                Socket s=ss.accept();
                    System.out.println("Accept Connection...:");                                   
                     new HttpThread(s).start();
                }        
        } 
    }



点击(此处)折叠或打开

  1. package new_test;


    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import
    } java.net.Socket;


    public class HttpThread extends Thread {


    private Socket socket;


    public HttpThread(Socket s) {
    this.socket = s;


    public void run() {
    InputStream ins = null;
    OutputStream ous = null;
    try {
    ous = socket.getOutputStream();
    ins = socket.getInputStream();
    Receive rcv = new Receive(ins);
    String sURL = rcv.parse();
    System.out.println("sURL is " + sURL);


    if (sURL.equals("/")) {
    sURL = HttpServer.defaultPage;
    }
    Answer ans = new Answer(ous);
    ans.send(sURL);
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (ins != null) {
    ins.close();
    }
    if (ous != null) {
    ous.close();
    }
    if (socket != null) {
    socket.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }

点击(此处)折叠或打开

  1. package new_test;


    import java.io.IOException;
    import java.io.InputStream;


    public class Receive {
    InputStream in = null;


    public Receive(InputStream ins) {
    this.in = ins;
    }


    public String parse() {
    StringBuffer receiveStr = new StringBuffer(2048);
    int i ;
    byte[] buffer = new byte[2048];
    try {
    i = in.read(buffer);
    } catch (IOException e) {
    i = -1;
    }
    for (int j = 0; j < i; j++) {
    receiveStr.append((char)buffer[j]);


    }
    return getUri(receiveStr.toString());
    }


    private String getUri(String receiveStr) {
    int index1, index2;
    System.out.println("receiveStr is " + receiveStr);
    index1 = receiveStr.indexOf(" ");
    if (index1 != -1) {
    index2 = receiveStr.indexOf(" ", index1 + 1);
    if (index2 > index1) {
    return receiveStr.substring(index1 + 1, index2);
    }
    }
    return null;
    }


    }





点击(此处)折叠或打开

  1. package new_test;


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.OutputStream;


    public class Answer {
    OutputStream out = null;


    public Answer(OutputStream ous) {
    this.out = ous;
    }


    public void send(String pageFile) {
    byte[] bytes = new byte[2048];


    FileInputStream fis = null;
    try {
    File file = new File(HttpServer.ROOT, pageFile);
    if (file.exists()) {


    fis = new FileInputStream(file);
    int ch = fis.read(bytes, 0, 2048);
    String sBody = new String(bytes, 0);
    String sendMessage = "HTTP/1.1 200 OK\r\n"
    + "Content-Type:text/html\r\n" + "Content-Length:" + ch
    + "\r\n" + "\r\n" + sBody;
    out.write(sendMessage.getBytes());
    } else {
    String errorMessage = "Http/1.1 404 File NOT FOUND\r\n"
    + "Content-Type:text/html\r\n"
    + "Content-Length:23\r\n" + "\r\n"
    + "

    File Not Found

    ";
    out.write(errorMessage.getBytes());
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (fis != null) {
    try {
    fis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }


    }


    }


一个简单调用的情况就是,如果不存在对应的页面会直接抛出File Not Found的错误信息

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23718752/viewspace-1452767/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23718752/viewspace-1452767/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值