• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

How do I employ a "deployment descriptor" for a Java business class in both stand-alone and servlet context

I am attempting to build my first Java application, with an initial goal of making it a web app.  I have followed the MVC convention and placed all of the business logic in a separate class (the Model).  I am unclear how to create a configuration file for this Model class for things like datasource information.  In my research it has been very clear how configuration works for servlets (a deployment descriptor: web.xml).  What isn't clear is how I can utilize some similar configuration scheme for the Model class, such that it will work when deployed as part of a servlet app but also as a class included by a non-web Java app (such as the test harness I built for the Model class). It seems I could use web.xml for the model class when it is part of a servlet, but then how would it work in a standalone app? The ideal solution won't make me use two different methods for using the class in a servlet vs in a standalone app.
1 Solution
Most people put configuration information (for non-Web apps) into a properties file, sometimes named myapp.properties.  It's in the name=value format, so it can easily be read using the methods of the Properties class.  You can put myapp.properties in the same directory as your class files, or in your .jar file (often in the top directory, but location varies).

Then, in your web.xml file, you could put the location of the myapp.properties file, which is then used to read the properties for your model class.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now