0

I'm working on a Java based web app which will consist of a couple of JSPs and some servlets. The JSPs and servlets all need to access a proprietary remote resource which is accessible by submitting a text based request over TCP (the response is also plain text).

To facilitate this, I have created a DAO style object with various getXbyId() style methods. Internally, the DAO maintains a SocketPool, that is, a collection of Sockets with synchronized get() and put() methods. Calling get() will cause the pool to grow if it is depleted (I should probably cap the size of the pool but I'm not there yet).

I have the above code working for a single servlet. Specifically, the servlet init() method instantiates the DAO object and stores it as a local object. My plan was to expand on this by putting a single instance of the DAO object into application scope (possibly enforced by making it a singleton). Every JSP and servlet would need to check for the existance of this object in the application scope and initialise it where required. JSPs would be able to use <jsp:useBean> while servlets would be need to do this programmatically (that is, fetch it from application context and instantiate it if the fetch returns null). The problem with the above scenario is that the DAO cannot meaningfully be initialised with a no-arg constructor. It needs arguments to specify the IP address, port etc. for the remote resource. These values are stored in a properties file which I load from the ServletContext (via getResourceAsStream).

The question then is, how should I best go about making a single instance of this DAO object available to all of the servlets and JSPs in my application without lots of repetitive and error prone boiler-plate initialisation code?

Thanks, Phil

PhilDin
  • 2,802
  • 4
  • 23
  • 38
  • 1
    possible duplicate of [Using init servlet](http://stackoverflow.com/q/3468150), [I want to use a method which is called only once after deploying to Server](http://stackoverflow.com/q/2364390), [How do I load a java class (not a servlet) when the tomcat server starts](http://stackoverflow.com/q/4175726) and many more. By the way, `` is dead. Do not use it. You shouldn't use DAOs directly in JSPs anyway. – BalusC Jan 11 '12 at 18:41
  • possible duplicate of [Using init servlet](http://stackoverflow.com/questions/3468150/using-init-servlet) – Tomasz Nurkiewicz Jan 11 '12 at 18:44
  • It looks like they all point to the same solution (using a ServletContextListener) which I think will also address my question. Thanks for your post. – PhilDin Jan 11 '12 at 18:53
  • Do you have Spring available to you to use? – Todd Murray Jan 11 '12 at 18:44
  • I've never used Spring, I'm not ruling it out but I was hoping that this is not something that would require a whole framework to resolve. – PhilDin Jan 11 '12 at 18:46
  • @BalusC, thanks for your input. It's a pretty simply app so I could probably get away with cutting some corners but OTOH, it would be nice to feel I was doing things the *right way*. Do you have any pointers on how JSPs and servlets should interact? – PhilDin Jan 11 '12 at 19:02
  • Correction, I meant to say "how JSPs and DAOs should interact". – PhilDin Jan 11 '12 at 19:10

1 Answers1

0

You are re-inventing all sorts of things here:

  • Parameters, specially data such as port number, ip address, etc. should be defined in using web.xml, unless you have very compelling reason not to do so. (?)

  • Given that you are (let me guess) trying to hook a noSQL backend to your web-app, ideally you should use JCA (Java Connector Architecture) mechanisms to isolate the driver. Throwing in blocking network calls and a socket pool (!) from the servlet directly can work, but all bets are off in terms of the guarantees that the Java EE-Web container provides you and I personally would never permit such an hack/app to end up in production servers. Ever.

  • Even if you insist on doing this, having a ServletContextListener map the life-cycle of your web-app with the life-cycle of the connector is the way to do this.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
alphazero
  • 27,094
  • 3
  • 30
  • 26
  • Thanks, the JCA point doesn't apply as it really is just a third party service using text over sockets (it predates noSQL by a long shot:) I'll take a look at the web.xml option though, I wasn't aware of this. – PhilDin Jan 11 '12 at 19:06
  • JCA is designed to integrate "Enterprise Information Systems". It has nothing to do with noSQL. It simply allows for a standard mechanism to integrate both passive *and* active external "resources", in your case a legacy 3rd party service. I just googled this http://www.theserverside.com/news/1364656/Serve-It-Up-with-J2EE-14-Use-JCA-15-and-EJB-21-to-Extend-your-App-Server It is non-trivial but it is the correct and robust approach. – alphazero Jan 11 '12 at 22:24
  • Ah, I understand. Thanks for the post, I'll look further into this. Cheers. – PhilDin Jan 12 '12 at 12:30