English 中文(简体)
送还JSON的服务站:招待会
原标题:Servlet that sends back JSON: Confusion on reception

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.

我怀疑这个问题与我回过来的方式有关: 阅读者阅读了该链接的InputStream的引力。

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

最佳回答

如果我同时提出几项要求,结果会好坏参半,我有时得到的反应与我提出的要求不符。

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.

问题回答

在删除<条码>查询时,你只使用该链接作为投入使用,因此,你应不使用。

HttpClient, 替代试验使用





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...