spring 整合servlet

import java.io.IOException;

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

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;




public class AppServlet extends HttpServlet {
	
    private IAppService appService;
    private StoreService storeService;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AppServlet() {
        super();
    }

    /**
	 * 处理get方式请求
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * 处理post方式请求
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO:解析request中的json
		String jsonStr = "{'ts': 1201226308500,'body':{'user_id':1,'size':5,'device_id':14}}";
		appService.getRecAppListByUser(jsonStr);
		storeService.getTest();
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		appService = (IAppService)getbean("appService");
		storeService = (StoreService)getbean("storeService");
	}
	
	private Object getbean(String str){
		WebApplicationContext wac =
			WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		return wac.getBean(str);
	}
}

你可能感兴趣的:(spring,servlet)