• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Accessing DBCP in RequestProcessor class

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am doing struts application where i creating customized Action servlet by extending RequestProcessor. I want to access the list of countries from datbase and make them avaliable to every entry form. I am using DBCP.
HOw to get Database connection in Customized Action servlet before client makes first request and make them avaliable to every enrey form
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd suggest you do this by creating a class that implements the org.apache.struts.action.Plugin interface and putting your code in the init() method. You would then register the plugin in your struts-config.xml file just as you would the validator plugin or any other plugin. If you do this, your code will run when the Struts ActionServlet initializes.

In your code, I would first get the datasource with the following code:

Once you have the collection, I'd suggest putting it in application context so that it is available to all other Action classes and JSPs.

Warning: The DBCP has been deprecated and is no longer availble in version 1.3.5 of Struts. It might be a good idea to get away from DBCP and move to a DataSource stored in JNDI.
[ February 01, 2007: Message edited by: Merrill Higginson ]
 
Satyajeet Kadam
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the soltion.
Currently i am configuring datasource in struts-config.xml.

Q1)ActionServlet is not bind to Struts-config.xml?
after action servlet is fully intilalized then it get access to struts-congig.xml.so from where it gets access to datasource?

q2)How and where to specfiy jndi ?



<data-sources>
<data-source type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<set-property property="driverClassName" value="org.gjt.mm.mysql.Driver" />
<set-property property="url" value="jdbc:mysql://localhost:3306/silicon" />
<set-property property="username" value="root" />
<set-property property="password" value="" />

</data-source>
</data-sources>
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the first things the Struts ActionServlet does when it initializes is to read the struts-config.xml file. It then follows the instructions in the config file and creates objects as needed. If you've specified a DataSource in your config file, the ActionServlet will create it and put it in Application scope. After it is finished with all its configuration tasks, the ActionServlet will call the init method on any plugin classes that are registered in the config file. So, by the time it calls your init method, everything has been initialized.

In answer to your second question: Java Naming and Directory Interfact (JNDI) is a part of the J2EE specification and is supported by every application server. It's considered best practice to register a DataSource in JNDI. Each application server has its own interface for setting up JNDI Datasources. Search for JNDI or DataSource in your App server's documentation and you'll find it.

This link shows how to do it in Tomcat 5.
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Warning: The DBCP has been deprecated and is no longer availble in version 1.3.5 of Struts. It might be a good idea to get away from DBCP and move to a DataSource stored in JNDI.


Yeah...thats true..DBCP has been deprecated in version 1.3.5 of Struts.

create context.xml file under your META-INF directory ...


enjoy....

 
My, my, aren't you a big fella. Here, have a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic