java nio http服务器(3)简单的Servlet容器

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/maosijunzi/article/details/37766723

Servlet容器中放着我们所有要访问的Servlet,根据我们具体的请求来访问不同的Servlet。下面是一个简单的Servlet容器的实现。request和response的封装以及连接器的实现代码这里就不再重复写了,请看上一篇博客。

servlet

package com.hcserver.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends  HttpServlet{

	private static final long serialVersionUID = 1L;

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String aa = req.getParameter("aa");
		String bb = req.getParameter("bb");
		System.out.println("param aa = "+aa);
		System.out.println("param bb = "+bb);
		
		resp.getWriter().print("hello world...");
	}

	
	
}


servlet容器

package com.hcserver.container;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;

public class Container {
	public final static String WEB_ROOT = Container.class.getResource("/webapps").getPath();
	
	private Map<String,Map<String,HttpServlet>> projectMap  = new HashMap<>();
	
	private static Container container = new Container();
	
	private Container(){}
	public static Container getInstance(){
		return container;
	}
	
	public void init(){
		try{
			//模拟解析web.xml,利用反射得到servlet实例
			Map<String,HttpServlet> map = new HashMap<>();
			Class<?> servletClass = Class.forName("com.hcserver.servlet.MyServlet");
			HttpServlet newInstance = (HttpServlet)servletClass.getConstructor(new Class[]{}).newInstance(new Object[]{});
			map.put("myServlet", newInstance);
			projectMap.put("hello",map);
			
		}catch(Exception e){
			
		}
	}
	
	public Map<String,HttpServlet> getProject(String projectName){
		return projectMap.get(projectName);
		
	}
	
	public static void main(String[] args) {
		container.init();
		
	}
	
	
}


处理器修改

package com.hcserver.process;

import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hcserver.container.Container;

public class Process {
	
	public void service(HttpServletRequest request,HttpServletResponse response){
		String project = request.getContextPath();
		String servletPath = request.getServletPath();
		
		//得到容器类
		Container container = Container.getInstance();
		Map<String, HttpServlet> servletMap = container.getProject(project);
		HttpServlet servlet = servletMap.get(servletPath);
		try {
			//返回页面
			servlet.service(request, response);
		} catch (Exception e) {
			
			e.printStackTrace();
		} 
	}
	
}


最后启动服务,浏览器,输入:http://localhost:8084/hello/MySevlet?aa=1&bb=3回车后浏览器显示 hello world..

后台输出

param aa = 1

param bb = 3

 

展开阅读全文

没有更多推荐了,返回首页