欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

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

来源: javaer 分享于  点击 34554 次 点评:230

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


对servlet进行简单的封装,动态调用用户指定的方法,默认调用execute ,现在只是个新手,欢迎老鸟能批评指点!

BaseServlet.java

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;/** * 动态调用用户指定的方法,默认调用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;}
相关栏目:

用户点评