Servlet代码优化(详解)

发布时间 2023-07-05 14:51:59作者: Karlshell

 

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...");
    }
}