java - How to display output in servlet when invoking a method dynamically -



java - How to display output in servlet when invoking a method dynamically -

i trying dynamically load class , invoke 1 of methods servlet.

in servlet have next code :

printwriter out = response.getwriter(); seek { class<?> obj = class.forname(myclassname); method method = obj.getclass().getmethod(mymethodname); string returnvalue = (string) method.invoke(obj, null); out.println(returnvalue); } catch(exception e){}

and in class have :

public class studentclass { public string index() { homecoming "this studentclass"; } }

the problem when run application not display anything. expecting this studentclass output , index method of class returning.

could please tell me how solve problem?

your invoke usage wrong:

class<?> obj = class.forname(myclassname); // homecoming class, not instance method method = obj.getclass().getmethod(mymethodname); string returnvalue = (string) method.invoke(obj, null);

proper utilize like:

class<?> clazz = class.forname(myclassname); object obj = clazz.newinstance(); // give studentclass instance method method = clazz.getmethod(mymethodname); string returnvalue = (string) method.invoke(obj);

see class.forname(string) , method.invoke(object, object...) , this tutorial on reflection api.

java servlets reflection

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -