对servlet进行简单的封装,动态调用用户指定的方法,默认调用execute ,现在只是个新手,欢迎老鸟能批评指点!
- package org.doudouyota.utils;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.log4j.Logger;
- /**
- 切割双眼皮http://www.bj-swjtu.com/shypi/
- * 动态调用用户指定的方法,默认调用execute
- * @author YUANDONG
- * 2012-7-16
- */
- @SuppressWarnings("serial")
- public abstract class BaseServlet extends HttpServlet {
- private Logger log = Logger.getLogger(BaseServlet.class);
- @Override
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- doPost(req, resp);
- }
- @Override
- public void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- String method = req.getParameter("method");
- if(method==null||method.trim().equals("")) {
- method = "execute";
- }
- log.debug("执行的方法为:"+method);
- try {
- Method md = this.getClass().getMethod(method, HttpServletRequest.class,HttpServletResponse.class);
- md.invoke(this, req,resp);
- } catch (Exception e) {
- log.debug("没有方法"+method);
- throw new RuntimeException(e.getMessage(),e);
- }
- }
- public abstract void execute(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException;
- }