[英]invoke a method in a servlet class
我有这个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)
我应该如何正确编写调用?
该方法是私有的,您应该使用.getDeclaredMethod(..)
,然后使用setAccessible(true)
.getMethod(..)
仅返回公共方法。 但是您也可以将该方法公开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.