JavaWeb - Servlet的抽取

2018-03-19  本文已影响0人  小闫94

刚开始学Servlet的时候,每个小功能都写一个具体的Servlet,比如

于是,开始向上抽取:把这一组跟用户注册相关的Servlet中的service方法,放到一个UserServlet中变成UserServlet的方法,并且增加一个请求参数method,然后在UserServlet的service方法中通过判断method调用不同的方法,类似下面这样:

public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        
        switch (method) {
        case "active":
            active(request, response);
            break;
        case "register":
            register(request, response);
            break;
        case "checkUsername":
            checkUsername(request, response);
            break;
        case "randomcode":
            randomcode(request, response);
            break;
        case "checkRandomcode":
            checkRandomcode(request, response);
            break;
        default:
            System.out.println("UserServlet-参数错误:"+method);
            response.sendRedirect("/register.jsp");
            break;
        }
    }
    
    public void checkUsername(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //do something...
    }
    
    public void checkRandomcode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //do something...   }
    
    public void register(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //do something...
    }
    public void active(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //do something...
    }
    
    public void randomcode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //do something...
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

这样就简洁多了,不再需要为每个小功能单独创建一个Servlet了。但是,这样做每个Servlet的service都要写上一堆的if-else判断语句,而且一旦一个Servlet中的方法过多,分支语句就会显得太过复杂冗余…

又于是,继续向上抽取:写一个BaseServlet,让它继承HttpServlet。让UserServlet继承BaseServlet,并删除UserServlet中的service方法,把分发功能抽取到BaseServlet中实现,利用反射通过request的method参数,执行不同的方法。

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;

public class BaseServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    // UserServlet或其他BaseServlet的子类中没有service方法,其对象会调用父类BaseServlet的service方法
    // 这里的this表示当前对象,即具体的UserServlet的对象,或者是其他BaseServlet的子类的对象
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        
        try {
            // 1、获得请求的method名称
            String method = req.getParameter("method");
            // 2、获得“当前“被访问对象的字节码对象
            Class<? extends BaseServlet> clazz = this.getClass();
            // 3、获得当前字节码对象中的指定的方法
            Method method2 = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            // 4、执行相应功能方法
            method2.invoke(this,req, resp);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

public class UserServlet extends BaseServlet {
    private static final long serialVersionUID = 1L;
//不再需要
/*  public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        
        switch (method) {
        case "active":
            active(request, response);
            break;
        case "register":
            register(request, response);
            break;
        case "checkUsername":
            checkUsername(request, response);
            break;
        case "randomcode":
            randomcode(request, response);
            break;
        case "checkRandomcode":
            checkRandomcode(request, response);
            break;
        default:
            System.out.println("UserServlet-参数错误:"+method);
            response.sendRedirect("/register.jsp");
            break;
        }
    }*/
    
    public void checkUsername(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //do something...
    }
    
    public void checkRandomcode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //do something...   }
    
    public void register(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //do something...
    }
    public void active(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //do something...
    }
    
    public void randomcode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //do something...
    }
    
    
/* 不再需要
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }*/

}
//直接写具体的方法代码即可...
//需要注意的一点是,因为通过反射调用,方法修饰符必须用public,否则没有权限调用
    public void method(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //do something...
    }


至此,代码又清爽了许多...

上一篇 下一篇

猜你喜欢

热点阅读