随着互联网的日益普及,Web应用越来越丰富。当人们在浏览器的地址栏里输入
一个网址,就能显示出一张漂亮的网页。对于普通用户来说,能看到包含所需信息的
网页就足够了,但对开发人员来说,就必须对从输入网址到显示出网页 这个过程中的
相关细节了然于胸。
这个过程简单地说,就是由浏览器通过Socket的形式经由网络向服务器提交
一个请求,然后服务器将这个请求转交给相关方面来处理,并将处理结果通过
Socket返回给浏览器。详细过程如下图所示:
在这个过程中,我们可以看到有以下实体参与到整个过程当中:
1、HTTP Requests
2、HTTP Responses
3、The Socket Class
4、The ServerSocket Class
5、The HttpServer Class
6、The Request Class
7、The Response Class
下面我们逐一分析以上这些实体。
1、HTTP Requests
一个HTTP请求包含以下三个部分:
1). Method-URI-Protocol/Version
2). Request headers
3). Entity body
一个HTTP请求的实例如下所示:
POST /servlet/default.jsp HTTP/1.1
Accept: text/plain; text/html
Accept-Language: en-gb
Connection: Keep-Alive
Host: localhost
Referer: http://localhost/ch8/SendDetails.htm
User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)
Content-Length: 33
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
LastName=Franks&FirstName=Michael
2、HTTP Responses
一个HTTP回应包含以下三个部分:
1). Protocol-Status code-Description
2). Response headers
3). Entity body
一个HTTP回应的实例如下所示:
HTTP/1.1 200 OK
Server: Microsoft-IIS/4.0
Date: Mon, 3 Jan 1998 13:13:33 GMT
Content-Type: text/html
Last-Modified: Mon, 11 Jan 1998 13:23:42 GMT
Content-Length: 112
<html>
<head>
<title>HTTP Response Example</title></head><body>
Welcome to Brainy Software
</body>
</html>3、 The Socket Class
在网络上通信,可以采取Socket的形式,也就是说在要通信的两端分别建立
一个Socket对象。浏览器端的Socket发送HTTP请求,并接受来自服务器端的HTTP回应。
一个浏览器端的Socket实例如下:
Socket socket = new Socket("127.0.0.1", "8080");
OutputStream os = socket.getOutputStream();
boolean autoflush = true;
PrintWriter out = new PrintWriter( socket.getOutputStream(), autoflush );
BufferedReader in = new BufferedReader(
new InputStreamReader( socket.getInputStream() ));
// send an HTTP request to the web server
out.println("GET /index.jsp HTTP/1.1");
out.println("Host: localhost:8080");
out.println("Connection: Close");
out.println();
// read the response
boolean loop = true;
StringBuffer sb = new StringBuffer(8096);
while (loop) {
if ( in.ready() ) {
int i=0;
while (i!=-1) {
i = in.read();
sb.append((char) i);
}
loop = false;
}
Thread.currentThread().sleep(50);
}
// display the response to the out console
System.out.println(sb.toString());
socket.close();
4、The HttpServer Class
HttpServer代 表web服务器,它通过ServerSocket接收来自浏览器的请求,
或者返回被请求的静态资源,或者将请求转交给servlet处理,并将处理结果
返 回给浏览器。在HttpServer的入口main()方法中,会调用一个await()方法,
等待处理来自浏览器的请求。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;
}
}
}
5、The Request Class 和 Response Class
一 个Request对象代表一个HTTP请求,当服务器获取一个HTTP请求时,
会根据InputStream创建一个Request对象,然后 Request对象解析出被
请求的静态资源或servlet的URI地址,从而将静态资源写入Response对象
返回给浏览器,或者是将请求转交给 servlet处理,并将处理结果通过
Response对象返回。
参考资料:
How Java Web Servers WorkBudi Kurniawan http://www.onjava.com/pub/a/onjava/2003/04/23/java_webserver.html

7万+

被折叠的 条评论
为什么被折叠?



