Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:52:57+00:00 2026-06-12T11:52:57+00:00

I want to send a serialized object from a Java class to a servlet

  • 0

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’m aware that I have to use HttpURLConnection to make a POST request to a servlet, but I don’t know whether the below code 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? Also, I want the servlet to send back the object it receives as response.

Any help would be appreciated. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:52:58+00:00Added an answer on June 12, 2026 at 11:52 am

    You would need to

    • Make sure the Names class implements java.io.Serializable marker interface.
    • Create an ObjectOutputStream from the servlet’s outputstream as follows:

      out = new ObjectOutputStream(urlCon.getOutputStream());
      
    • On the receiver side, once you read the object from the servlet’s inputstream and persist in the file, write it back to the response’s output stream as follows:

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

    On the sender’s 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’s 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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have this class serialized, so i can send it as the
Hi I have TCP/IP client server application. i want to send large serialized object
I have an object that I want to send with my jquery.ajax function but
I want to send SMS using LWUIT. what class should i be looking at?
I have an object that is mapped to a cookie as a serialized base-64
I am trying to send a serialized object from a server process to a
I want send a email by Email Class in codeigniter with gmail, but i
Want to send the text from my current vb application to the Active Window
I want to send a opened file descriptor between two different programs. So I
Well I want to send commands and data between client and server. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.