码迷,mamicode.com
首页 > 其他好文 > 详细

2020.05.14 BaseServlet servlet的优化和分类

时间:2020-05-14 22:21:00      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:err   soft   substring   cti   protected   优化   etc   set   int   

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的优化和分类

标签:err   soft   substring   cti   protected   优化   etc   set   int   

原文地址:https://www.cnblogs.com/aojie/p/12891598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!