为什么当我使用Java反射时IDEA会出错,但是Eclipse没有

时间:2018-12-08 09:52:48

标签: java servlets reflection

我尝试使用JAVA反射完成Servlet演示。 但提示错误。 这些提示仅出现在IDEA中, 日食正常,没有任何提示。

提示: enter image description here

这样的代码:

package...
import...

    public class ServletDemo02 extends HttpServlet {
        private static final long serialVersionUID = 1L;
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }

        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String md=request.getParameter("method");

            String path=null;

            Class clazz = this.getClass();
            try {

                Method method=clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
                if(null!=method){
                    //
                    path=(String)method.invoke(this, request,response);
                }
                if(null!=path){

                    request.getRequestDispatcher(path).forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }



        }

        public String addStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("add");
            return "/test.html";

        }
        public String delStu...
        public String delStu...

1 个答案:

答案 0 :(得分:0)

getMethodClass的声明是

Method getMethod(String name, Class<?>... parameterTypes)

parameterTypes是一个变量,至少需要Java5。再次检查是否在您的IDE中激活了Java 5。