采坑-深入剖析Tomcat一书
程序员文章站
2022-07-14 10:36:39
...
采坑一、第一章 一个简单的web服务器
整体没碰到什么问题,按书写就可以了,但,最后一步卡死了,那个郁闷呀。核对了几遍都没错。最后才解决。
项目结构:
环境
jdk1.8,IDEA 。
需要自己额外引入这两个jar。
直接上代码先
HttpServer.java
package com.diy.tomcat.server;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer {
/**
* WEB_ROOT是HTML和其他文件所在的目录。对于这个包,WEB_ROOT是工作目录下的“webroot”目录。工作目录是文件系统中调用java命令的位置。
*/
public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot";
// 关机提示
private static final String SHUTDOWN_COMMEND = "/SHUTDOWN";
// 收到关机命令
private boolean shutdown = false;
public static void main(String[] args) {
HttpServer httpServer = new HttpServer();
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);
}
while (!shutdown) {
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
socket = serverSocket.accept();
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
// 创建请求对象并且解析
Request request = new Request(inputStream);
request.parse();
// 创建返回对象
Response response = new Response(outputStream);
response.setRequest(request);
response.sendStaticResource();
// 关闭socket
socket.close();
// 检查前面的URI是否是关闭命令
shutdown = request.getUri().equals(SHUTDOWN_COMMEND);
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
}
Request.java
package com.diy.tomcat.server;
import java.io.IOException;
import java.io.InputStream;
public class Request {
private InputStream inputStream;
private String uri;
public Request(InputStream inputStream) {
this.inputStream = inputStream;
}
/**
* 解析HTTP请求中的原始数据.
*/
public void parse() {
StringBuffer stringBuffer = new StringBuffer(2048);
int i;
byte[] buffer = new byte[2048];
try {
i = inputStream.read(buffer);
} catch (IOException e) {
e.printStackTrace();
i = -1;
}
for (int j = 0; j < i; j++) {
stringBuffer.append((char) buffer[j]);
}
System.out.println(stringBuffer.toString());
uri = parseUri(stringBuffer.toString());
}
/**
* 解析HTTP请求的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;
}
}
Response.java
package com.diy.tomcat.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Response {
private static final int BUFFER_SIZE = 1024;
Request request;
OutputStream outputStream;
public Response(OutputStream outputStream) {
this.outputStream = outputStream;
}
public void setRequest(Request request) {
this.request = request;
}
public void sendStaticResource() throws IOException {
byte[] bytes = new byte[BUFFER_SIZE];
FileInputStream fileInputStream = null;
try {
File file = new File(HttpServer.WEB_ROOT, request.getUri());
if (file.exists()) {
fileInputStream = new FileInputStream(file);
int ch = fileInputStream.read(bytes, 0, BUFFER_SIZE);
StringBuffer sb = new StringBuffer();
//响应头信息
sb.append("HTTP/1.1 200 OK\n");
sb.append("Content-Type: text/html;charset=UTF-8\n");
sb.append("\r\n");
while (ch != -1) {
sb.append(new String(bytes));
outputStream.write(sb.toString().getBytes());
ch = fileInputStream.read(bytes, 0, BUFFER_SIZE);
// outputStream.write(bytes, 0, ch);
}
} 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> File Not Found</h1>";
outputStream.write(errorMessage.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
// 如果不能实例化文件对象,则抛出
System.out.println(e.toString());
} finally {
if (fileInputStream != null) {
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
}
}
}
我碰到的坑在Response 里面。当按照书中说的写完之后,找不到文件可以返回,但把index文件放入指定的目录中后。
原文代码:
结果。
但
代码
对比发现区别在
这一部分。慢慢调试,最终
改为这样,没有这一行会一直循环
上一篇: 在apache服务器上部署vue项目
下一篇: S5PV210裸机之LCD