request.getParameter("className") ->com.test.RevokeTask先通过className得到需要执行的类
再去执行这个类中的process方法,
如何写这个程序呢

解决方案 »

  1.   

    Class c = Class.forName("...");    // 获得类定义
    Object o = c.newInstance();        // 由类定义生成一个实例Object result = c.getMethod("process").invoke(o);  // 调用实例的指定方法
      

  2.   

    随便搞个例子,很简单的
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;public class ReflectionDemo extends Base
    {
        private int a = 100;    private String str = "test";    public int pi = 222;    public static final double PI = 3.1415;    public void setInt(int i)
        {
            this.a = i;
        }    private class InnerClass
        {
            private int i = 0;
        }    public void print(String ss)
        {
            System.out.println(ss);
        }    /**
         * @param args
         *            void
         */
        public static void main(String[] args)
        {
            ReflectionDemo demo = new ReflectionDemo();
            Base bd = new Base();
            Base br = new ReflectionDemo();
            System.out.println(demo.getClass().getName());
            System.out.println(demo.getClass());
            try
            {
                // 通过Class.forName()可以检查该类是否存在,并返回该类的Class对象
                Class bdc = Class.forName("classdemo.Base");
                Class brc = Class.forName("classdemo.ReflectionDemo");
                Class rc = Class.forName("classdemo.ReflectionDemo");
                demo = (ReflectionDemo) Class.forName("classdemo.ReflectionDemo")
                        .newInstance();
                // Annotation[] as = rc..getAnnotations();            System.out.println(brc.getDeclaredClasses()[0].getName());
                Method m = demo.getClass().getDeclaredMethod("print", new Class[]
                { "test method".getClass() });
                m.invoke(demo, new Object[]
                { "test method" });        }
            catch (ClassNotFoundException e)
            {
                e.toString();
                e.printStackTrace();
            }
            catch (InstantiationException e)
            {
                e.toString();
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.toString();
                e.printStackTrace();
            }
            catch (SecurityException e)
            {
                // 捕获异常:异常处理
                e.printStackTrace();
            }
            catch (NoSuchMethodException e)
            {
                // 捕获异常:异常处理
                e.printStackTrace();
            }
            catch (IllegalArgumentException e)
            {
                // 捕获异常:异常处理
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                // 捕获异常:异常处理
                e.printStackTrace();
            }        System.out.println(demo.pi);
        }}/**
     * 
     * <基类> <功能详细描述>
     */
    class Base
    {
        private int a1 = 100;    private String str1 = "test";    public int pi1 = 222;    public static final double PI1 = 3.1415;    public void setInt(int i)
        {
            this.a1 = i;
        }
    }
      

  3.   


    public class Test {
    public static void main(String[] args) {
    // char[] cs={'1','2','3','4'};
    // String str=new String(cs,1,2);
    // System.out.println(cs);
    try {
    Class cls = Class.forName("Test");
    Method[] methods = cls.getMethods(); 
    for (Method m : methods) {
    String name = m.getName();
    if ("process".equals(name)) {
    m.invoke(new Test(), null);
    return;
    }
    } } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } public void process() {
    System.out.println("process");
    }
    }
      

  4.   

    String className = request.getParameter("className");
    Class c = Class.forName(className);
    Method m = c.getDeclaredMethod("process",String.Class);//如果这你的process方法是  
                                                                      process(String s),
                                                              String.class代表参数类型
    m.invoke(className.getClass(),参数);
      

  5.   

    cls.getMethod("process",new Class[]{Integer.class, String.class}).invoke(o,new Object[]{1,""});public void process(Integer i, String b) {
    System.out.println("process");
    }