Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Access context parameters from Bean.  RSS feed

 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to access the Context parameter set by the web.xml from a java bean or simple java class. For example, If I have database connection details in my web.xml and I want to get these in a Java Bean of a class which will pick up the values and create a Connection object and return to the guy calling it, either JSP or servlet.
I have a idea of using InitialContext class. But I doubt it works for Context parameter defined in the web.xml.
It worked if I use like this for DataSource object which is created in my application server.
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("OMSDataSource");
return ds.DatabaseConnection.getConnection();
So how can I access a parameter "Webmaster" defined in web.xml like this..
<context-param>
<param-name>Webmaster</param-name>
<param-value>webmaster@mycorp.com</param-value>
</context-param>
Cheers.
 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
doesn't ServletContext.getInitParameter(String)
do the job??
[ March 10, 2003: Message edited by: Asher Tarnopolski ]
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good idea or another alternative is that you can do something similiar to placing your information in a properties file and pass the url of the properties file as a parameter value. Something similar to the struts framework. Which by the way is something you may want to look into.

Craig
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
In both cases, its either passing the context object from my servlet to bean. ( feeding my bean with the context object ) or opening a file on the server in the second case.
But I wanted to know is there anyway I can access the context parameters directly like the DataSource.
BeanContext class has any role in this??
Cheers.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know there are at least 2 ways of accessing the parameters of the servlet context directly.
One way as
Asher
pointed out is
through the ServletContext interface or you can gain access to the ServletContext object through
the getServletContext() method of the ServletConfig interface,which makes up the signature of the init(ServletConfig config) method.
Other than that, I may not fully understand your
question. But if that is not the answer you are
looking for, you may want to look into JNDI as a way of binding you're datasource.
cj
 
L Goundalkar
Ranch Hand
Posts: 395
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi CJ,
Well my requirement is simple, let me give the details.
I have a bean which reads the database details from a properties file on the server and helps the other servlets to get Connection object.
Now I am putting these details in web.xml, so when my application is up, it will be there in the context.
Its easy for me to access the same from servlets, but i dont want to change all the servlets, instead I will change the bean which is readng the file. I will make the bean to read the database details like username, server and so on from the context instead of file.
I thought of using DataSource, which is perfectly possible. But I dont want to use DataSource object. I want to get the connection details from this bean and let my servelts create and destroy the Connection object.
Is there anyway I can read the context parameters from a bean ?
Hope this explains my requirement.
Thanks for the time.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anyway I can read the context parameters from a bean ?

What it all comes down to is that there is no way on earth that your bean can get access to the servlet context out of the ozone. You need to pass the servlet context to the bean.
One pattern I've seen used is to "attach" a bean to a page context (from which you can get just about any info you want regarding the servlet environment) by passing the page context to the bean which stores it as an instance variable of the bean.
Another is to simply pass the context as a parameter to whichever method needs it.
hth,
bear
[ March 12, 2003: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!