Solved

Dynamically load classes with servlets

Posted on 2003-12-11
6
217 Views
Last Modified: 2013-11-24
Hi all,
A variable is based to my servlet and a lookup is preformed in the database. Depending on what this variable is a class is loaded. I've written an abstract to handle all this. When I was testing I didn't use a servlet initially but instead wrote a main and tested on the command line to make sure the lookup and creation was working. This test file began with:
public class TestUser extends DecisionCreator
      {
      public static void main(String args[])
            {
etc...

Where DecisonCreator is:
public abstract class DecisionCreator
{
                protected static Decision createDecision(String name) throws DecisionNotCreatedException
      {
      Decision myDecision;
      try
            {
            Class DecisionClass = Class.forName(name);
            myDecision = (Decision) DecisionClass.newInstance();
            }
                //omitted catches
      return myDecision;
      }
Decision is a simple interface:
public interface Decision
      {            
      String[] launch(String user);
      }
When I went to put use my code in the servlet I came up against this problem. My servlet is called Link:
public class Link extends HttpServlet
I cannot extend DecisionCreator as I did in my test file. Is there a solution to this or do I have to re-design everything?
If anything is not clear please let me know

Thanks,
Shane
0
Comment
Question by:fast74
  • 2
  • 2
6 Comments
 
LVL 35

Expert Comment

by:girionis
Comment Utility
 Call the TestUser class from your Servlet and let it (the TestUser) do the loading of the classes.
0
 
LVL 35

Accepted Solution

by:
girionis earned 250 total points
Comment Utility
 For instance:

MyServlet extends HttpServlet
{
  public void doPost(...)
  {
      if (value read from db == something)
      {
           TestUser tu = new TestUser();
           tu.loadClass(something);
           ...
      }
  }
}
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
yeah incorporate an instance, don't try to extend :-)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
If you want to do it without TestUser, then do you necessarily need to make the DecisionCreater class as abstract? If not, then you can use its instance....
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Accept girinois' comment as answer.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now