《深入剖析Tomcat》阅读(一)

第一章 一个简单的Web服务器

该应用程序仅接受位于指定目录的静态资源的请求,如HTML文件和图像文件。它也可以将传入的HTTP请求字节流显示到控制台上。但是,它并不发送任何头信息到浏览器,如日期或者cookies等。

应用程序的入口在HttpServer的静态main方法中,main()方法会创建一个HttpServer实例。然后,调用其await()方法,顾名思义,await()方法就是在指定端口上等待HTTP请求,对其进行处理,然后发送响应信息回客户端,在接受到关闭命令之前,它会保持等待状态。

HttpServer.java

package com.simpleHttpServer;

import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.File;

public class HttpServer
{

	public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot";

	// 关闭HttpServer命令
	private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";

	// 是否收到关闭HttpServer命令
	private boolean shutdown = false;

	public static void main(String[] args)
	{
		HttpServer server = new HttpServer();
		server.await();
	}

	public void await()
	{
		ServerSocket serverSocket = null;
		int port = 8080;
		try
		{
			serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
		} catch (IOException e)
		{
			e.printStackTrace();
			System.exit(1);
		}

		// 循环等待Http请求
		while (!shutdown)
		{
			Socket socket = null;
			InputStream input = null;
			OutputStream output = null;
			try
			{
				socket = serverSocket.accept();
				input = socket.getInputStream();
				output = socket.getOutputStream();

				// 创建一个Request对象并解析
				Request request = new Request(input);
				request.parse();

				// 创建一个Response对象
				Response response = new Response(output);
				response.setRequest(request);
				response.sendStaticResource();

				// 关闭socket
				socket.close();

				// 检查是否是关闭命令
				shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
			} catch (Exception e)
			{
				e.printStackTrace();
				continue;
			}
		}
	}
}

Request.java 

package com.simpleHttpServer;

import java.io.InputStream;
import java.io.IOException;

public class Request
{

	private InputStream input;
	private String uri;

	public Request(InputStream input)
	{
		this.input = input;
	}

	public void parse()
	{
		StringBuffer request = new StringBuffer(2048);
		int i;
		byte[] buffer = new byte[2048];
		try
		{
			i = input.read(buffer);
		} catch (IOException e)
		{
			e.printStackTrace();
			i = -1;
		}
		for (int j = 0; j < i; j++)
		{
			request.append((char) buffer[j]);
		}
		System.out.print(request.toString());
		uri = parseUri(request.toString());
	}

	private String parseUri(String requestString)
	{
		int index1, index2;
		index1 = requestString.indexOf(' ');
		if (index1 != -1)
		{
			index2 = requestString.indexOf(' ', index1 + 1);
			if (index2 > index1)
				return requestString.substring(index1 + 1, index2);
		}
		return null;
	}

	public String getUri()
	{
		return uri;
	}

}

Response.java

package com.simpleHttpServer;

import java.io.OutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.File;

public class Response
{

	private static final int BUFFER_SIZE = 1024;
	Request request;
	OutputStream output;

	public Response(OutputStream output)
	{
		this.output = output;
	}

	public void setRequest(Request request)
	{
		this.request = request;
	}

	public void sendStaticResource() throws IOException
	{
		byte[] bytes = new byte[BUFFER_SIZE];
		FileInputStream fis = null;
		try
		{
			File file = new File(HttpServer.WEB_ROOT, request.getUri());
			if (file.exists())
			{
				fis = new FileInputStream(file);
				int ch = fis.read(bytes, 0, BUFFER_SIZE);
				while (ch != -1)
				{
					output.write(bytes, 0, ch);
					ch = fis.read(bytes, 0, BUFFER_SIZE);
				}
			} else
			{
				String errorMessage = "HTTP/1.1 404 File Not Found
" + "Content-Type: text/html
" + "Content-Length: 23
" + "
" + "<h1>File Not Found</h1>";
				output.write(errorMessage.getBytes());
			}
		} catch (Exception e)
		{
			System.out.println(e.toString());
		} finally
		{
			if (fis != null)
				fis.close();
		}
	}
}

 

(1)System.getProperty("user.dir")是取得当前工作目录  

HttpServer中定义的public static final String WEB_ROOT 即     工作目录/webroot

(2) 在创建TCP服务端监听的时候

try
   {
	serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
   } catch (IOException e)
   {
	 e.printStackTrace();
	 System.exit(1);
   }

程序主动捕获了IOException,深入ServerSocket源码可以看到:

public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException {
        setImpl();
        if (port < 0 || port > 0xFFFF)
            throw new IllegalArgumentException(
                       "Port value out of range: " + port);
        if (backlog < 1)
          backlog = 50;
        try {
            bind(new InetSocketAddress(bindAddr, port), backlog);
        } catch(SecurityException e) {
            close();
            throw e;
        } catch(IOException e) {
            close();
            throw e;
        }
    }

  在创建ServerSocket对象(开启TCP服务端监听)的时候可能抛出三种异常

   (1) new IllegalArgumentException  在要监听的TCP端口范围不在正确范围0-65535之间,就抛出参数异常 改异常时候RuntimeException 不用显式在方法后throws以及方法调用处try catch

   (2) SecurityException 也是一个RuntimeException  异常触发条件, 如果安全管理器存在并且其 checkListen 方法不允许进行该操作。

   (3) IOException 是非运行时异常  如果打开套接字时发生 I/O 错误。 

简要介绍下ServerSocket:

把服务器套接字绑定到特定的端口号,这样远程客户端才能定位TCP服务,如果传递进来的值为零(zero),就使用任何空闲的端口--但是客户端可能没办法访问该服务,除非用什么方式通知了客户端端口号是多少,为队列分配足够的空间以支 持特定数量的客户端套接字。在ServerSocket(int port, int numberOfClients)构造函数的重载版本 中,加入了InetAddress参数,在多地址计算机上,它允许服务器套接字绑定到某个特定的IP地址。例如,某台计算机可能有两块网 卡,或者使用虚拟IP地址把它配置成像几台计算机一样工作的时候。如果地址的值为空(null),服务 器套接字将在所有的本地地址上接受请求。在默认情况下,队列的大小设置为50,但是也提供了备用的构造函数,它允许修改这个设置。如果端口已经被绑定了,或者安全性约束条件(例如安全性规则或知名端口上的操作系统约束条件)阻挡了访问,就会产生异常。

(3) 程序创建File对象,调用了构造函数,java.io.File类中不常用的一个构造方法

根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
如果 parent 为 null,则创建一个新的 File 实例,这与调用以给定 child 路径名字符串作为参数的单参数 File 构造方法效果一样。

否则,parent 路径名字符串用于表示目录,child 路径名字符串用于表示目录或文件。如果 child 路径名字符串是绝对路径名,
则用与系统有关的方式将它转换为一个相对路径名。如果 parent 是空字符串,则通过将 child 转换为抽象路径名,
并根据与系统有关的默认目录解析结果来创建新的 File 实例。否则,将每个路径名字符串转换为一个抽象路径名,并根据父抽象路径名解析子抽象路径名。

 

参数:
parent - 父路径名字符串
child - 子路径名字符串
抛出:
NullPointerException - 如果 child 为 null

  

 public File(String parent, String child) {
        if (child == null) {
            throw new NullPointerException();
        }
        if (parent != null) {
            if (parent.equals("")) {
                this.path = fs.resolve(fs.getDefaultParent(),
                                       fs.normalize(child));
            } else {
                this.path = fs.resolve(fs.normalize(parent),
                                       fs.normalize(child));
            }
        } else {
            this.path = fs.normalize(child);
        }
        this.prefixLength = fs.prefixLength(this.path);
    }

  

原文地址:https://www.cnblogs.com/wuxinliulei/p/4852353.html