反射替代各种else if (servlet)
@WebServlet("/order") public class OrderServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取提交的参数 String method = request.getParameter("method"); //判断参数,调⽤不同的⽅法 if ("add".equals(method)) { add(request, response); } else if ("delete".equals(method)) { delete(request, response); } else if ("update".equals(method)) { update(request, response); } else if ("findAll".equals(method)) { findAll(request, response); } else if ("findByPage".equals(method)) { findByPage(request, response); } } //添加订单功能 public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //删除订单功能(指订单的状态发⽣改变,数据库中并未删除) public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //更新订单功能 public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //查找所有订单功能 public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //分⻚查找的订单功能 public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
反射技术减少重复性代码(用了反射,性能可能会降低 )
@WebServlet("/order") public class OrderServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //获取提交的参数 String md = request.getParameter("method"); //反射,调⽤⽅法 Class clazz = this.getClass(); //获取⽅法 Method method = clazz.getMethod(md, HttpServletRequest.class, HttpServletResponse.class); //执⾏⽅法 method.invoke(this, request, response); } catch (Exception e) { e.printStackTrace(); } } //添加订单功能 public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //删除订单功能(指订单的状态发⽣改变,数据库中并未删除) public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //更新订单功能 public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //查找所有订单功能 public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //分⻚查找的订单功能 public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
把反射的代码提取到base,代码就更简洁了
public abstract class baseServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //获取提交的参数 String md = request.getParameter("method"); //反射,调⽤⽅法 Class clazz = this.getClass(); //获取⽅法 Method method = clazz.getMethod(md, HttpServletRequest.class, HttpServletResponse.class); //执⾏⽅法 method.invoke(this, request, response); } catch (Exception e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)