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 8441099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:28:38+00:00 2026-06-10T08:28:38+00:00

I am working on a web application using java servlet and here is my

  • 0

I am working on a web application using java servlet and here is my scenario:

servlet1: Has the application's interface and main logic
servlet2: Suplies values to servlet1 on ajax request from servlet1

Totally there are 250 values that servlet1 can request. But calculating these values in servlet2 takes time(As it involves making GET request to other servers to get values). So calculating these values on request makes the client to wait for a long time.

So is there a way to make servlet2 to start pre-calculating the values when servlet1 is first called(so it can quickly send the values on request)?
Any help on how this can be implemented?

P.S. database or filesystem cannot be used.

  • 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-06-10T08:28:39+00:00Added an answer on June 10, 2026 at 8:28 am

    If the data that servlet2 needs to precalculate is not dependant on servlet1 input and needs to be calculated only once, either calculate it eagerly in ServletContextListener.contextInitialized() or in GenericServlet.init(). However it the computation takes some time, you’ll better move it to some background thread to avoid long application startup time (these methods block deployment until they are finished).

    Here is a simple example:

    public class Servlet2 extends HttpServlet {
    
        private final ExecutorService threadPool = Executors.newSingleThreadExecutor();
        private Future<String> calculationResult;
    
        @Override
        public void init() throws ServletException {
            calculationResult = threadPool.submit(new PreComputingTask());
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            final String slowResponse = calculationResult.get();
            //...
        }
    }
    
    class PreComputingTask implements Callable<String> {
    
        @Override
        public String call() throws Exception {
            //Call external systems, whatever...
            return "slow response";
        }
    }
    

    As you can see, when servlet2 starts, it initiates pre computation task in a separate thread. Then in doGet() you retrieve the result, possibly waiting for it, if it hasn’t finished yet.

    If the pre-computation is dependant on input in servlet1 (e.g. request parameters of servlet1 need to be used when calling external system), it’s much more challenging and interesting.

    I see at least two options:

    • start the Future task in servlet1 and retrieve that future (hopefully already finished) in servlet2. You need to pass it somehow between the servlets, e.g. by placing it in ServletContext

    • send a message to jms queue from servlet1. Message listener will process the request, precomputing the results and putting the result in some temporary, unique queue. servlet2 can then later receive message from that queue (you’ll have to agree on some naming scheme somehow), or wait for it a little bit.

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

Sidebar

Related Questions

we are currently working on a new web application using Java and MySql. We
I am working on a web application using ASP.NET 3.5. The application has hundreds
I am currently working on a JAVA web application using JSF, Facelets, JSTL and
I am working on web application and using GAE/J blobstore tutorial http://code.google.com/appengine/docs/java/blobstore/overview.html I was
I'm working on a web application written in Java using the Tapestry 5.1.0.5 framework.
I have a decent working web application (Java/Servlet/Jsp) that I would like to improve
I'm using Java for a web application, and I'm working with a MySql database.
I'm working on development of a social web application using Java. I need to
I'm working with a company that produces a web application using struts with Java.
i am working in a web application using java language.i have a requirement to

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.