发送回 JSON 的 Servlet:接收混乱

发布于 2024-12-26 20:53:23 字数 860 浏览 0 评论 0原文

我有一个发送回 JSON 对象的 Servlet,我想在另一个 Java 项目中使用这个 Servlet。我有这个方法可以得到结果:

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;
}

这很有效,但我面临的问题如下: 当我同时执行多个请求时,结果会混淆,有时会得到与我发送的请求不匹配的响应。

我怀疑问题与我获取响应的方式有关:读取器从连接的输入流读取字符串。

我怎样才能确保我得到一个请求 ->一个相应的回复? 有没有更好的方法从 servlet 检索 JSON 对象?

干杯, 蒂姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅的被狗咬 2025-01-02 20:53:23

当我同时执行多个请求时,结果会混淆,有时会得到与我发送的请求不匹配的响应。

您的 servlet 不是线程安全的。我敢打赌,您已经错误地将请求范围的数据直接或间接分配为 servlet 的实例或类变量。这是初学者常见的错误。

仔细阅读Servlet 是如何工作的?实例化、会话、共享变量和多线程并相应地修复您的 servlet 代码。问题不在目前显示的 URLConnection 代码中,尽管它表明您在 两者 doGet() 中执行完全相同的工作和 doPost(),这反过来又已经让人了解 servlet 的设计方式了。

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.

夏末的微笑 2025-01-02 20:53:23

尝试删除 setDoOutput(true),您仅将连接用于输入,因此不应使用它。

编辑:或者尝试使用HttpClient,它是比使用“原始”Java 好得多。

Try removing setDoOutput(true), you are using the connection only for input and so you shouldn't use it.

Edit: alternatively try using HttpClient, it's much nicer that using "raw" Java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文