简单实现
public class BootStrap{
public static void main(String[] args){
ServiceSocket server = new ServerSocket();
server.bind(new InetSocketAddress(8888));
System.out.println("服务器启动:8888");
while(true){
Socket accept = server.accept();
InputStream inputStream = accept.getInputStream();
byte[] buffer = new byte[2048];
int len = inputStream.read(buffer);
System.out.println(new String(buffer,0,len));
String html = "<h1>xxx</h1>"
String response = "http/1.1 200 ok\r\n"
+ "content-Type: text/html\r\n"
+ "content-Length: "+ html.getBytes().length + "\r\n"
+ "\r\n"
+ html ;
OutputStream outputStream = accept.getOutputStream();
outputStream.write(response.getBytes());
}
}
}
改进
public class Request{
private String requestType;
private String url;
private String protocolType;
private Map<String,String> headers;
private String requestBody;
}
public class RequestHandler{
public static Request requestHandler(Socket socket) throws IOException{
StringBuilder sb = new StringBuilder();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = inputSTream.read(bytes);
sb.append(new String(bytes,0,len));
String[] headerAndBody = sb.toString().split("\r\n\r\n");
String[] split = headerAndBody[0].split("\r\n");
String[] typeSplit = split[0].split(" ");
Request request = new Request();
request.setRequestType(typeSplit[0]);
request.setUrl(typeSplit[1]);
request.setProtocolType(typeSplit[2]);
Map<String,String> headers = new HashMap<>();
for(int i=1;i<split.length();i++){
if(split[i]==null||"".equals(split(i))){
break;
}
String[] others = split[i].split(": ");
headers.put(others[0],others[1]);
}
request.setHeaders(headers);
if(headerAndBody.length==2){
request.setRequestBody(headerAndBody[1]);
}
return request;
}
}
public class Response{
private final String CRLF = "\r\n";
private StringBuilder responseLine = new StringBuilder();
private StringBuilder responseHeader = new StringBuilder();
private StringBuilder responseBody = new StringBuilder();
private String content;
private void buildResponseLine(int code){
responseLine.append("HTTP/1.1").append(" ").append(code).append(" ");
switch(code){
case 404:
responseLine.append("NOT FOUND");
break;
case 505:
responseLine.append("SEVER ERROR");
break;
case 200:
responseLine.append("OK");
break
}
responseLine.append(CRLF);
}
private void buildResponseHeader(String contentType){
responseHeader.append("Content-type:").append(contentType).append(";charset=utf-8").append(CRLF);
responseHeader.append("Content-Length:").append(content.getBytes().length()).append(CRLF);
responseHeader.append(CRLF);
}
private void buildResponseBody(){
responseBody.append(content);
}
private String build(int code,String content,String contentType){
setContent(content);
buildResponseLine(code);
buildResponseHeader(contentType);
buildResponseBody();
return responseLine.append(responseHeader).append(responseBody).toString();
}
private void setContent(String content){
this.content = content;
}
}
public class BootStrap{
public static void main(String[] args){
ServiceSocket server = new ServerSocket();
server.bind(new InetSocketAddress(8888));
System.out.println("服务器启动:8888");
while(true){
Socket accept = server.accept();
Request request = RequestHandler.requestHandler(accept);
System.out.println(request.getUrl());
if(request.getUrl().equals("/home")){
String html = "<h1>home</h1>"
String response = "http/1.1 200 ok\r\n"
+ "content-Type: text/html\r\n"
+ "content-Length: "+ html.getBytes().length + "\r\n"
+ "\r\n"
+ html ;
OutputStream outputStream = accept.getOutputStream();
outputStream.write(response.getBytes());
}else if(request.getUrl().equals("/page")){
String html = "<h1>page</h1>"
Response response = new Response();
String str = response.build(200,html,"text/html");
OutputStream outputStream = accept.getOutputStream();
outputStream.write(str.getBytes());
}else{
InputStream resourceAsStream = Bootstrap.class.getClassLoader().getResourceAsStream("/page/index.html");
byte[] buf = new byte[1024];
StringBuilder html = "";
int len;
while((len=resourceAsStream.read(buf))!=-1){
html.append(new String(buf,0,len));
}
Response response = new Response();
String str = response.build(200,html,"text/html");
OutputStream outputStream = accept.getOutputStream();
outputStream.write(response.getBytes());
}
}
}
}
改进(分发)
public class BootStrap{
public static void main(String[] args){
ServiceSocket server = new ServerSocket();
server.bind(new InetSocketAddress(8888));
System.out.println("服务器启动:8888");
while(true){
Socket accept = server.accept();
Request request = RequestHandler.requestHandler(accept);
Response response = RequestDispatcher.dispatch(request);
}
}
}
public class RequestDispatch{
public static Response dispatch(Request request){
String url = request.getUrl();
return null;
}
}