工作中,有时候可能会直接使用到servlet,这样,和spring整合起来,就有点麻烦,还要写个spring的代理,比较麻烦,下面这个servlet,就不需要了。如果继承了这个servlet,那么,他的子类就会具有自动装配的功能了。代码如下:
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.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public abstract class AutowiredHttpServlet extends HttpServlet {
private static final long serialVersionUID = -2000909174467653847L;
public void init() throws ServletException {
super.init();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
AutowireCapableBeanFactory factory = ctx.getAutowireCapableBeanFactory();
factory.autowireBean(this);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
servlet(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
servlet(request, response);
}
/**
* 子类实现该方法,进行Servlet Post和Get请求服务。
* @param request HttpServletRequest实例
* @param response HttpServletResponse实例
* @throws IOException
* @throws ServletException
* @author yinlei
* date 2011-8-9 下午7:15:41
*/
public abstract void servlet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException;
/**
* 直接输出json。
*/
public void renderJson(HttpServletResponse response, String json) {
response(response, json, "application/json;charset=UTF-8");
}
/**
* 直接输出纯HTML。
*/
public void renderHtml(HttpServletResponse response, String text) {
response(response, text, "text/html;charset=UTF-8");
}
/**
* 直接输出纯字符串。
*/
public void renderText(HttpServletResponse response, String text) {
response(response, text, "text/plain;charset=UTF-8");
}
public void response(HttpServletResponse response, String text, String contentType) {
try {
response.setContentType(contentType);
response.getWriter().write(text);
} catch (IOException e) {
}
}
}
这样以来,他的子类,可以直接像struts2和spring mvc一样,直接将service bean都注入进来了。例如:
public class DemandServlet extends AutowiredHttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(DemandServlet.class);
@Autowired
private SupplyDemandService supplyDemandService;
@Autowired
private UserMemberService userMemberService;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
servlet(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
servlet(request, response);
}
@Override
public void servlet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String type = request.getParameter("type");
String userId = request.getParameter("userId");
if (type == null || type.equals("init")) {// 去发布,需要验证用户的信息
//去发布求购,验证用户信息是否完整
Member member = userMemberService.get(userId);
if (member == null || member.getSopenid() == null) {// 验证没有通过,让用户完善信息
request.getRequestDispatcher("weixin/binding.jsp").forward(request, response);
} else {// 已经验证通过,跳转到发布页面
request.getRequestDispatcher("weixin/demand.jsp").forward(request, response);
}
} else if (type.equals("publish")) {// 发布
//绑定数据
SupplyDemandInf demand = binder(request);
supplyDemandService.saveOrUpdate(demand);
request.getRequestDispatcher("weixin/demandPublishSuccess.jsp").forward(request, response);
}
}
}