一个基于servlet极简的web框架实现

一个基于servlet极简的web框架实现,通过web.xml来做servlet mapping是痛苦的。简单写了一个web框架。

 




	 
		dispatcherServlet
		com.taobao.proxy.web.ProxyDispatcherServlet
		1
	
	
	
		dispatcherServlet
		/
	
	
	

 

package com.taobao.tlog.proxy.web;

import java.io.IOException;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 此Servlet是web.xml的入口,提供了简单实用的servlet mapping,避免写大量的xml
 * @author yunpeng
 *
 */
public class ProxyDispatcherServlet extends HttpServlet {

	public static final Logger logger = LoggerFactory.getLogger(ProxyDispatcherServlet.class);

	private static final long serialVersionUID = -156295649559639694L;

	protected void mapper() throws InstantiationException, IllegalAccessException {
		UrlMapping.mapping("^/jobadd$", JobAddServlet.class);
		UrlMapping.mapping("^/jobinfo$", JobInfoServlet.class);
		UrlMapping.mapping("^/jobstatus$", JobStatusServlet.class);
		UrlMapping.mapping("^/$", ProxyServlet.class);
	}

	public void init() throws ServletException {
		logger.info("tlog-proxy starting...");
		try {
			mapper();
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String path = request.getRequestURI();
		ProxyBaseServlet serviet = UrlMapping.find(path);
		if (serviet == null) {
			response.sendError(404);
			return;
		} else {
			serviet.doGet(request, response);
		}
	}
}

 

package com.taobao.tlog.proxy.web;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 一个极简的urlMapping
 * @author yunpeng
 *
 */
public class UrlMapping {

	private static Map urlPatterns = new HashMap();

	public static void mapping(String pattern, Class servlet)
			throws InstantiationException,
			IllegalAccessException {
		Pattern ptn = Pattern.compile(pattern);
		urlPatterns.put(ptn, servlet.newInstance());
	}

	public static ProxyBaseServlet find(String path) {
		for (Entry entry : urlPatterns.entrySet()) {
			Matcher mc = entry.getKey().matcher(path);
			if (mc.find()) {
				return entry.getValue();
			}
		}
		return null;
	}
}

 

 

import java.io.IOException;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taobao.hsf.tlog.proxy.web.core.ProxyBaseServlet;

public class ProxyServlet extends ProxyBaseServlet {

	private static final long serialVersionUID = -2124094625600853748L;

	public static final Logger logger = LoggerFactory.getLogger(ProxyServlet.class);

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		jsonMsg(false, "this is tlog poxy ", response);

	}
}

 

/**
 * 此Servlet是所有业务Servlet的基类,提供一些通用的方法
 * @author yunpeng
 *
 */
public abstract class ProxyBaseServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7353341318065451370L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	protected void jsonMsg(boolean success, Object data, HttpServletResponse response) throws IOException {
		response.setContentType("text/html");
		response.setStatus(HttpServletResponse.SC_OK);
		Map map = new HashMap();
		map.put("success", success);
		map.put("data", data);
		String result = new Gson().toJson(map);
		response.getWriter().write(result);
	}
}

 

你可能感兴趣的:(web)