一、实现原理
1.基于ServerSocket网络编程,对外暴露服务。
2.用户在前端请求访问,ServerSocket监听建立的TCP请求后,通过其请求IO,反序列化获取访问url
3.根据拿到的URL,匹配对应的Handler处理类,将处理结果写入Socket 输出流中,最终返回给用户页面。
二、实现方案
1.创建MyHttpRequest类
public class MyHttpRequest implements Serializable {
//请求路径
private String url;
//请求方法
private String method;
//读取输入字节流,封装成字符串格式的请求内容
public MyHttpRequest(InputStream inputStream) throws IOException{
String httpRequest = "";
byte[] httpRequestBytes = new byte[1024];
int length = 0;
if((length = inputStream.read(httpRequestBytes)) > 0) {
httpRequest = new String(httpRequestBytes, 0, length);
}
//HTTP请求协议:首行的内容依次为:请求方法、请求路径以及请求协议及其对应版本号
// GET /index HTTP/1.1
String httpHead = httpRequest.split("\n")[0]; //取出HTTP请求协议的首行
System.out.println(httpHead);
method = httpHead.split("\\s")[0]; //按照空格进行分割,第一个是请求的方法
url = httpHead.split("\\s")[1]; //按照空格进行分割,第二个是请求的路径
System.out.println(this.toString());
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
@Override
public String toString() {
return "MyRequest [url=" + url + ", method=" + method + "]";
}
}
2.创建MyHttpResponse类
public class MyHttpResponse implements Serializable {
private OutputStream outputStream;
public MyHttpResponse(OutputStream outputStream) {
this.outputStream = outputStream;
}
//将文本转换为字节流
public void write(String content) throws IOException{
StringBuffer httpResponse = new StringBuffer();
httpResponse.append("HTTP/1.1 200 OK\n") //按照HTTP响应报文的格式写入
.append("Content-Type:text/html\n")
.append("\r\n")
.append("<html><head><link rel=\"icon\" href=\"data:;base64,=\"></head><body>")
.append(content) //将页面内容写入
.append("</body></html>");
outputStream.write(httpResponse.toString().getBytes()); //将文本转为字节流
outputStream.close();
}
}
3.创建请求处理器 RequestHandler
public class RequestHandler {
public void doService(MyHttpRequest myHttpRequest,MyHttpResponse myHttpResponse) throws IOException {
System.out.println("进入RequestHandler。。。。。");
myHttpResponse.write("RequestHandler success");
}
4.创建请求处理类 DefaultRequestHandler
public class DefaultRequestHandler {
public void doService(MyHttpRequest myHttpRequest,MyHttpResponse myHttpResponse) throws IOException {
System.out.println("进入DefaultRequestHandler。。。。。");
myHttpResponse.write("DefaultRequestHandler success");
}
}
5.创建 Tomcat类
public class MyTomcat {
void start() throws IOException {
try {
ServerSocket serverSocket = new ServerSocket(8081);
while (true) {
Socket socket = serverSocket.accept();
MyHttpResponse myHttpResponse = new MyHttpResponse(socket.getOutputStream());
MyHttpRequest myHttpRequest = new MyHttpRequest(socket.getInputStream());
String urL = myHttpRequest.getUrl();
if (urL.equals("/handle")) {
new RequestHandler().doService(myHttpRequest, myHttpResponse);
} else {
new DefaultRequestHandler().doService(myHttpRequest, myHttpResponse);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.创建启动类 MyTomcatApp
public class MyTomcatApp
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
try {
new MyTomcat().start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、测试结果
1、访问 http://localhost:8081/handle
2、访问 http://localhost:8081/handle2
3、IDEA后台打印