Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to dynamically call a class

Status
Not open for further replies.

Sethington

Technical User
May 16, 2006
34
US
What I'm doing is I have a website that sends a command property to a front controller servlet and then forwards onto a class. All that I want this class to do is get the command property and dynamically call the appropriate class and its method. For example if the command is "Login" then I want it to call the Login class and the "getLogin" method. I went searching around and here is what I have come up with so far but it throws a Class not found exception. Can anyone please help it is driving me crazy trying to figure this out. [banghead]

Here is my source:

code:



INTERFACE

import java.sql.Connection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.Branch

public interface ApplicationControllerInterface {

public HttpServletResponse getRule(HttpServletRequest request, HttpServletResponse response, Connection conn);

}



APP CONTROLLER CLASS

import com.BranchBCO;

public class HTBApplicationController implements ApplicationControllerInterface {

public static void requestBusinessControlObject(HttpServletRequest request, HttpServletResponse response, Connection conn) {

try {((ApplicationControllerInterface)(Class.forName((String)request.getParameter("cmd")+"BCO").newInstance())).doRequest(request, response, conn);
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (ClassNotFoundException e) {
}
}
 
Are you sure that the desired class(es) are on the classpath of your application? For example, if these classes are in a jar file, is that jar file in the lib directory under the WEB-INF directory of your web application?

Tim
 
Looks like you are not fully qualifying the class namespace.

So you are trying to instantiate "LoginBCO" rather than "mypackage.LoginBCO".

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I finally fixed my problem. I had 2 problems. Both of which was in my BranchBCO file. I didn't say that the class implements the interface and second I had is set up as public static and my interface was only public. This is why I was getting the type cast error.

Thanks for trying to help though
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top