troubleshooting Question

Sending objects with objects to java servlet using ajax

Avatar of MWilliamsBolton
MWilliamsBolton asked on
5 Comments2 Solutions2279 ViewsLast Modified:
I am trying to send an object from a browser to a java servlet and store it in a JSON object on the server.

I have the following code on the browser

         var newDetails = {age: 12, name:{firstname:"Martin", lastname:"Williams"}};

               type: "POST",
               url: "TestServlet",
               data: newDetails

And the following code on the test servlet.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("Age " + request.getParameter("age"));
            System.out.println("Name: " + request.getParameter("name"));


The code returns the age for the simple entry but prints null for the name.

Is there a generic way to send a complex (ish) object in javasvript and put it into a JSON object on the server?

Thanks in advance

Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros