简体   繁体   English

从Java应用程序调用Servlet

[英]Calling a Servlet from a Java application

I want to call a Servlet from a Java application. 我想从Java应用程序调用Servlet。 The problem is, that the call seems not to reach the Servlet. 问题是,调用似乎没有到达Servlet。 I do not get any error, but do not reach the first output "doPost" in the Servlet. 我没有得到任何错误,但没有到达Servlet中的第一个输出“doPost”。 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. 如果我在网络浏览器中打开URL,我当然得到了GET不支持的错误等,但至少我看到,有些事情发生了。

I use the following code (the ActionPackage class only holds a Vector of parameters and is Serializable): 我使用以下代码(ActionPackage类只包含参数Vector并且是Serializable):

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: 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. 谢谢。

URLConnection is only lazily executed whenever you call any of the get methods. 只要你调用任何get方法, URLConnection就会被懒惰地执行。

Add the following to your code to actually execute the HTTP request and obtain the servlet response body. 将以下内容添加到代码中以实际执行HTTP请求并获取servlet响应主体。

InputStream response = servletConnection.getInputStream();

See also: 也可以看看:

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

Try wrapping the entire body of the doPost in a try/catch block: 尝试在try / catch块中包装doPost的整个主体:

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输出日志文件或窗口以获取可能有用的新异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM