简单手写Tomcat

public class TestServer {
	//获取项目的绝对路径
	private static String web_root = System.getProperty("user.dir") + "\\"
			+ "WebRoot";
	//资源名
	private static String url = "";
	//保存配置信息的map集合
	private static Map<String, String> map = new HashMap<String, String>();
	static {
		try {
			//加载配置信息
			Properties pro = new Properties();
			pro.load(new FileInputStream(web_root + "\\conf.properties"));
			Set set = pro.keySet();
			Iterator iterator = set.iterator();
			while (iterator.hasNext()) {
				String key = (String) iterator.next();
				String value = pro.getProperty(key);
				map.put(key, value);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) throws IOException,
			ClassNotFoundException, InstantiationException,
			IllegalAccessException {
		// 创建服务器的ServerSocket对象
		ServerSocket serverSocket = new ServerSocket(9090);
		while (true) {
			// 获取客户端的Socket对象
			Socket socket = serverSocket.accept();
			// 获取输入流
			InputStream is = socket.getInputStream();
			// 获取输出流
			OutputStream ops = socket.getOutputStream();
			// 获取请求中的资源名称,并赋值给url
			parset(is);
			// 判断请求的是静态,还是动态
			if (null != url) {
				if (url.indexOf(".") != -1) {
					// 发送静态资源
					sendStaticResource(ops);
				} else {
					// 发送动态资源
					sendDynamicResource(is, ops);
				}
			}

			if (ops != null) {
				ops.close();
				ops = null;
			}
			if (is != null) {
				is.close();
				is = null;
			}
			if (socket != null) {
				socket.close();
				socket = null;
			}
		}

	}
	// 发送动态资源
	private static void sendDynamicResource(InputStream is, OutputStream ops)
			throws IOException, ClassNotFoundException, InstantiationException,
			IllegalAccessException {
		// 发送响应头和响应行
		ops.write("HTTP/1.1 200 OK\n".getBytes());
		ops.write("Server:apache-Coyote/1.1\n".getBytes());
		ops.write("Content-Type:text/html;charset=utf-8\n".getBytes());
		ops.write("\n".getBytes());
		//在map中判断有没有对应的key
		if (map.containsKey(url)) {
			String value = map.get(url);
			//获取对应的Servlet对象
			Class clazz = Class.forName(value);
			Servlet ser = (Servlet) clazz.newInstance();
			//运行服务
			ser.init();
			ser.Service(is, ops);
		}
		url = null;
	}

	// 发送静态资源
	private static void sendStaticResource(OutputStream ops) throws IOException {
		// 用于存储资源的数组
		byte[] bytes = new byte[2048];
		// 资源的文件对象
		File file = new File(web_root, url);
		// 判断资源文件是否存在
		if (file.exists()) {
			// 发送响应头和响应行
			ops.write("HTTP/1.1 200 OK\n".getBytes());
			ops.write("Server:Apache\n".getBytes());
			ops.write("Content-Type:text/html;charset=utf-8\n".getBytes());
			ops.write("\n".getBytes());
			// 读取资源文件,并将其作为响应体发送
			FileInputStream fis = new FileInputStream(file);
			int len = fis.read(bytes);
			while (len != -1) {
				ops.write(bytes, 0, len);
				len = fis.read(bytes);
			}
			// 关闭资源
			if (null != fis) {
				fis.close();
				fis = null;
			}
		} else {
			// 发送响应头和响应行
			ops.write("HTTP/1.1 404 not found\n".getBytes());
			ops.write("Server:apache-Coyote/1.1\n".getBytes());
			ops.write("Content-Type:text/html;charset=utf-8\n".getBytes());
			ops.write("\n".getBytes());
			String errorMessage = "file not found";
			ops.write(errorMessage.getBytes());
		}
		url = null;
	}

	// 获取请求中的资源名称,并赋值给url
	private static void parset(InputStream is) throws IOException {
		// 存储请求信息的Buffer
		StringBuffer content = new StringBuffer(2048);
		byte[] buffer = new byte[2048];
		//利用输入流读取到请求信息
		int len = is.read(buffer);
		//将请求信息保存在Buffer中
		for (int i = 0; i < len; i++) {
			content.append((char) buffer[i]);
		}
		//将请求信息中的资源部分截取出来
		parseUrl(content.toString());
	}
	//将请求信息中的资源部分截取出来
	private static void parseUrl(String content) {
		int index1, index2;
		index1 = content.indexOf(" ");
		if (index1 != -1) {
			index2 = content.indexOf(" ", index1 + 1);
			if (index2 > index1) {
				url = content.substring(index1 + 2, index2);
			}
		}
		System.out.println(url);
	}
}

仅供大家参考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值