对servlet进行简单的封装,动态调用用户指定的方法,默认调用execute

对servlet进行简单的封装,动态调用用户指定的方法,默认调用execute ,现在只是个新手,欢迎老鸟能批评指点!
                    
                                            
  1. package org.doudouyota.utils;
  2. import java.io.IOException;
  3. import java.lang.reflect.Method;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.log4j.Logger;
  9. /**
  10. 切割双眼皮http://www.bj-swjtu.com/shypi/
  11. * 动态调用用户指定的方法,默认调用execute
  12. * @author YUANDONG
  13. * 2012-7-16
  14. */
  15. @SuppressWarnings("serial")
  16. public abstract class BaseServlet extends HttpServlet {
  17.         private Logger log = Logger.getLogger(BaseServlet.class);
  18.         @Override
  19.         public void doGet(HttpServletRequest req, HttpServletResponse resp)
  20.                         throws ServletException, IOException {
  21.                 doPost(req, resp);
  22.         }
  23.         @Override
  24.         public void doPost(HttpServletRequest req, HttpServletResponse resp)
  25.                         throws ServletException, IOException {
  26.                 String method = req.getParameter("method");
  27.                 if(method==null||method.trim().equals("")) {
  28.                         method = "execute";
  29.                 }
  30.                 log.debug("执行的方法为:"+method);
  31.                
  32.                 try {
  33.                         Method md = this.getClass().getMethod(method, HttpServletRequest.class,HttpServletResponse.class);
  34.                                 md.invoke(this, req,resp);
  35.                 } catch (Exception e) {
  36.                        
  37.                         log.debug("没有方法"+method);
  38.                         throw new RuntimeException(e.getMessage(),e);
  39.                 }
  40.                
  41.                
  42.         }
  43.        
  44.         public abstract void execute(HttpServletRequest req, HttpServletResponse resp)
  45.                         throws ServletException, IOException;
  46.        
  47. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值