Servlet代码优化(详解)
package com.itheima.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.InvocationTargetException; import java.lang.reflect.Method; /* * 替换HttpService,根据请求的最后一段路径来进行方法分发 */ public class BaseServlet extends HttpServlet{ //根据请求的最后一段路径来进行方法分发 @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1. 获取请求路径 getRequestURI获取短路径 getRequestURL获取长路径 String uri = req.getRequestURI(); // /brand-case1/brand/selectAll // System.out.println(uri); //2. 获取最后一段路径:方法名 int index = uri.lastIndexOf("/");//从最后面往前找,找到最后一个"/"的位置,然后赋值给index String methodName = uri.substring(index + 1);// substring:从一个位置往后截取全部,注意"/"也会被获取所以要加1 // System.out.println(methodName); //3. 执行方法 //3.1获取BrandServlet / UserServlet 字节码对象 Class //谁调用我(this所在的方法),我(this)代表谁 // System.out.println(this); Class<? extends BaseServlet> cls = this.getClass();//因为this谁调用是谁,所以谁调用就是谁的class字节码对象 //3.2 获取方法method对象 try { // 使用调用的字节码对象 获取到字节码对象内部的的 HttpServletRequest.class 和 HttpServletResponse.class Method method = cls.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); //3.3 执行方法 // 执行的对象就是this,this就是获取到的字节码对象的this // method获取到的对象对应本方法内的req和resp,对应执行,执行对象为this method.invoke(this,req,resp); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
package com.itheima.web.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/brand/*") public class BrandServlet extends BaseServlet{ public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("brand selectAll..."); } public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ System.out.println("brand add..."); } }
package com.itheima.web.servlet; 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..."); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?