将servlet定义为springBean的方法

需要完成一个比较晓得web服务端提供给外部系统调用,又不想用比较复杂web层框架(struts),只是采用servlet即可,不过每定义一个servlet都需要在web.xml里面写入配置,不方便管理,并且servlet和spring的交互感觉不是那么的河蟹……

 

这里自己想到了一个办法,将所有的http请求都转发到springBean里面,只需要定义一个servlet即可:

 

 

首先需要定义一个接口叫servlet处理 器

import java.io.IOException;

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


public interface HttpHandler {

	/**
	 * 处理 http请求
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void requestHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException;

}

 

 

 

 

servlet  class

 

 

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

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

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ServletDispacher extends HttpServlet {

	private static final long serialVersionUID = -4397593743538525329L;
	private Map<String, HttpHandler> handlers = new HashMap<String, HttpHandler>();
	Logger log = Logger.getLogger(this.getClass());

	@SuppressWarnings("unchecked")
	@Override
	public void init(ServletConfig config) throws ServletException {
		ServletContext context = config.getServletContext();
		WebApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		handlers = appContext.getBeansOfType(HttpHandler.class);
		for (HttpHandler handler : handlers.values()) {
			if (handler instanceof ServletBean) {
				ServletBean bean = (ServletBean) handler;
				bean.init(appContext);
			}
		}
		super.init(config);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		String beanName = request.getPathInfo();
		HttpHandler handler = handlers.get(beanName);
		if (handler == null) {
			String wrongInfo = "cant't find servlet bean name " + beanName;
			log.error(wrongInfo);
			throw new RuntimeException(wrongInfo);
		}
		if (handler instanceof ServletBean) {
			ServletBean bean = (ServletBean) handler;
			bean.printLog(request);
		}
		handler.requestHandler(request, response);
	}

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

 

servlet 在web.xml的配置:

 

	<servlet>
		<servlet-name>remote</servlet-name>
		<servlet-class>com.cqcis.ecsc.app.servlet.ServletDispacher</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>remote</servlet-name>
		<url-pattern>/remote/*</url-pattern>
	</servlet-mapping>

 

 

servletBean(这个是我自己定义的名字)的java代码

 

 

package com.cqcis.ecsc.app.servlet;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;

public class ServletBean implements HttpHandler {
	
@SuppressWarnings("unchecked")
	public void requestHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {


// 这里和普通的servlet一样的写

	}
}

 

 

最后把servletBean 配置为springBean

 

 

	<bean name="/testDatabase" class="com.cqcis.ecsc.app.bank.TestServ" ></bean>

 

最后我们可以根据路径/remote/testDatabase 来访问我们定义的servletBean

 

 

 

这样带来两点好处

1.再也不需要关心web.xml里面定义大量servlet的问题了,我们只需要在spring配置文件里面定义新的bean就可以打到新增servlet的目的

2 servlet里面引用普通springBean也变得容易了,直接按照一般规则注入就行了

你可能感兴趣的:(servlet)