Solved

Dynamically load classes with servlets

Posted on 2003-12-11
6
218 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
ID: 9919499
 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
ID: 9919510
 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
ID: 9919512
yeah incorporate an instance, don't try to extend :-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9919978
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:Mayank S
ID: 11665346
Accept girinois' comment as answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
json example 39 130
Systems talking to each other 5 123
Java Filesystem manipulation of Word Docx file (find replace text) 3 63
arguments to jar 5 7
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

930 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

17 Experts available now in Live!

Get 1:1 Help Now