1. "《How Tomcat Works_Chapter 1: A Simple Web Server》"
1.1: HTTP hypetext transfer protocal,
1.2: Request: split a uri from address input
1.3: Response: get the uri to read the file from the servers
1.4: HttpServer: new a socket to receive & send bytes info
2. "《How Tomcat Works_Chapter 2: A Simple Servlet Container》"
2.1: When the servlet is called for the first time, load the servlet class and call the
servlet's' init method (once only)
2.2: For each request, construct an instance of javax.servlet.ServletRequest and
an instance of javax.servlet.ServletResponse.
2.3: Invoke the servlet's' service method, passing the ServletRequest and
ServletResponse objects.
2.4: When the servlet class is shut down, call the servlet's' destroy method and
unload the servlet class.
HttpServer.java:
1
package
chap1_ASimpleWebServer;
2
import
java.io.File;
3
import
java.io.IOException;
4
import
java.io.InputStream;
5
import
java.io.OutputStream;
6
import
java.net.InetAddress;
7
import
java.net.ServerSocket;
8
import
java.net.Socket;
9
10
11
public
class
HttpServer {
12
public
static
final
String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot"
;
13
14
private
static
final
String SHUTDOWN_COMMAND = "/SHUTDOWN"
;
15
16
private
boolean
shutdown =
false
;
17
18
public
void
await() {
19
ServerSocket serverSocket =
null
;
20
int
port = 8080
;
21
try
{
22
serverSocket =
new
ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"
));
23
}
catch
(IOException e) {
24
e.printStackTrace();
25
System.exit(1
);
26
}
27
//
loop waiting for a request
28
while
(!
shutdown) {
29
Socket socket =
null
;
30
InputStream input =
null
;
31
OutputStream output =
null
;
32
try
{
33
socket =
serverSocket.accept();
34
input =
socket.getInputStream();
35
output =
socket.getOutputStream();
36
//
create Request object & parse
37
Request request =
new
Request(input);
38
request.parse();
39
//
create Response object
40
Response response =
new
Response(output);
41
response.setRequest(request);
42
response.sendResource();
43
//
close socket
44
socket.close();
45
//
check if the previous URI is a shutdown command
46
shutdown =
request.getUri().equals(SHUTDOWN_COMMAND);
47
}
catch
(IOException e) {
48
e.printStackTrace();
49
continue
;
50
}
51
}
52
}
53
54
public
static
void
main(String[] args) {
55
System.out.println(WEB_ROOT);
56
HttpServer server =
new
HttpServer();
57
server.await();
58
}
59
}
Request.java:
package
chap1_ASimpleWebServer;
import
java.io.IOException;
import
java.io.InputStream;
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
len;
byte
[] buffer =
new
byte
[2048
];
try
{
len
=
input.read(buffer);
}
catch
(IOException e) {
e.printStackTrace();
len
= -1
;
}
for
(
int
i = 0; i < len; i++
) {
request.append((
char
)buffer[i]);
//
do not forget cast type byte to char
}
System.out.println(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:
1
package
chap1_ASimpleWebServer;
2
import
java.io.File;
3
import
java.io.FileInputStream;
4
import
java.io.IOException;
5
import
java.io.OutputStream;
6
7
8
public
class
Response {
9
private
static
final
int
BUFFER_SIZE = 1024
;
10
Request request;
11
OutputStream output;
12
13
public
Response (OutputStream output) {
14
this
.output =
output;
15
}
16
17
public
void
setRequest(Request request) {
18
this
.request =
request;
19
}
20
21
public
void
sendResource()
throws
IOException {
22
byte
[] bytes =
new
byte
[BUFFER_SIZE];
23
FileInputStream fis =
null
;
24
try
{
25
File file =
new
File(HttpServer.WEB_ROOT, request.getUri());
26
System.out.println(request.getUri().toString());
27
if
(file.exists()) {
28
fis =
new
FileInputStream(file);
29
int
len = fis.read(bytes, 0
, BUFFER_SIZE);
30
while
(len != -1
) {
31
output.write(bytes, 0
, len);
32
len = fis.read(bytes, 0
, BUFFER_SIZE);
33
}
34
}
else
{
35
String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
36
"ContentType: text/html\r\n" +
37
"ContentLength: 23\r\n\r\n" +
38
"<head>File Not Found</head>"
;
39
output.write(errorMessage.getBytes());
40
}
41
}
catch
(Exception e) {
42
e.printStackTrace();
43
}
finally
{
44
if
(fis !=
null
) {
45
fis.close();
46
}
47
}
48
}
49
}
简单的html, 放在webroot文件夹下:
<
html
>
<
head
>
<
title
>
Hello Tomcat!
</
title
>
</
head
>
<
body
>
<
img
src
="./images/tomcat.jpg"
>
<
br
>
<
h1
>
It's Works!
</
h1
>
.
</
body
>
</
html
>
直接java application 运行, 在页面上即可,结果如下:
光是如此是远远不够的, 所以我们还要增加对java文件的支持(Servlet文件)。。。

