java webserver-封装响应协议

发布时间:2020-06-26 02:37:32 作者:wx5d21d5e6e5ab1
来源:网络 阅读:251

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() {

    }

}
推荐阅读:
  1. java中HTTP请求流程是怎么样的
  2. java webserver-封装请求协议2

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java bs ava

上一篇:DialogFragment

下一篇:Mock工具wiremock-py

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》