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)