wtx0625 2014-12-15 06:34 采纳率: 0%
浏览 2213

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 2014-12-15 06:46
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试