部分代码
public void go(ServletRequest request,ServletResponse response){ String methodName = "index";//方法名 String className = "indexAction";//类名 try{ Class> action = Class.forName(className); //参数列表 Class>[] parameterTypes={ServletRequest.class,ServletResponse.class}; //获取参数对应的构造方法 Constructor> constructor=action.getConstructor(parameterTypes); //参数 Object[] parameters={request,response}; //获取方法 Method method = action.getMethod(methodName); //上面的方法针对public method 若目标是private method 则使用此种 //Method method = action.getDeclaredMethod(methodName); //method.setAccessible(true); method.invoke(constructor.newInstance(parameters)); } catch(Exception e){ e.printStackTrace(); } }