其他分享
首页 > 其他分享> > 2020.05.14 BaseServlet servlet的优化和分类

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();
}


}
}

标签:BaseServlet,14,HttpServletResponse,2020.05,URI,method,import,servlet,javax
来源: https://www.cnblogs.com/aojie/p/12891598.html