Part of the Code
public void Go (servletrequest request,servletresponse response) { String methodName = "index";//Method name string ClassName = "indexaction";//class name try{ class<?> action = Class.forName (className); Parameter list class<?>[] Parametertypes={servletrequest.class,servletresponse.class}; Get the construction method corresponding to the parameter constructor<?> constructor=action.getconstructor (parametertypes); Parameter object[] Parameters={request,response}; Get methods Method = Action.getmethod (methodName); The method above is for public method if the target is private method then use this kind of //method method = Action.getdeclaredmethod (methodName); Method.setaccessible (true); Method.invoke (constructor.newinstance (parameters)); } catch (Exception e) { e.printstacktrace (); }}
Java reflection creates a class with constructed parameters and executes methods