0
点赞
收藏
分享

微信扫一扫

how tomcat works 读书笔记(一)----------一个简单的web服务器


http协议

若是两个人能正常的说话交流,那么他们间必定有一套统一的语言规则<在网络上服务器与客户端能交流也依赖与一套规则,它就是我们说的http规则(超文本传输协议Hypertext transfer protocol)。


http分两部分,一个是请求(客户端发给服务器),一个是回复(服务器发给客户端)。


先看http请求 


下面就是一个http请求的例子,其中的各项参数,请查阅相关资料。

how tomcat works 读书笔记(一)----------一个简单的web服务器_源代码



http回复


下面就是http回复的例子,除了这个图之外,后面的部分就是大家看到的页面的源代码

how tomcat works 读书笔记(一)----------一个简单的web服务器_.重构_02

socket

我们一般说的socket,广义上包含java.net包下的Socket类与ServerSocekt类。


另一方面有基于tcp的网络编程也有基于udp的网络编程,其中差别大家百度,这里只谈tcp。


定义性的东西大家可以查看各种资料(个人推荐尚学堂 马士兵老师讲解的socket部分视频,但在看socket之前建议大家先看io部分),带大家看一段代码,大家应该就会知道socket编程的大致原理了。(代码来自马士兵老师的讲义)


import java.net.*;
import java.io.*;


public class TCPServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(6666); //在本机的TCP6666号端口上监听
while(true) {
Socket s = ss.accept(); //ServerSocket的accept为阻塞式方法,只有当它监听到一个请求时
//它才会执行
System.out.println("a client connect!");
DataInputStream dis = new DataInputStream(s.getInputStream());//获得客户端向自己说的"话"
//InputStream 从外部指向内存
System.out.println(dis.readUTF()); //按照uft-8的格式读取内容
dis.close();
s.close();
}

}
}


import java.net.*;
import java.io.*;


public class TCPClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1", 6666); //连接127.0.0.1(本机)的tcp端口6666
OutputStream os = s.getOutputStream(); //获得一条线路,来给服务器"说话"
DataOutputStream dos = new DataOutputStream(os);//对这个线路进行包装
Thread.sleep(3000); //"暂停"3秒
dos.writeUTF("hello server!"); //对服务器说 hello server!
dos.flush();
dos.close();
s.close();
}
}


先运行server端,再运行client端。当运行client端后,控制台首先会打印a client connect!三秒之后会打印hello server! 


模拟一个最最最基础的tomcat

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() {
ServerSocket serverSocket = null;
int port = 8080;
try {
serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
//这个1是什么功能 参加 http://www.51cto.com/art/200702/40196_1.htm
}
catch (IOException e) {
e.printStackTrace();
System.exit(1);
}


// Loop waiting for a request
while (!shutdown) { //最开始的时候 shutdown为false 这段话会执行
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try {
socket = serverSocket.accept(); //只有当有客户端请求时 它才会运行 阻塞式方法!
System.out.println(new Date()+"AAAAAAAAAAAA");
input = socket.getInputStream(); //里面放的是客户端对服务器说的话
output = socket.getOutputStream();//这里面将要放的是服务器要对客户端说的话


// create Request object and parse
Request request = new Request(input);
request.parse(); //参见request


// 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;
}
}
}
}


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() {
// Read a set of characters from the socket
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]); //将客户端的请求加到request(StringBuffer)中
}
System.out.println("request 如下");
System.out.print(request.toString());
uri = parseUri(request.toString());
System.out.println("uri 如下 "+uri);
}


/**
*看看System.out.println("uri 如下 "+uri); 就不用解释这个方法了
*
**/
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;
}


}



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


/*
HTTP Response = Status-Line
*(( general-header | response-header | entity-header ) CRLF)
CRLF
[ message-body ]
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
*/


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 {
System.out.println(HttpServer.WEB_ROOT+" **** "+request.getUri());
File file = new File(HttpServer.WEB_ROOT, request.getUri()); //连接用户请求的"文件"
if (file.exists()) {
fis = new FileInputStream(file);
int ch = fis.read(bytes, 0, BUFFER_SIZE); //把文件里的东西读出来放到bytes字符数组里
while (ch!=-1) { //把bytes数组里的东西放到要给客户端回复的流里面
output.write(bytes, 0, ch);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
}
else { //要是文件不存在 不解释
// file not found
String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: 23\r\n" +
"\r\n" +
"<h1。File Not Found</h1。";
output.write(errorMessage.getBytes());
}
}
catch (Exception e) {
// thrown if cannot instantiate a File object
System.out.println(e.toString() );
}
finally {
if (fis!=null)
fis.close();
}
}
}


首先如果大家用的是Eclipse,那么没有任何问题,如果大家是用命令行的形式的话会存在一个问题,HttpServer与Response两个类相互依赖,先编译谁?


解决办法 cd到三个类的目录 然后 javac *.java


等启动HttpServer后


在浏览器输入localhost:8080/index.html


显示如下 

how tomcat works 读书笔记(一)----------一个简单的web服务器_java_03


明白了吧 我们把index.html放到D:\尚学堂 j2ee\javase\尚学堂科技_马士兵_J2SE_5.0_第01章_JAVA简介_源代码_及重要说明\java\Socket\no\WebContent 目录下

index.html内容如下

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

我是index

</body>

</html>


再次请求 截图如下

how tomcat works 读书笔记(一)----------一个简单的web服务器_http协议_04




大家一定很奇怪,我为什么不用火狐或这个Chrome,找个EditPlus点过来点过去。


大家试试就知道了,火狐不知道因为什么原因,在地址栏敲回车后,会发出两次请求。结果就是报错。


再试试http://localhost:8080/SHUTDOWN

how tomcat works 读书笔记(一)----------一个简单的web服务器_源代码_05

程序退出


举报

相关推荐

0 条评论