从 J2ME 中的 servlet 检索响应时遇到问题

发布于 2025-01-04 11:55:16 字数 2524 浏览 0 评论 0原文

我正在尝试使用下面的代码检索从 servlet 到 midlet 的响应,

public String receiveData() {
        HttpConnection connection = null;
        String url = "http://localhost:8084/MCastServer/Create";
        DataInputStream is = null;
        OutputStream os = null;
        StringBuffer stringBuffer = new StringBuffer();
        String res = null;
        try {
            connection = (HttpConnection) Connector.open(url);
            connection.setRequestMethod(HttpConnection.GET);
            connection.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");
            connection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Confirguration/CLDC-1.0");
            connection.setRequestProperty("Content-Language", "en-CA");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            os = connection.openOutputStream();
            is = connection.openDataInputStream();
            System.out.println(url);
            int ch = 0;
            while ((ch = is.read()) == -1) {
                stringBuffer.append((char) ch);
                System.out.println(stringBuffer);
            }
            res = stringBuffer.toString();
            System.out.println(res);
            //ByteArrayOutputStream bos = new ByteArrayOutputStream();
        } catch (Exception e) {
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
                if (connection != null) {
                    connection.close();

                }
                //display.setCurrent(textBox);
            } catch (Exception e) {
            }

        }
        return res;
    }

但它始终返回空输出。我已经搜索并尝试了各种方法,但它仍然返回相同的结果。 下面是我写的Servlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();

        String groupNames = "SELECT phone_group_name FROM phone_group_name";
        InteractToDB dbCall = new InteractToDB("org.postgresql.Driver");
        dbCall.connect("jdbc:postgresql://localhost:5432/mcast", "postgres", "mimi");
        out.print(dbCall.getNames());
        System.out.println(dbCall.getNames() + " call");
        try {

        } finally {
            out.close();
        }
    }

I am trying to retreive a response from a servlet to a midlet using the code below

public String receiveData() {
        HttpConnection connection = null;
        String url = "http://localhost:8084/MCastServer/Create";
        DataInputStream is = null;
        OutputStream os = null;
        StringBuffer stringBuffer = new StringBuffer();
        String res = null;
        try {
            connection = (HttpConnection) Connector.open(url);
            connection.setRequestMethod(HttpConnection.GET);
            connection.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");
            connection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Confirguration/CLDC-1.0");
            connection.setRequestProperty("Content-Language", "en-CA");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            os = connection.openOutputStream();
            is = connection.openDataInputStream();
            System.out.println(url);
            int ch = 0;
            while ((ch = is.read()) == -1) {
                stringBuffer.append((char) ch);
                System.out.println(stringBuffer);
            }
            res = stringBuffer.toString();
            System.out.println(res);
            //ByteArrayOutputStream bos = new ByteArrayOutputStream();
        } catch (Exception e) {
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
                if (connection != null) {
                    connection.close();

                }
                //display.setCurrent(textBox);
            } catch (Exception e) {
            }

        }
        return res;
    }

But it keeps returning a null output. I have searched and tried various means but it still returns the same.
Below is the Servlet which I wrote

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();

        String groupNames = "SELECT phone_group_name FROM phone_group_name";
        InteractToDB dbCall = new InteractToDB("org.postgresql.Driver");
        dbCall.connect("jdbc:postgresql://localhost:5432/mcast", "postgres", "mimi");
        out.print(dbCall.getNames());
        System.out.println(dbCall.getNames() + " call");
        try {

        } finally {
            out.close();
        }
    }

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

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

发布评论

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

评论(1

早茶月光 2025-01-11 11:55:16

你有一个空的 catch 块 - 这不是一个好主意。您应该至少打印堆栈跟踪。

我还认为将数据库代码放入 servlet 中是一个糟糕的主意。我将编写一个基于接口的 POJO,在没有 servlet 的情况下彻底测试代码,然后在 servlet 中调用其方法。它将问题分解为更小的问题,并有助于您的单元测试工作。

为什么要为每个请求创建一个连接?为什么不使用连接池来分摊创建连接的成本?

为什么你在课堂上以纯文本形式硬连接你的信息?如果 dbCall 为 null 会发生什么?如果抛出SQLException怎么办?

我越看这段代码,就越发现它最糟糕。我最好现在就停下来。

You have an empty catch block - that's not a good idea. You should print the stack trace at minimum.

I also think it's a terrible idea to put database code in a servlet. I'd write an interface-based POJO, test out the code thoroughly without the servlet, and then call its methods in the servlet. It decomposes the problem into smaller ones and helps your unit testing efforts.

Why are you creating a connection per request? Why aren't you using a connection pool to amortize the cost of creating connections?

Why are you hard-wiring your information in plain text in the class? What happens if dbCall is null? What if a SQLException is thrown?

The more I look at this code, the worst it gets. I'd better stop now.

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