简易WebServer v2.0

增加了简易的获取参数信息后逻辑判断再将数据流传输至浏览器

package cn.com.demo.tomcat;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RequestDealThread extends Thread {
    private Socket socket;

    public RequestDealThread(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();
            // 1、获取请求内容
            InputStreamReader reader = new InputStreamReader(in);
            char[] chars = new char[1024];
            int count = reader.read(chars);
            System.out.println(count);
            // 2、获取请求名称
            String reqStr = new String(chars, 0, count);
            String[] lines = reqStr.split("\n");// 获取到请求信息后,用split通过换行分割
            String reqName = null;
            if (lines != null && lines.length > 0) {
                String firstLine = lines[0];// split分割后的第一行
                String[] firstLines = firstLine.split(" ");// 将分割后的第一行信息用split通过空格分割
                if (firstLines != null && firstLines.length == 3) {
                    reqName = firstLines[1];// 得到分割后的第二个字符串
                    System.out.println(reqName);
                }
            }
            
            
            // 4、将内容和响应头信息合并,写入输出流
            out.write("HTTP/1.1 200 OK\n".getBytes());
            out.write("Server: Apache-Coyote/1.1\n".getBytes());
            out.write("Date: Wed, 27 Dec 2017 12:59:42 GMT\n".getBytes());
            out.write("\n".getBytes());

            // 判断请求名称中是否带参数,若有则提取出来
            int index = reqName.indexOf("?");
            String paramStr = null;
            if (index != -1) {
                paramStr = reqName.substring(index + 1);
                reqName = reqName.substring(0, index);
            }
            if (reqName.equals("/hello")) {
                String name = null;
                if (paramStr != null) {
                    String[] params = paramStr.split("=");
                    if (params != null && params.length == 2) {
                        if ("name".equals(params[0])) {
                            name = params[1];
                        }

                    }
                }
                //生成包含问候姓名的html并且返回
                out.write("<html>".getBytes());
                out.write("<head>".getBytes());
                out.write("<title>测试</title>".getBytes());
                out.write("<meta http-quiv='Content-Type' content='text/html';charset='UTF-8'/ >".getBytes());
                out.write("</head>".getBytes());
                out.write(("<body> hello"+name+"</body>").getBytes());
                out.write("</html>".getBytes());
            } else if (reqName.equals("/time")) {
                Date date = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String dateStr = format.format(date);
            } else {
                // 3、根据资源请求名称,读取文件内容
                InputStream resourceIN = this.getClass().getResourceAsStream(
                        reqName);
                byte[] bytes = new byte[1024];
                while ((count = resourceIN.read(bytes)) != -1) {
                    out.write(bytes, 0, count);
                }
                resourceIN.close();
            }
            out.flush();
            in.close();
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容