Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:53:26+00:00 2026-05-28T01:53:26+00:00

I’m working on a Java based web app which will consist of a couple

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:53:27+00:00Added an answer on May 28, 2026 at 1:53 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.