Solved

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

Posted on 2006-11-15
12
184 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JTable - RowFilter & Columns Header. 3 140
maven project import to eclipse problems 13 102
Struts tiles for layout structure along with Angular JS. 4 152
getting param value 7 51
Scenario: Your operations manager has discovered an anomaly in your security system. The business will start to suffer within 15 minutes if it is a major IT incident. What should she do? We have 6 recommendations for managing major incidents (https:…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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