public class BaseServlet extends HttpServlet {
@SneakyThrows
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.获取请求的方法名称
String methodName = getMethodName(req);
System.out.println(methodName);
// 2.调用action
Object o = invocateAction(req, methodName, resp);
if (o != null) { //不为null,说明有返回值,且统一是字符串类型
String s = (String) o;
//判断前缀
if (s.startsWith(ConstUtils.REDIREC)) { //重定向
String content = s.split(":")[1];
resp.sendRedirect(req.getContextPath() + content);
} else if (s.startsWith(ConstUtils.FORWARD)) { //转发
String content = s.split(":")[1];
req.getRequestDispatcher(content).forward(req, resp);
} else { //返回字符串
resp.getWriter().write(s);
}
}//为null,此处不做处理,需要自己写跳转
}
private Object invocateAction(HttpServletRequest req, String methodName, HttpServletResponse response) {
// 1.找到需要调用的方法
Method method = null;
for (Method me : this.getClass().getMethods()) {
if (me.getName().equals(methodName)) {
method = me;
break;
}
}
// 2.判断是否找打了调用的action
if (method != null) {
// 准备方法的参数
Object[] args = methodParam(method, req, response);
try {
// 调用方法
Object invoke = method.invoke(this, args);
return invoke;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} else {
System.err.println("【" + methodName + "】没有定义");
}
return null;
}
private Object[] methodParam(Method method, HttpServletRequest req, HttpServletResponse response) {
// 1.创建一个数组,数组的长度就是方法的形参的个数
Object[] args = new Object[method.getParameterCount()];
// 2.获取调用方法传递的参数列表
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
String paramName = parameter.getName(); // 形参名称
Class<?> paramType = parameter.getType();
if (paramType.getSimpleName().equals(HttpServletRequest.class.getSimpleName())) {
args[i] = req;
} else if (paramType.getSimpleName().equals(HttpServletResponse.class.getSimpleName())) {
args[i] = response;
} else if (paramType.getSimpleName().equals(Integer.class.getSimpleName())) {
String parameter1 = req.getParameter(paramName);
if (parameter1 != null && !"".equals(parameter1)) {
Integer value = Integer.parseInt(parameter1);
args[i] = value;
}
} else {
String value = req.getParameter(paramName);
if (value == null || "".equals(value)) {
// 把这个参数当成对象来处理,
Map<String, String[]> parameterMap = req.getParameterMap();
// 把参数Map的数据拷贝到一个对象中
try {
Object obj = parameter.getType().newInstance(); // 实例化一个对象
BeanUtils.populate(obj, parameterMap); // 把map中的数据拷贝到对象中
args[i] = obj;
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
} else {
args[i] = value;
}
}
}
return args;
}
private String getMethodName(HttpServletRequest req) {
String requestURI = req.getRequestURI(); // sever
int index = req.getRequestURI().lastIndexOf("/");
return requestURI.substring(index + 1);
}
public String index(HttpServletRequest request, HttpServletResponse response) {
return ConstUtils.REDIREC + "index.jsp";
}
}
该方法封装了服务器获取参数的过程,最后写方法的时候只关注传递需要的参数即可,不再关注参数的获取过程,也不关注参数封装成为对象的过程