Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to instantiate a common resource in a JSP/Servlet app?
    primarykey
    data
    text
    <p>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).</p> <p>To facilitate this, I have created a DAO style object with various <code>getXbyId()</code> style methods. Internally, the DAO maintains a <code>SocketPool</code>, that is, a collection of Sockets with synchronized <code>get()</code> and <code>put()</code> methods. Calling <code>get()</code> 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). </p> <p>I have the above code working for a single servlet. Specifically, the servlet <code>init()</code> 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 <code>&lt;jsp:useBean&gt;</code> 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).</p> <p>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? </p> <p>Thanks, Phil</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload