Solved

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

Posted on 2006-11-15
12
183 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

22 Experts available now in Live!

Get 1:1 Help Now