我想从Java应用程序调用Servlet。问题是,该调用似乎未到达Servlet。我没有收到任何错误,但是没有到达Servlet中的第一个输出“
doPost”。如果我在Web浏览器中打开URL,我当然会得到如下错误:不支持GET等。但是至少我知道发生了一些错误。
我使用以下代码(ActionPackage类仅包含一个Vector参数,并且可序列化):
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);
}
}
任何想法出了什么问题?
谢谢。