2020.05.14 BaseServlet servlet的优化和分类

Posted aojie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
}


}
}

以上是关于2020.05.14 BaseServlet servlet的优化和分类的主要内容,如果未能解决你的问题,请参考以下文章

BaseServlet

关于BaseServlet那些事

BaseServlet 继承 httpServlet

BaseServlet方法分发

javaWeb实例之抽取BaseServlet

BaseServlet????????????