码迷,mamicode.com
首页 > 编程语言 > 详细

java webserver-封装响应协议

时间:2019-09-05 20:31:35      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:添加内容   int   buffered   get   response   请求头   inpu   web   trace   

Response:

public class Response {
    private BufferedWriter bw;
    private Socket client;
    private StringBuilder headInfo; //协议头包括状态行和请求头和回车
    private StringBuilder content;
    private int len=0; //正文的字节数
    private final String BLANK=" ";
    private final String CRLF="\r\n";

    private Response()
    {
        content=new StringBuilder();
        headInfo=new StringBuilder();
        len=0;
    }
    public Response(Socket client)
    {
        this(); //调用默认构造器
        try {
            bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        } catch (IOException e) {

            e.printStackTrace();
            headInfo=null;
        }
    }

    public Response(OutputStream os)
    {
        this();  //调用默认构造器
        bw=new BufferedWriter(new OutputStreamWriter(os));
    }
    public Response print(String info)   //流模式,不断添加内容返回自身
    {
        content.append(info);
        len+=info.getBytes().length;
        return this;
    }
    public Response println(String info)
    {
        content.append(info).append(CRLF);
        len+=(info+CRLF).getBytes().length;  //换行符也占长度
        return this;

    }
    //推送响应信息
    public void pushToBrowser(int code) throws IOException
    {
        if(null==headInfo) {
            code=505;
        }
        createHeadInfo(code);

            bw.append(headInfo);  //bw.append()以追加模式写出数据,.write()刷新原有数据,只有当前数据
            bw.append(content);
            bw.flush();

    }
    //构建头信息
    private void createHeadInfo(int code)  //传不同的code有不同的状态
    {
        //1、响应行:HTTP/1.1 200 OK
        headInfo.append("HTTP/1.1").append(BLANK);
        headInfo.append(code).append(BLANK);
        switch(code)
        {
            case 200:
                headInfo.append("OK").append(CRLF);
                break;
            case 404:
                headInfo.append("NOT FOUND").append(CRLF);
                break;
            case 505:
                headInfo.append("SERVER ERROR").append(CRLF);
                break;
        }
        //2、响应头
        headInfo.append("Date:").append(new Date()).append(CRLF);
        headInfo.append("Server:").append("shsxt Server/0.0.1;charset=GBK").append(CRLF);
        headInfo.append("Content-type:text/html").append(CRLF);
        headInfo.append("Content-length:").append(len).append(CRLF);
        headInfo.append(CRLF); //响应头和正文之间有空行

    }
}

Server:

public class Server02 {
    private ServerSocket serverSocket ;
    public static void main(String[] args) {
        Server02 server = new Server02();
        server.start();
    }
    //启动服务
    public void start() {
        try {
            serverSocket =  new ServerSocket(8888);
             receive();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("服务器启动失败....");
        }
    }
    //接受连接处理
    public void receive() {
        try {
            Socket client = serverSocket.accept();
            System.out.println("一个客户端建立了连接....");
            //获取请求协议
            InputStream is =client.getInputStream();
            byte[] datas = new byte[1024*1024];
            int len = is.read(datas);
            String requestInfo = new String(datas,0,len);
            System.out.println(requestInfo);

            //关注了内容
            Response response=new Response(client); //创建好了输出流
            response.print("<html>");    //通过输出流输出
            response.print("<head>");
            response.print("<title>");
            response.print("服务器响应成功");
            response.print("</title>");
            response.print("</head>");
            response.print("<body>");
            response.print("shsxt server终于回来了。。。。");
            response.print("</body>");
            response.print("</html>");
            //关注了状态码
            response.pushToBrowser(200);

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("客户端错误");
        }
    }
    //停止服务
    public void stop() {

    }

}

java webserver-封装响应协议

标签:添加内容   int   buffered   get   response   请求头   inpu   web   trace   

原文地址:https://blog.51cto.com/14437184/2435858

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!