Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-15
12
Medium Priority
?
198 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
11 Comments
 
LVL 10

Accepted Solution

by:
jaggernat earned 800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
How to fix a SonicWall Gateway Anti-Virus firewall blocking automatic updates to apps like Windows, Adobe, Symantec, etc.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

783 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