资讯

展开

tomcat探究一搭建简单的web容器

作者:快盘下载 人气:

tomcat探究一搭建简单的web容器

说明代码HttpServerRequestResponse 请求测试

说明

这里的web程序只是一个静态资源的web程序
HttpServer作为web入口,监听一个端口,作为web的入口
Request对请求进行处理,拿到http请求报文,获取第一行,拿到请求资源路径
Response对响应进行处理,返回静态资源或者错误信息

代码

HttpServer

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpServer {
	//静态资源的路径
	public  static final String WEB_ROOT = ;D:tomcat_servletstatic;;
	//关闭命令
	private static final String SHUTDOWN_COMMAND = ;/SHUTDOWN;;
	//是否关闭socket
	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);
		} catch (IOException  e) {
			e.printStackTrace();
		}
		
		while(!shutdown) {
			Socket socket = null;
			InputStream in = null;
			OutputStream out = null;
			
			try {
				socket = serverSocket.accept();
				in = socket.getInputStream();
				out = socket.getOutputStream();
				
				Request request = new Request(in);
				request.parse();
				if(request.getUri() == null) {
					socket.close();
					continue;
				}
				
				Response response = new Response(out, request);
				
				response.sendStaticResource();
				socket.close();
				
				shutdown = request.getUri().equals(SHUTDOWN_COMMAND);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

Request

import java.io.InputStream;

public class Request {
	private InputStream in;
	private String uri = null;
	private static final int BUFFER_SIZE = 2048;
	
	public Request (InputStream in){
		this.in = in;
	}
	
	public void parse() {
		StringBuffer request = new StringBuffer(BUFFER_SIZE);
		
		int i = 0;
		byte[] buffer = new byte[BUFFER_SIZE];
		
		try {
			i = in.read(buffer);
		} catch (Exception e) {
			e.printStackTrace();
			i = -1;
		}
		
		for(int j = 0;j < i;j;;) {
			request.append((char)buffer[j]);
		}
		
		System.out.println(request.toString());
		
		uri = parseUri(request.toString());
	}
	
	
	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

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 out;
	
	public Response(OutputStream out,Request request) {
		this.out = out;
		this.request = request;
	}
	
	public void sendStaticResource() {
		byte[] bytes = new byte[BUFFER_SIZE];
		
		FileInputStream fin = null;
		
		try {
			File file = new File(HttpServer.WEB_ROOT ; File.separator ; request.getUri());
			if(file.exists()) {
				fin = new FileInputStream(file);
				String pre = ;HTTP/1.1 200 ok

; ;  
				out.write(pre.getBytes());
				
				int ch = fin.read(bytes, 0, BUFFER_SIZE);
				while(ch != -1) {
					out.write(bytes,0,ch);
					ch = fin.read(bytes, 0, BUFFER_SIZE);
				}
				out.flush();
			}else {
				/**
				 * 文件不存在
				 */
				String errorMessage = ;HTTP/1.1 404 File Not Found
;
						; ;Content-type: text/html
;
						; ;Content-Length: 23
;
						;;
;
						;;<h1>File Not Found</h1>;;
				
				out.write(errorMessage.getBytes());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(fin != null) {
				try {
					fin.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

请求测试

http://localhost:8080/hello.txt

加载全部内容

相关教程
猜你喜欢
用户评论
快盘暂不提供评论功能!