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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:07:27+00:00 2026-05-28T04:07:27+00:00

I have a Servlet that sends back a JSON Object and I would like

  • 0

I have a Servlet that sends back a JSON Object and I would like to use this servlet in another Java project. I have this method that gets me the results:

public JSONArray getSQL(String aServletURL)
{
 JSONArray toReturn = null;
 String returnString = "";
 try
 {
    URL myUrl = new URL(aServletURL);
        URLConnection conn = myUrl.openConnection();
        conn.setDoOutput(true);
        BufferedReader in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
        String s;
        while ((s = in.readLine()) != null )
         returnString += s;
        in.close();

        toReturn = new JSONArray(returnString);
 }
 catch(Exception e)
 {
    return new JSONArray();
 }
        return toReturn;
}

This works pretty will, but the problem I am facing is the following:
When I do several simultaneous requests, the results get mixed up and I sometimes get a Response that does not match the request I send.

I suspect the problem to be related to the way I get the response back: The Reader reading a String from the InputStream of the connection.

How can I make sure that I get one reques -> one corresponding reply ?
Is there a better way to retrieve my JSON object from my servlet ?

Cheers,
Tim

  • 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-28T04:07:28+00:00Added an answer on May 28, 2026 at 4:07 am

    When I do several simultaneous requests, the results get mixed up and I sometimes get a Response that does not match the request I send.

    Your servlet is not thread safe. I’d bet that you’ve improperly assigned request scoped data either directly or indirectly as instance or class variables of the servlet. This is a common beginner’s mistake.

    Carefully read this How do servlets work? Instantiation, sessions, shared variables and multithreading and fix your servlet code accordingly. The problem is not in the URLConnection code shown so far, although it indicates that you’re doing exactly the same job in both doGet() and doPost(), which in turn is already a smell as to how the servlet is designed.

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

Sidebar

Related Questions

I have a normal java app that sends the same object to the servlet
I have a servlet that I would like to run within ColdFusion MX 7.
I have this java servlet that grabs information from a form, I need to
I have a file upload form that is being posted back to a servlet
I have a servlet that sends a file by setting the HTTP Content-Type to
I have an application that sends the customer to another site to handle the
I have a middlet that sends data to a servlet. Anyone knows how can
I have a Java servlet that's getting overloaded by client requests during peak hours.
I have tried to create a android application that sends a serialzed object from
I have a servlet that is used for many different actions, used in the

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.