小编Ice*_*ava的帖子

从Java应用程序调用Servlet

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

我使用以下代码(ActionPackage类只包含参数Vector并且是Serializable):

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Servlet的:

public class PushServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("doPost");
    ObjectInputStream objIn …
Run Code Online (Sandbox Code Playgroud)

java servlets urlconnection

5
推荐指数
1
解决办法
5万
查看次数

标签 统计

java ×1

servlets ×1

urlconnection ×1