调用servlet类中的方法

时间:2012-08-06 19:24:58

标签: java servlets methods invoke

我有这个servlet -

@WebServlet("/CreateNewPersonServlet")
public class CreateNewPersonServlet extends HttpServlet {
      private void saveInDB() {

            // here use the invoke ...
            String methodName = "saveManager";
            Method method = CreateNewPersonServlet.class.getMethod(
                    methodName, new Class[] {});
            method.invoke(this);

      }
      private void saveManager() {

      }

}

当跑步到达线 -

Method method = CreateNewPersonServlet.class.getMethod(
                        methodName, new Class[] {});

它抛出异常 -

java.lang.NoSuchMethodException: control.CreateNewPersonServlet.saveManager()
    at java.lang.Class.getMethod(Unknown Source)

我应该如何正确编写调用?

1 个答案:

答案 0 :(得分:4)

该方法是私有的,您应该使用.getDeclaredMethod(..),然后使用setAccessible(true)

.getMethod(..)仅返回公共方法。但你也可以公开这个方法。

相关问题