java webserver-封装响应协议

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java webserver-封装响应协议相关的知识,希望对你有一定的参考价值。

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-封装响应协议的主要内容,如果未能解决你的问题,请参考以下文章

java webserver-封装请求协议2

java webserver-获取请求协议和返回响应协议

2017-9-7 - A - WebServer项目

Nginx的重要概念之request

java webserver-http协议图

Java Http协议