美文网首页
JavaWeb - Servlet的抽取

JavaWeb - Servlet的抽取

作者: 小闫94 | 来源:发表于2018-03-19 16:48 被阅读0次

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

  • CheckUsernameServlet
  • CheckRandomcodeServlet
  • GetRandomCodeServlet
  • ActiveServlet
  • RegisterServlet
  • ...
    这样下来,一个注册功能就要写N多个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参数,执行不同的方法。

  • BaseServlet
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();
        }
    }

}

  • UserServlet
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);
    }*/

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


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

相关文章

  • JavaWeb - Servlet的抽取

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

  • javaee 02 servlet

    servlet 是javaWeb 三大组件之一(Servlet,Filter,Listener)servlet 的...

  • 4.0.1 JSP

    JavaWeb早期 :applet + servlet,其中servlet中需要大量的response.getWr...

  • JavaWeb知识点笔记

    1.1 Servlet 1.1.1 什么是Servlet Servlet是JavaWeb三大组件之一(Servle...

  • JavaWeb—Servlet基础(细节版,相当细节)

    JavaWeb—Servlet基础(细节版,相当细节) ​ 什么是Servlet? Servlet就是一个普通的类...

  • IntelliJ 中JavaWeb的简单使用

    Intellijidea建javaWeb以及Servlet简单实现

  • JavaWeb (day9)

    1.什么是 Servlet? Servlet 是 javaweb 的三大组件之一,它属于动态资源。Servlet ...

  • 3.0.4 Servlet

    Servlet是JavaWeb三大组件 servlet 是运行在web服务器的小型Java程序。 servlet通...

  • Servlet

    1、什么是Servlet? Servlet是javaweb的三大组件之一,它属于动态资源,servlet的作用是处...

  • Servlet

    一.Servlet概述 Servlet是JavaWeb的三大组件之一,它属于动态资源。三大组件:Servlet,f...

网友评论

      本文标题:JavaWeb - Servlet的抽取

      本文链接:https://www.haomeiwen.com/subject/qqepqftx.html