Link to home
Start Free TrialLog in
Avatar of IanWood
IanWoodFlag for United States of America

asked on

Solution required to store server/db connection parameters for use in a JSP.

Hi

I've got a jsp app.  It connects to an oracle db via JDBC. My problem is how to store the server and database name somewhere, so the code is portable? At the minute, the db name is entered by the users on sign-in and the db server is hardcoded.  Not ideal!!

Any ideas?  After random googling, I think I might  be able to 1. use web.xml file, or 2. JNDI. or 3. A properties file.

Are any of thes on the right lines and if so which would be the best way of doing it?

Some sample code or link to a guide would be great.

Cheers

Ian    
ASKER CERTIFIED SOLUTION
Avatar of jaggernat
jaggernat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IanWood

ASKER

Thanks for that.

Excuse my slowness, but I still don't get how to create the file.   There was no values added or listed in the example as far as i can see.

Can i create a properties file manually to reference from the application?  
Avatar of jaggernat
jaggernat

Can i create a properties file manually to reference from the application?  

Yes you can.

from above link , this is your properties file name    "filename.properties"

and it would contain soemthing like

JNDI_NAME = yourjndiName

...
Avatar of IanWood

ASKER

Cheers.

I'm not having any joy yet:-

"Generated servlet error:
C:\jakarta-tomcat-4.0\work\localhost\SL\SLEdit$jsp.java:93: Class org.apache.jsp.FileInputStream not found.
                        properties.load(new FileInputStream("filename.properties"));
                                            ^
An error occured between lines: 30 and 49 in the jsp file: /SLEdit.jsp

Generated servlet error:
C:\jakarta-tomcat-4.0\work\localhost\SL\SLEdit$jsp.java:94: Class org.apache.jsp.IOException not found.
                    } catch (IOException e) {"
Avatar of IanWood

ASKER

Sorry, ignore that.  Had to import java.io

Avatar of IanWood

ASKER

It's compiling now, but haven't managed to read anything from the file.

What do i need to do to get te values in properties?

I've got my file in WEB-INF\lib.  is that OK?

Sorry to be a pain!!
>>I've got my file in WEB-INF\lib.  is that OK?

NO

remember , lib contains only jar files
create a package and put the properties file in that package.

create a java class and put all the contants and properties file in that java class like this

package com.aoc.utils;
public static final String PROPERTIES_FILE               = "filename.properties";

and then from your java class read the properties file using above code
You can setup a datasource which contain the db info at the server and retrieve the db connection using jndi.
http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html
Avatar of IanWood

ASKER

Thanks jaggernat.

I'm not too great at java (no sh*t sherlock!!) and am still a bit confused.  i wouldn't know how to make the java class (I only use limited java in jsps, and that's mostly just to run the required SQL).  or how to then get the information onto my jsp page from the class.  

Can you provide a complete working example?

Apologies for being slow, and thanks for your patience.   I don't do much jsp programming and find all the catalina_home, jar files, web-inf, web.xml etc etc, pretty confusing.

boonleng/anyone, I'm even more confused now.  What's the advanatges of doing it that way?  Which is easiest for a novice?

I'll have a little play with both suggestions this afternoon, see if i can get any further.
Setting up the datasource at the server side allows the database connections to be managed by the server instead of your application. Changing datasource is much easier as you don't require to redeploy the application.

It also provide some level of security as users won't be able to view the db connection info such username and password. Only the server administrator have the access right to view the info. (This doesn't apply to tomcat as the configuration stored in xml file) Enterprise server such as Websphere or Weblogic even provide JAAS feature for db connection.

If you are develping a product base application which will deploy on many servers, then storing the db info in property file will make the deployment much easier as you don't require to configure the server, just place the war file and thats it.
Avatar of IanWood

ASKER

I've kind of sussed it now.  Thanks for your advice  boonleng, but i'm going to give jaggernat the points.

Cheers,