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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:37:51+00:00 2026-05-18T05:37:51+00:00

I would like to recursively call a servlet from itself until my operation completes.

  • 0

I would like to recursively call a servlet from itself until my operation completes. The reason I need to do this is because my hosting service has a hard deadline that must be met for all RPC calls. Therefore, I need to break up the operation in managable chunks and call the servlet recursively for each chunk until the operation completes.

I feel like the following code should work, but it is not calling the servlet when the HttpURLConnection is opened and connected. No errors are being thrown either. Any help would be greatly appreciated.

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class ServletExample extends HttpServlet {

HttpServletRequest request;

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    request = req;

    String strRequestIndex = request.getParameter("requestIndex");
    int intRequestIndex = 0;

    if (strRequestIndex != null) {
        intRequestIndex = Integer.parseInt(strRequestIndex);
    }

    if (intRequestIndex < 10) {
        try {
            callSameServlet(intRequestIndex);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

private void callSameServlet(int requestIndex) throws Exception {

    URL recursiveUrl = new URL(request.getRequestURL().toString()
            + "?requestIndex=" + (requestIndex + 1));

    HttpURLConnection connection = (HttpURLConnection) recursiveUrl
            .openConnection();

    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.connect();

    DataOutputStream dos = new DataOutputStream(
            connection.getOutputStream());

    dos.flush();
    dos.close();

}

}
  • 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-18T05:37:52+00:00Added an answer on May 18, 2026 at 5:37 am

    As to the technical problem as exposed in your question: the URLConnection is lazily executed. The URLConnection#getOutputStream() in your code only returns a handle to write the request body. You need to use URLConnection#getInputStream() to actually fire the HTTP request and obtain the response body.

    See also:

    • How to use URLConnection to fire and handle HTTP requests?

    There are however more problems in your code:

    You’re firing a POST request, but there’s nowhere a doPost() method in your servlet definied. You need to either fire a normal GET or to implement doPost(). But that’ll be obvious by investigating the obtained error response as described in the above link.

    Also, your servlet is not threadsafe. You should never declare/assign request/session scoped data as instance variable of the servlet. Read this answer to learn more.

    Finally, did you consider using RequestDispatcher#include() instead? That will be handled internally and saves you the cost/overhead of firing HTTP requests.

    request.getRequestDispatcher("servleturl").include(request, response);
    

    You can then use request attributes instead of request parameters. The whole functional requirement makes little sense by the way. I’d just use a for or while loop on the code you’d like to execute recursively. But that’s a different problem.

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

Sidebar

Related Questions

I'm quiet new to bash scripting, and I would like to convert recursively all
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
I have a javascript object that I would like to update with data from
I have a recursive method call. When any exception is thrown, I would like
There is this Telerik's RadTreeView (Silverlight Version) control I'd like to extend (inherit from).
How do I construct functional and recursive datatypes in javascript? I would like to
In clojure, I would like to write a tail-recursive function that memoizes its intermediate
Would like a for loop in jquery so that: For every hover_link: show hidden
Would like someone to take a look at my script and tell me where
Would like to be able to set colors of headings and such, different font

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.