
Service方法 提供的 doGet() 和 doPost() 方法只能根据请求方式进行分发,假设我们现在有多个服务(添加、删除、查询...),每个服务都要对应一个 Servlet(AddServlet、DeleteServlet、SelectServlet),这样管理起来是很不方便的,我们希望使用一个 Servlet 一个实体来处理其业务,比如 BrandServlet 处理 Brand 相关的业务(增删改查),UserServlet 处理 User 相关的业务

http://localhost/BrandServlet?method=add,然后通过 request.getParameter("method") 即可获取到 add 参数,再通过 if 进行判断,调用 add() 方法即可
- 方案二:重写 HttpServlet 的 Service 方法,自定义分发规则,直接通过访问路径进行业务处理BaseServlet,继承 HttpServlet,重写 Service 方法 ```java
package com.ruochen.web.servlet;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.lang.reflect.Method;/** * 替换 HttpServlet,根据请求的最后一段路径来进行方法分发
*/
public class BaseServlet extends HttpServlet { // 根据请求的最后一段路径来进行方法分发 @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1. 获取请求路径 "/brand-case/brand/selectAll" String uri = req.getRequestURI(); // 2. 获取最后一段路径,方法名 int index = uri.lastIndexOf("/"); String methodName = uri.substring(index + 1); // selectAll // 3. 执行方法 // 3.1 获取 BrandServlet / UserServlet 字节码对象 Class // 谁调用我(this 所在的方法),我(this)代表谁 // System.out.println(this); // BrandServlet Class<? extends BaseServlet> cls = this.getClass(); // 3.2 获取方法 Method 对象,执行方法 try { Method method = cls.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); method.invoke(this, req, resp); } catch (Exception e) { e.printStackTrace(); } }}```import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet("/user/*")public class UserServlet extends BaseServlet{ public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("user selectAll..."); } public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("user add..."); }}```/user/selectAll,可以看到控制台输出了 user selectAll...

/user/add,可以看到控制台输出了 user add...

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。