Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need to dynamically instantiate a java class that extends Action Class(struts), and associate this instance to a specific url without use web.xml.

What I have tried:

i can't find a solution!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Posted
Updated 15-Jan-17 1:31am

1 solution

You need to use Reflection in Java for any kind of behavior manipulation of this type on runtime. You can try the following code and see if that works (since you did not show any effort, I will not give any code).
Java
Class<?> cls = Class.forName(name); // name holds the fully qualified name for type.
Object clsInstance = (Object) cls.newInstance();

You can then have the instance of the type. I used the following code to create a new instance of "String" type in Java,
try {
    // TODO code application logic here
    Class classtype = Class.forName("java.lang.String");
    Object instance = classtype.newInstance();
            
    if((String)instance != null) {
        System.out.println("Object created.");
    } else {
        System.out.println("Object not created -- or null.");
    }
} catch (ClassNotFoundException ex) {
    Logger.getLogger(HelloJava.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception er) {
    // Skipped.         
}
/* Output:
run:
Object created.
BUILD SUCCESSFUL (total time: 0 seconds)
 */

You can create any object, just pass the type of that object to create for Java runtime to build it and then get the instance of that object.

For more on reflection or the Class type in Java, please read: Class (Java Platform SE 7 )[^]
 
Share this answer
 
Comments
Hamza Halim 15-Jan-17 11:29am    
thank you, but what i need seriously is that instance must be associated to url without use web.xml, just with coding
Afzaal Ahmad Zeeshan 15-Jan-17 12:01pm    
Web.xml is not a standard, or requirement at all. Your question was unclear, now it is even more unclear. Please be specific in what you actually need.
Hamza Halim 16-Jan-17 4:36am    
i need to dynamically instantiate a java class that extends Action Class(struts), and associate this instance to a specific url without use struts-config.xml, to associate this url to that instance.

<action path="/threed1" type="DManager1" validate="false">


<action path="/threed2" type="DManager2" validate="false">

DManager1 and DManager2 in fact, are the same class. so what i want to do is to use just one class and create instances to treat urls(threed1, threed2, ..., threedN)
Afzaal Ahmad Zeeshan 16-Jan-17 6:54am    
So, did you try to do that?

The code that I shared will actually instantiate the object.
Hamza Halim 16-Jan-17 9:40am    
yes, it works, but how can i make this instance serve url one or url two ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900