This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I'm using Struts 1.1 and Oracle 10g and attempting to establish a connection between the two. I have successfully done so by hard-coding the datasource info in my action class but am running into a problem when placing that info in the struts-config.xml. Here's the data-source entry:
I call it with this:
I have determined that it is stumbling on the DataSource line, giving me a java.lang.NullPointerException. If you can see what's going wrong with the code, please let me know.
When I remove the try/catch block from the database access action class, it produces this:
It's thus still the DataSource line that's breaking. Any ideas?
Joined: Jul 13, 2006
Hi again, so I've narrowed it down a bit. Looks like the getDataSource is calling Action.class and it's stumbling on the following line:
I've commented all my db access code out and just entered that (and imported javax.servlet.ServletContext) and I get the same NullPointerException. Anybody know what's going on here?
Joined: Jul 13, 2006
So, I've in fact more or less solved the problem. The solution confuses me however, so if anyone can help me understand, I would very much appreciate it.
My jsp forwards to an action class which calls a class that does the database access. The database access class returns the result to the action class. The action class populates a bean with the results and sets a request attribute with the bean. Finally it forwards back to the jsp, which displays the results from the bean. As you can tell, it's a tiny web-app at this point, but I believe it is structured properly (if you have different information about where the db access should happen, please let me know, I really want this app to be structured to the standards).
The thing that I got hung up on was accessing the application scope attributes (from the ServletContext) in the database access method. I'll post it here:
As I said, I'm calling this class and its one method rather than forwarding to it. As such, I think it's not being passed the ServletContext unless I do it explicitly(as I do). This is what confuses me. I assumed that because the ServletContext was application scope that it would automatically be available in any class I called in my web-app. I guess that was a poor assumption. Can anyone explain what I'm missing here? There's some bit of theory that hasn't gotten through to me and I'd really appreciate it if someone could explain briefly.
I realize you've already spent a good deal of time on this, but my recommendation is to go a completely different route. I suggest you abandon the use of the Stuts datasource entirely.
My reasons for recommending this are:
1-It's deprecated in the latest release of Struts 2-It was included in Struts only as a "last resort" if your Application server doesn't have it's own connection pooling mechanism 3-For a long time, the authors of Struts have recommended you use your Application Server's connection pooling mechanism instead of the one in Struts
I'd suggest you look in your application server documentation for information on how to set up an Oracle Connection pool. You can then access the DataSource in any of your classes by doing a JNDI lookup.