I am using Apache’s HttpClient to call a servlet from a Java class. I want to send an object to the servlet which should save the object using Serialization. How to send the object to the servlet?
public static void main(String[] args) {
Names names = new Names();
names.setName("ABC");
names.setPlace("Bangalore");
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://localhost:9080/HttpClientPractice/FirstServlet");
//Rest of the code
In the above code snippet, I have another class Names and I want to send an object of Names to the servlet. Right now I am only calling the servlet with the URI, but I want to pass the object.
Thanks!
Convert the object to a Json String using a Library like Gson in the class you have shown and add this string as a query parameter – showing you how to do it with Gson.
In the class you are going to store this object, do a fromJson (method in the Gson library)