简单服务器开发(七)响应静态页面到浏览器

静态页面

/httpserver-001/oa/index.html

<!DOCTYPE html>
<html>
    <head>
        <title>OA办公系统首页</title>
        <meta content="text/html;charset=utf-8"/>
    </head>
    <body>
        <center><font size="35xp" color="blue">OA办公系统首页</font></center>
    </body>
</html>

A、 在 HandlerRequest 类中 run() 方法:继续处理请求,目前已经截获到请求的 URI
B、 在 HandlerRequest 类中 run() 方法:判断请求 URI 是否以 html 或 或 htm 结尾
  a 、 如果是以 html 或 或 htm 结尾,服务器定位到该 html 文件,将其读取到 JVM 内存中

  b 、 然后再将这些 html 代码响应到浏览器页面上(怎么响应?-->输出流) ,浏览器解释执行 html 代码展示效果

  c 、 编写 public static void responseStaticPage(requestRUI,out) 方法处理静态页面请求

package com.zda.httpserver.core;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import com.zda.httpserver.util.Logger;

/**
 * 处理客户端请求
 * @author zda
 * @version 1.0
 * @since    1.0
 */
public class HandlerRequest implements Runnable {
    public Socket clientSocket;
    public HandlerRequest(Socket clientSocket){
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {
        //处理客户端请求
        BufferedReader br = null;
        PrintWriter out = null;
        Logger.log("httpserver thread: " + Thread.currentThread().getName());
        try {
            //接收客户端消息
            br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            //获取响应流对象
            out = new PrintWriter(clientSocket.getOutputStream());
            //获取请求协议的请求行
            String requestLine = br.readLine();// GET /oa/index.html HTTP/1.1
            //获取URI -> 请求行(requestLine) -> 请求方式  URI 请求协议版本号 -> 三者之间是通过一个空格进行连接
            String requestURI = requestLine.split(" ")[1];//{"GET","/oa/index.html","HTTP/1.1"}
            //判断用户请求是否为一个静态页面:以.html或.htm结尾的文件叫作html页面
            if(requestURI.endsWith(".html") || requestURI.endsWith(".htm")){
                //处理静态页面的方法
                responseStaticPage(requestURI,out);
            }
            //强制刷新
            out.flush();            
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //关闭资源
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(clientSocket != null){
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }

    /**
     * 处理静态页面
     * @param requestURI 请求URI
     * @param out    响应流对象
     */
    public void responseStaticPage(String requestURI, PrintWriter out) {
        //requestURI:/oa/index.html
        //静态页面的路径:oa/index.html
        String htmlPath = requestURI.substring(1);
        BufferedReader br = null;
        try {
            //读取页面
            br = new BufferedReader(new FileReader(htmlPath));
            StringBuilder html = new StringBuilder();
            //拼接响应信息
            html.append("HTTP/1.1 200 OK\n");
            html.append("Content-Type:text/html;charset=utf-8\n\n");
            String temp = null;
            while((temp = br.readLine()) != null){
                html.append(temp);
            }
            //输出html
            out.print(html);
        } catch (FileNotFoundException e) {
            //404找不到资源
            StringBuilder html = new StringBuilder();
            html.append("HTTP/1.1 404 NotFound\n");
            html.append("Content-Type:text/html;charset=utf-8\n\n");
            html.append("<html>");
            html.append("<head>");
            html.append("<title>404-错误</title>");
            html.append("<meta content='text/html;charset=utf-8'/>");
            html.append("</head>");
            html.append("<body>");
            html.append("<center><font size='35px' color='red'>404-Not Found</font></center>");
            html.append("</body>");
            html.append("</html>");
            out.print(html);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

猜你喜欢

转载自www.cnblogs.com/zhaideang/p/12302440.html