Solved

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

Posted on 2006-11-15
12
185 Views
Last Modified: 2010-04-01
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    
0
Comment
Question by:IanWood
  • 6
  • 3
  • 2
12 Comments
 
LVL 10

Accepted Solution

by:
jaggernat earned 200 total points
ID: 17947992
Store them in Properties file

and write a java class which can read the properties file.

use this code
http://javaalmanac.com/egs/java.util/Props.html
0
 

Author Comment

by:IanWood
ID: 17948268
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?  
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17948382
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

...
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:IanWood
ID: 17948878
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) {"
0
 

Author Comment

by:IanWood
ID: 17948916
Sorry, ignore that.  Had to import java.io

0
 

Author Comment

by:IanWood
ID: 17948979
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!!
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 17950214
>>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
0
 
LVL 14

Expert Comment

by:boonleng
ID: 17952082
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
0
 

Author Comment

by:IanWood
ID: 17955207
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.
0
 
LVL 14

Expert Comment

by:boonleng
ID: 17970631
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.
0
 

Author Comment

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

Cheers,
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

15 Experts available now in Live!

Get 1:1 Help Now