BaseServlet请求分发通用

package com.po;
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.Method;

public class BaseServlet extends HttpServlet {

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
* 1.获取参数,用来识别用户想请求的方法
* 2.然后判断是哪一个方法,是哪一个我们就调用哪一个方法
*/
String methodName = req.getParameter("method");

if (methodName == null || methodName.trim().isEmpty()) {
throw new RuntimeException("您没有传递method参数!无法确定您想调用的方法。");
}
/*
* 得到方法名称,是否可通过反射来调用方法?
* 1.得到方法名,通过方法名在得到Method类的对象!
* >需要得到Class,然后调用它的方法进行查询!得到Method
* >我们要查询的是当前类的方法,所以我们需要得到当前类的Class
*/
Class c = this.getClass();//得到当前类的class对象
//查询方法-->参数:1.方法名 2.两个需要用到的对象的类
Method method = null;
try {
method = c.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);//方法名,参数
} catch (Exception e) {
throw new RuntimeException("您要调用的方法:" + methodName + ",它不存在。");
}
}
}
posted @ 2022-04-08 11:06  旧人12  阅读(28)  评论(1编辑  收藏  举报