How to use HttpURLConnection to send a serialized object to a servlet from a Java class?

I want to send a serialized object from a Java class to a servlet, where the servlet should retrieve and save the object as a file. I know that I need to use HttpURLConnection to make a POST request for the servlet, but I do not know if the code below is correct.

private static HttpURLConnection urlCon; private static ObjectOutputStream out; public static void main(String[] args) { Names names = new Names(); names.setName("ABC"); names.setPlace("Bangalore"); URL url; try { url = new URL("http://localhost:6080/HttpClientSerializable/HttpPostServlet"); try { out = (ObjectOutputStream) urlCon.getOutputStream(); out.writeObject(names); urlCon = (HttpURLConnection) url.openConnection(); urlCon.setRequestMethod("POST"); urlCon.setDoOutput(true); out.close(); } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e1) { e1.printStackTrace(); } } 

And in the servlet, I have the following code:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ObjectInputStream in = new ObjectInputStream(request.getInputStream()); try { names = (Names) in.readObject(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } in.close(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:/Documents and Settings/RAGASTH/Desktop/Names")); out.writeObject(names); out.close(); } 

What should I do to make it work? In addition, I want the servlet to send back the object that it received as a response.

Any help would be greatly appreciated. Thanks!

+7
source share
1 answer

You need

  • Make sure the Names class implements the java.io.Serializable token interface.
  • Create an ObjectOutputStream from the servlet output stream as follows:

     out = new ObjectOutputStream(urlCon.getOutputStream()); 
  • On the receiver side, as soon as you read the object from the servlet input stream and save it in a file, write it back to the response output stream as follows:

     out = new ObjectOutputStream(response.getOutputStream()); out.writeObject(names); out.close(); 

On the sender side:

 Names names = new Names(); names.setName("ABC"); names.setPlace("Bangalore"); URL url; try { url = new URL("http://localhost:6080/HttpClientSerializable/HttpPostServlet"); urlCon = (HttpURLConnection) url.openConnection(); urlCon.setDoOutput(true); // to be able to write. urlCon.setDoInput(true); // to be able to read. out = new ObjectOutputStream(urlCon.getOutputStream()); out.writeObject(names); out.close(); ObjectInputStream ois = new ObjectInputStream(urlCon.getInputStream()); names = (Names) ois.readObject(); ois.close(); } catch (IOException e) { e.printStackTrace(); } catch (MalformedURLException e1) { e1.printStackTrace(); } 

On the receiver side:

 ObjectInputStream in = new ObjectInputStream(request.getInputStream()); try { names = (Names) in.readObject(); } catch (ClassNotFoundException e) { e.printStackTrace(); } in.close(); ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("C:/Documents and Settings/RAGASTH/Desktop/Names")); out.writeObject(names); out.close(); ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream()); oos.writeObject(names); oos.close(); 
+8
source

All Articles