Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Dynamically load classes with servlets

Posted on 2003-12-11
6
221 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

766 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