您的位置:首页 > 运维架构 > Tomcat

Tomcat源码学习(5)-How Tomcat works(转)

2009-08-16 05:33 525 查看
HttpServer

HttpServer类代表一个web服务器并展示在Listing 1.1中。请注意,await方法放在Listing 1.2中,为了节省空间没有重复放在Listing 1.1中。

Listing 1.1: HttpServer类

package ex01.pyrmont;

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 {

/** WEB_ROOT is the directory where our HTML and other files reside.

* For this package, WEB_ROOT is the "webroot" directory under the

* working directory.

* The working directory is the location in the file system

* from where the java command was invoked.

*/

public static final String WEB_ROOT =

System.getProperty("user.dir") + File.separator + "webroot";

// shutdown command

private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";

// the shutdown command received

private boolean shutdown = false;

public static void main(String[] args) {

HttpServer server = new HttpServer();

server.await();

}

public void await() {

...

}

}

Listing 1.2: HttpServer类的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);

}

// Loop waiting for a request

while (!shutdown) {

Socket socket = null;

InputStream input = null;

OutputStream output = null;

try {

socket = serverSocket.accept();

input = socket.getInputStream();

output = socket.getOutputStream();

// create Request object and parse

Request request = new Request(input);

request.parse();

// create Response object

Response response = new Response(output);

response.setRequest(request);

response.sendStaticResource();

// Close the socket

socket.close();

//check if the previous URI is a shutdown command

shutdown = request.getUri().equals(SHUTDOWN_COMMAND);

}

catch (Exception e) {

e.printStackTrace ();

continue;

}

}

}

web服务器能提供公共静态final变量WEB_ROOT所在的目录和它下面所有的子目录下的静态资源。如下所示,WEB_ROOT被初始化:

public static final String WEB_ROOT =

System.getProperty("user.dir") + File.separator + "webroot";

代码列表包括一个叫webroot的目录,包含了一些你可以用来测试这个应用程序的静态资源。你同样可以在相同的目录下找到几个servlet用于测试下一章的应用程序。为了请求一个静态资源,在你的浏览器的地址栏或者网址框里边敲入以下的URL:
http://machineName:port/staticResource
如果你要从一个不同的机器上发送请求到你的应用程序正在运行的机器上,machineName应该是正在运行应用程序的机器的名称或者IP地址。假如你的浏览器在同一台机器上,你可以使用localhost作为machineName。端口是8080,staticResource是你需要请求的文件的名称,且必须位于WEB_ROOT里边。

举例来说,假如你正在使用同一台计算机上测试应用程序,并且你想要调用HttpServer对象去发送一个index.html文件,你可以使用一下的URL:
http://localhost:8080/index.html
要停止服务器,你可以在web浏览器的地址栏或者网址框里边敲入预定义字符串,就在URL的host:port的后面,发送一个shutdown命令。shutdown命令是在HttpServer类的静态final变量SHUTDOWN里边定义的:

private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";

因此,要停止服务器,使用下面的URL:
http://localhost:8080/SHUTDOWN
现在我们来看看Listing 1.2印出来的await方法。

使用方法名await而不是wait是因为wait方法是与线程相关的java.lang.Object类的一个重要方法。

await方法首先创建一个ServerSocket实例然后进入一个while循环。

serverSocket = new ServerSocket(port, 1,

InetAddress.getByName("127.0.0.1"));

...

// Loop waiting for a request

while (!shutdown) {

...

}

while循环里边的代码运行到ServletSocket的accept方法停了下来,只会在8080端口接收到一个HTTP请求的时候才返回:

socket = serverSocket.accept();

接收到请求之后,await方法从accept方法返回的Socket实例中取得java.io.InputStream和java.io.OutputStream对象。

input = socket.getInputStream();

output = socket.getOutputStream();

await方法接下去创建一个ex01.pyrmont.Request对象并且调用它的parse方法去解析HTTP请求的原始数据。

// create Request object and parse

Request request = new Request(input);

request.parse ();

在这之后,await方法创建一个Response对象,把Request对象设置给它,并调用它的sendStaticResource方法。

// create Response object

Response response = new Response(output);

response.setRequest(request);

response.sendStaticResource();

最后,await关闭套接字并调用Request的getUri来检测HTTP请求的URI是不是一个shutdown命令。假如是的话,shutdown变量将被设置为true且程序会退出while循环。

// Close the socket

socket.close ();

//check if the previous URI is a shutdown command

shutdown = request.getUri().equals(SHUTDOWN_COMMA
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: