aspose file tools*
The moose likes Struts and the fly likes Accessing  DBCP in RequestProcessor class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing  DBCP in RequestProcessor class" Watch "Accessing  DBCP in RequestProcessor class" New topic
Author

Accessing DBCP in RequestProcessor class

Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
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
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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 ]

Merrill
Consultant, Sima Solutions
Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
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

Joined: Feb 15, 2005
Posts: 4864
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.
Milan Jagatiya
Ranch Hand

Joined: Jan 01, 2007
Posts: 164

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....


Milan.<br />I can because I think I can...
 
 
subject: Accessing DBCP in RequestProcessor class