@WebServlet("/ReflectServlet") public class ReflectServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getParameter("method"); System.out.println(method); //Instantiate the Class class object, the servlet is instantiated at the time of request Class<?> clazz = this.getClass(); try { if (method == null) { method="toStart"; //return a method object Method means = clazz.getMethod(method,HttpServletRequest.class,HttpServletResponse.class); //Call the method, pass in the instance of the calling method, the parameter list means.invoke(this,req,resp); }else { //return a method object Method means = clazz.getMethod(method,HttpServletRequest.class,HttpServletResponse.class); //Call the method, pass in the instance of the calling method, the parameter list means.invoke(this,req,resp); } } catch (Exception e) { e.printStackTrace (); } } }
Some code about java reflection
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325979282&siteId=291194637
Recommended
Ranking