Accessing initParam variables from a class

G

Guy Noir

My apologies if this is not the correct forum for this question:

I have a Tomcat server and I'm trying to develop an application that
accesses a SQL server from a java class.

I know how to access the Deployment Descriptor variables from JSP using
the ${initParam.varaible} method and then pass them to a bean or a java
class, but my question is this:

I want to create a java class that can get the
username/password/hostname for the sql DB from the deployment
descriptor without using a jsp/bean.

Is there a method that will allow me access the Deployment Descriptor
variables directly from a java class in my project?

Thanks much
-Aaron
 
R

Ross Bamford

Is there a method that will allow me access the Deployment Descriptor
variables directly from a java class in my project?

Check out:

public String ServletContext.getInitParameter(String name)

"Returns a String containing the value of the named context-wide
initialization parameter, or null if the parameter does not exist."

and:

String ServletConfig.getInitParameter(String name)

"Returns a String containing the value of the named initialization
parameter, or null if the parameter does not exist."

Depending on your need. Obviously you need access to either a Servlet
instance or the appropriate Context, which you get passed around as
you're aware. Which you'd use depends on your needs.

Descriptions above are from J2EE 1.4 Javadoc btw:

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletConfig.html

Cheers,
Ross
 
G

Guy Noir

Ok, well here is my problem and I'm sure I'm showing my ignorance of
struts:

I am writing an authentication module. Everything is working however I
am doing my best to:
A: Decouple business logic from weblogic
B: Make the resources such as the DB password and username easy to
change.

So, In my ActionForward or authentication object that the actionforward
calls, I need to have the dbusername and password available.

I would like the LoginAction or the authentication object to perform
this lookup to get the jdbc url, username, and password from the
initparam.

When trying to use the below methods from the actionforward or my
authentication object, I get a "nonstatic from a static" method error.

Again, I COULD pass these arguments easliy from a JSP page, but I'm
trying to follow the MVC as closely as possible and might learn a thing
or 2 from your responses. (So, thanks in advance)

I could also store the information in the
ApplictionResources.properties file.

So, I guess what I am asking is along the lines of best practice as far
as MVC and struts are concerned.

Thanks again for the excellent input.

-Aaron
 
W

Wendy Smoak

Guy Noir said:
So, In my ActionForward or authentication object that the actionforward
calls, I need to have the dbusername and password available.

I don't think ActionForwards are intended to have logic in them, but just to
'represent a destination'.

From an Action, however, you can call getServlet(), which returns an
ActionServlet, on which you can call getServletContext, then
getInitParameter. You might need a 'getServletConfig' in there somewhere.
Just click around the API docs until you piece it together.

For database user id and password, is there a reason you're not configuring
a DataSource?

I would not tie data access to the Servlet spec. I put my (non-JDBC)
database config in a plain old .properties file that sits in
WEB-INF/classes, and retrieve it with
classLoader.getResourceAsStream( filename );
 
G

Guy Noir

Good thoughts Wendy. Thanks for the tips. Just trying to wrap my head
around struts and I see your point on the action vs actionForward. I
probably muddled it, but the Action us where I am attempting this.

No, I have no good reason to NOT use datasource. That sounds like a
reasonable route. Just following this line of thought as I'm thinking
"OK..there maybe some other parameters I need access to".....

So, let's assume I have some other configuration data I need access
to.....

I understand the getServlet() from the action. What I have created is
an interface...lets call it authinterface and then implemented that in
an Authorization class. In the Action, I create an Authorization object
and pass it my parameters, username, password, dbusername, dbpassword,
dburl.

So:
Authorization auth = new Authentication();
boolean isAllowed = auth.authenticate(user,pass, dbuser,dbpass, host);

Something like that from the action.

Forgive my ignorance on this one, but when you say "I would not tie
data access to the Servlet spec." have I done that above? I assume
that's not best practice?

Thanks much for your comments and willingness to help out!!

-Aaron
 
W

Wendy Smoak

Guy Noir said:
I understand the getServlet() from the action. What I have created is
an interface...lets call it authinterface and then implemented that in
an Authorization class. In the Action, I create an Authorization object
and pass it my parameters, username, password, dbusername, dbpassword,
dburl.

I would have the Authentication object "know" how to talk to the database.
This part of the webapp doesn't even need to know that there *is* a
database, it only needs to ask, "Is this person who he says he is?" and "Can
this person see this page?"

If you configure a DataSource, your Authentication object can "look up" the
connection and not worry about how to connect. Then you just pass in the
userid/password pair your user typed in, and get back a response.
Forgive my ignorance on this one, but when you say "I would not tie
data access to the Servlet spec." have I done that above?

Yes, by putting the database userid and password in as init params, now your
auth code depends on things like 'ServletContext' in order to get those
values.

You're reinventing the wheel here... there are already authorization and
authentication schemes built into the Servlet spec. Are you sure they won't
work for you? If you haven't already, take a look at SRV.12 of the Servlet
2.3 Specification before you go too much further.
 
G

Guy Noir

Thanks much for the response. Agreed. As to using the built in
authentication, I am attempting to build a portal that will allow
instaneous access without modifying system files, so I'm MD5 hashing
passwords and storing them in the SQL backend.

So, I understand how to create the datasource. Now assume that I am
writing a Swing front end and moving to it instead of the web based,
how could I replicate that datasource object?

Sorry for the basic questions...I'm just trying to get up to speed with
the Java way after developing with perl and php for years.....

Thanks Much!!!
-Aaron
 
W

Wendy Smoak

Guy Noir said:
So, I understand how to create the datasource. Now assume that I am
writing a Swing front end and moving to it instead of the web based,
how could I replicate that datasource object?

Sorry for the basic questions...I'm just trying to get up to speed with
the Java way after developing with perl and php for years.....

You might want to start a new thread about that... I don't actually use
DataSource because I don't use JDBC to connect to my database. Or see if
this helps:
http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/GettingStartedTOC.fm.html
(Chapter 4 is about DataSource)
 
G

Guy Noir

Excellent. That's a piece of the JDBC I was never made aware of. I'm
assuming because all of the books that I read that include JDBC always
use URL's rather then a datasource. Excellent help and thanks again
Wendy!

-Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top