HTTP(超文本传输协议)允许Web服务器和浏览器通过Internet发送并接收数据,是一种基于“请求-响应”的协议。HTTP使用可靠的TCP链接,TCP协议默认端口为80端口;
Java中Socket类实现了TCP协议,使用Socket进行编写简易Web服务器:包含类(request,response,HttpServer)
Rquest类 :
Reponse 类
Java中Socket类实现了TCP协议,使用Socket进行编写简易Web服务器:包含类(request,response,HttpServer)
Rquest类 :
public class Request {
private InputStream input;
private String uri;
public Request(InputStream input) {
this.input = input;
}
//接收请求
public void parse() {
StringBuffer request = new StringBuffer();
int i;
byte[] buffer = new byte[2048];
try {
i = input.read(buffer);
} catch (Exception e) {
i = -1;
}
for (int j = 0; j < i; j++) {
request.append((char) buffer[j]);
}
System.out.println(request.toString());
uri = parse(request.toString());//解析请求
}
//
解析请求
private String parse(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;
}
}
Reponse 类
public class Response {
private static final int BUFFER_SIZE = 1024;
Request request;
OutputStream output;
public Response(OutputStream output) {
this.output = output;
}
public void serRequest(Request request) {
this.request = request;
}
public void sendStaticResource() throws IOException {
byte[] bytes = new byte[BUFFER_SIZE];
FileInputStream fis = null;
try {
File file = new File(HttpServer1.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\r\n" + "Content-Type:text/html\r\n"
+ "Content-length:23\r\n" + "\r\n<h1>404 File Not Found</h1>";
output.write(errorMessage.getBytes());
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (fis != null) {
fis.close();
}
}
}
}
HttpServer类
public class HttpServer1 {
// public static final String WEB_ROOT = System.getProperty("user.der") +
// File.separator + "webroot";
public static final String WEB_ROOT = new File("").getAbsolutePath() + File.separator + "webroot";
private static final String SHUDOWN_COMMAND = "/SHUTDOWN";
private boolean shutdown = false;
public void await() {
ServerSocket serverSocket = null;
int port = 8080;
try {
serverSocket = new ServerSocket(port);
} catch (Exception e) {
e.printStackTrace();
}
while (!shutdown) {
Socket socket = null;
InputStream input = null;
OutputStream output = null;
try {
socket = serverSocket.accept();// 等待请求,返回socket实例
input = socket.getInputStream();
output = socket.getOutputStream();
Request request = new Request(input);
request.parse();
Response response = new Response(output);
response.serRequest(request);
response.sendStaticResource();
socket.close();
shutdown = request.getUri().equals(SHUDOWN_COMMAND);
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
public static void main(String[] args) {
HttpServer server = new HttpServer();
server.await();
}
}
静态页面,在工程目录下新建一个webroot文件夹,并新建index.html文件
在浏览器输入 localhost:8080/index.html
控制台输出:
输入:http://localhost:8080/other.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Hello Tomcat</h1>
<h2>走近Tomcat之一个简单Web服务器</h2>
</body>
</html>
运行HttpServer:
在浏览器输入 localhost:8080/index.html
控制台输出:
GET /index.html HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
输入:http://localhost:8080/other.html
控制台内容
GET /other.html HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
输入 localhost:8080 /SHUTDOWN
控制台输出
Host: localhost:8080
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
输入 localhost:8080 /SHUTDOWN
控制台输出
GET /SHUTDOWN HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
关闭程序