从 Java 应用程序调用 Servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4349854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:22:47  来源:igfitidea点击:

Calling a Servlet from a Java application

javaservletsurlconnection

提问by IceJava

I want to call a Servlet from a Java application. The problem is, that the call seems not to reach the Servlet. I do not get any error, but do not reach the first output "doPost" in the Servlet. If I open the URL in a web browser, I got - of course - the error that GET is not supported etc., but at least I see, that something happens.

我想从 Java 应用程序调用 Servlet。问题是,该调用似乎没有到达 Servlet。我没有收到任何错误,但没有到达 Servlet 中的第一个输出“doPost”。如果我在 Web 浏览器中打开 URL,我当然会得到不支持 GET 的错误等,但至少我看到,发生了一些事情。

I use the following code (the ActionPackage class only holds a Vector of parameters and is Serializable):

我使用以下代码(ActionPackage 类仅包含一个 Vector 参数并且是可序列化的):

Java application:

Java应用:

    ActionPackage p = new ActionPackage();
    p.addParameter("TEST", "VALUE");

    System.out.println(p);

    URL gwtServlet = null;
    try {
        gwtServlet = new URL("http://localhost:8888/app/PushServlet");
        HttpURLConnection servletConnection = (HttpURLConnection) gwtServlet.openConnection();
        servletConnection.setRequestMethod("POST");
        servletConnection.setDoOutput(true);

        ObjectOutputStream objOut = new ObjectOutputStream(servletConnection.getOutputStream());
        objOut.writeObject(p);
        objOut.flush();
        objOut.close();

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Servlet:

小服务程序:

public class PushServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("doPost");
    ObjectInputStream objIn = new ObjectInputStream(request.getInputStream());

    ActionPackage p = null;
    try {
        p = (ActionPackage) objIn.readObject();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    System.out.println("Servlet received p: "+p);       
}

}

}

Any ideas what went wrong?

任何想法出了什么问题?

Thanks.

谢谢。

采纳答案by BalusC

URLConnectionis only lazily executed whenever you call any of the getmethods.

URLConnection仅在您调用任何get方法时才延迟执行。

Add the following to your code to actuallyexecute the HTTP request and obtain the servlet response body.

将以下内容添加到您的代码中以实际执行 HTTP 请求并获取 servlet 响应正文。

InputStream response = servletConnection.getInputStream();

See also:

也可以看看:

回答by Mike Clark

Try wrapping the entire body of the doPost in a try/catch block:

尝试将 doPost 的整个主体包装在 try/catch 块中:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        System.out.println("doPost");
        ObjectInputStream objIn = new ObjectInputStream(request.getInputStream());
        ActionPackage p = null;
        p = (ActionPackage) objIn.readObject();
        System.out.println("Servlet received p: "+p);       
    } catch (Throwable e) {
        e.printStackTrace(System.out);
    }
}

Then look again at your Servlet output log file or window for a new Exception which may help.

然后再次查看您的 Servlet 输出日志文件或窗口以查找可能有帮助的新异常。

回答by msuresh

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        System.out.println("doPost");
        ObjectInputStream objIn = new ObjectInputStream(request.getInputStream());
        ActionPackage p = null;
        p = (ActionPackage) objIn.readObject();
        System.out.println("Servlet rece p: "+p);       
    } catch (Throwable e) {
        e.printStackTrace(System.out);
    }
}