2020.05.14 BaseServlet servlet的优化和分类

package cn.itcast.travel.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
* @author aojie
* @fuction
* @create 2020-05-14 20:51
*/
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//完成方法分发
//1.获取请求路径
String URI = req.getRequestURI();
System.out.println(URI);
//2.获取方法名称
String methodName = URI.substring(URI.lastIndexOf("/") + 1);
System.out.println(methodName);
//3.获取方法对象method
try {
//忽略访问权限修饰符
Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
//4.执行方法
//暴力反射
//method.setAccessible(true);
method.invoke(this,req,resp);
} catch (Exception e) {
e.printStackTrace();
}


}
}
上一篇:Http与HttpServelRequest,HttpServletResponse


下一篇:HttpServletResponse