views:

143

answers:

1

I'm working on a website in JSP (in GWT really, but on the server side, it's really just JSP), and I need to configure my database.

I know HOW to code in the database connection etc, but i'm wondering how/where the database config should be saved.

To clarify my doubt, let me give an example; in PHP, a website usualy has a config.php, where the user configures the database, user, etc (or an install.php generates it).

However, since JSP is bytecode, I can't code this info into my site and have the user modify it, nor can I modify it analogously to an install.php.

How should I handle this? what's the best/most common practice ? I've found NO examples of this. Mainly, where should the config file be stored?

+1  A: 

There are several possibilities to do this, what I've seen done include:

  • Having database credentials in a special file, usually db.properties or some simple XML file that contain the required information (driver, url, username, password, any ORM parameters if needed). The properties file would be placed under WEB-INF or WEB-INF/classes; the downside of this approach is that the user would have to modify the file inside the WAR before deploying it to the application server.
  • Acquire the database connection via JNDI and expect it to be provided by the application server. This seems to be the most common way of doing this; on the upside, your WAR doesn't have to be changed, however, the downside is that configuring a JNDI data source is different for every application server and may be confusing if your system administrators are not experienced with Java technology.
andri
Just to be sure, since i'm a bit of a n00b at this still, WEB-INF is COMPLETELY inaccessible to outsiders right? - Placing a db.properties file generated by an install.jsp doesn't seem too far of in this case :)
Hugo
Yes, it's inaccessible to outsiders. Cite out of servlet 2.5 spec: No file contained in the WEB-INF directory maybe served directly to a client by the container.
andri
Perfect, thanks! I'm guessing that's the best way to go! BTW: I'm planning to have a user interface for modifying the config inside the WAR, so no manual changes should be necessary.
Hugo