Calling a servlet from a Java application

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.

+4
source share
3 answers

URLConnection is only executed lazily when you call any of the get methods.

Add the following to your code to complete the HTTP request and get the servlet response body.

 InputStream response = servletConnection.getInputStream(); 

See also:

+7
source

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

 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 file or servlet output log window for a new exception that might help.

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

Source: https://habr.com/ru/post/887173/


All Articles