2 wtx0625 wtx0625 于 2014.12.15 14:34 提问

java getmethod() 问题求解

package demo;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**

  • 最简单的Servlet
  • @author Winter Lau
    */
    public class HelloServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    // res.getWriter().println("Hello World!");
    String methodname = req.getParameter("method").toString();
    System.out.println(methodname+",,,,"+req.getClass());
    Method method = null;
    try {
    method = this.getClass().getMethod(methodname,new Class[]{req.getClass(),res.getClass()});
    } catch (NoSuchMethodException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    method.invoke(this,req, res);
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    public void robotpage(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
    String url="/jsp/robot/robot.jsp";
    ServletContext sc = getServletContext();

    RequestDispatcher rd = sc.getRequestDispatcher(url);

    rd.forward(req, res);
    }

}

程序运行时method = this.getClass().getMethod(methodname,new Class[]{req.getClass会报错java.lang.NoSuchMethodException: demo.HelloServlet.robotpage(org.apache.catalina.connector.RequestFacade, org.apache.catalina.connector.ResponseFacade)
at java.lang.Class.getMethod(Unknown Source)

2个回答

devmiao
devmiao   Ds   Rxr 2014.12.15 14:46

反射的时候没有找到类,检查下你的配置文件

wtx0625
wtx0625 类肯定是能找到啊,要不然怎么会运行程序呢
接近 3 年之前 回复
leon709
leon709   2014.12.15 14:57

rebotpage()方法参数类型不匹配吧,所以说没有找到rebotpage()

wtx0625
wtx0625 参数类型应该怎么写呢?
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片