I want to call a servlet from a Java application. The problem is that the call does not seem to reach the servlet. I am not getting any errors, but I am not reaching the first output of "doPost" in Servlet. If I open the URL in a web browser, I received, of course, a GET error, which is not supported, etc., but at least I can see that something is happening.
I use the following code (the ActionPackage class contains only a vector of parameters and is Serializable):
Java application:
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.
source share