03-封装Response响应

package com.day5;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class Tomcat {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        
        //申请注册8080套接字        
        ServerSocket server=new ServerSocket(8080);
        
        System.out.println("开始接受请求...");
        //接受客户端请求
        Socket socket = server.accept();
        
        Response response=new Response(socket);
        
        //设置网页DOM数据
        response.write("<title>Thinking in Java</title>");
        response.write("<h1>liu shi hua</h1>");
        response.write("<h2 style='color:red;'>[email protected]</h2>");
        
        response.pushToClient();
        
        socket.close();
        server.close();
                

    }

}
package com.day5;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Date;

/**
 * 简单封装Response对象
 * @author 刘诗华 liu shi hua...
 * 
 */
public class Response {
    
    //换行符
    private final String CRLF = "\r\n";
    //客户端socket对象
    private Socket socket;
    //缓冲字符流输出对象
    private BufferedWriter bw=null;
    //正文内容长度
    private int len;
    //响应头
    private StringBuilder headerStr;
    //正文内容
    private StringBuilder content;
    
    
    public Response()
    {
        len=0;
        this.headerStr=new StringBuilder();
        this.content=new StringBuilder();
    }
    
    public Response(Socket socket) throws Exception
    {
        this();
        this.socket=socket;
        //字符输出流对象
        OutputStream out = this.socket.getOutputStream();
        //将字节输出流转换成字符输出流
        OutputStreamWriter writer = new OutputStreamWriter(out);
        
        this.bw=new BufferedWriter(writer);
    }
    
    //构造响应报文
    public void setHeader()
    {
        this.headerStr.append("HTTP/1.1 200 OK").append(CRLF);
        this.headerStr.append("Server:Apache/2.4.10 (Win32) PHP/5.3.28").append(CRLF);
        this.headerStr.append("Date:").append(new Date()).append(CRLF);
        this.headerStr.append("Content-Type:text/html").append(CRLF);
        this.headerStr.append("Content-Length: ").append(len).append(CRLF);
        this.headerStr.append(CRLF);
    }
    
    //设置正文内容信息
    public Response write(String str)
    {
        //计算正文内容长度
        len+=(str+CRLF).getBytes().length;
        this.content.append(str).append(CRLF);
        return this;
    }
    
    
    //发送数据到客户端
    public void pushToClient() throws Exception
    {
        //第一步,构造响应头
        setHeader();
        //第二步,追加正文内容
        this.bw.append(this.headerStr.toString());
        this.bw.append(this.content.toString());
    
        this.bw.flush();
        //关闭资源
        this.bw.close();
        this.socket.close();
    }
}

执行结果:

猜你喜欢

转载自www.cnblogs.com/hua900822/p/9813706.html
03-