File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having problems sending JSON from html form to servlet

 
Sanjeet Khan
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I have problem. I have a JSONObject in my JSP page that I need to send to a servlet when the "submit" button on a form is pressed, along with other form data. Currently creating the JSONObject and setting the page attribute as such:

String myUrl = "http://localhost:8888/startexam";
URL url = new URL(myUrl);

BufferedReader reader = new BufferedReader (new InputStreamReader(url.openConnection().getInputStream()));

StringBuilder sb = new StringBuilder();
int cp;
while ((cp = reader.read()) != -1) {
sb.append((char) cp);
}

String jsonText = sb.toString();
JSONObject json = new JSONObject(jsonText);
System.out.println(json);

JSONArray questionArray = json.getJSONArray("exam");
request.setAttribute("JSONARRAY", jsonText);
request.setAttribute("STARTDATETIME", examID);

When I print this out, the JSONObject and the page attribute print out fine, I get the FULL JSONObject value. When the action is called on submit, a weird thing happens. Here's the form submit code (a table is imbedded):

<td></td>
<td colspan="4">
<input type="submit" value="Submit Answers" /></td>
<td><input type="hidden" name="json" value=<%= request.getAttribute("JSONARRAY") %>>
<input type="hidden" name="startdatetime" value=<%= request.getAttribute("STARTDATETIME") %>></td>
<td></td>

When the form is submitted, the JSON value is:

json=%7B%22exam%22%3A%5B%7B%22question%22%3A%7B%22choice2%22%3A%221000%22%2C%22choice1%22%3A%22100%22%2C%22choice4%22%3A%222000%22%2C%22category%22%3A%22TECHNOLOGY%22%2C%22choice3%22%3A%221500%22%2C%22answer%22%3A%22choice2%22%2C%22question%22%3A%22How&startdatetime=20130114182505

This is a lot shorter than the actual JSONObject value which is:

{"exam":[{"question":{"choice2":"1000","choice1":"100","category":"TECHNOLOGY","choice4":"2000","choice3":"1500","answer":"choice2","question":"How many megabytes are in a one gigabyte?"}},{"question":{"choice2":"10","choice1":"13","category":"MATH","choice4":"6","choice3":"12","answer":"choice3","question":"How many eggs are in a dozen?"}},{"question":{"choice2":"Database Management System","choice1":"Death Before Mad Skills","category":"TECHNOLOGY","choice4":"Delivery Before Modification System","choice3":"Data Batch Manipulation System","answer":"choice2","question":"What does DBMS stand for?"}},{"question":{"choice2":"Came Lights","choice1":"Camel Crush","category":"PERSONAL","choice4":"Marlboro Smooths","choice3":"Marlboro Menthol","answer":"choice1","question":"What type of cigarette does Imran smoke?"}},{"question":{"choice2":"Red","choice1":"Blue","category":"PERSONAL","choice4":"Pink","choice3":"Orange","answer":"choice4","question":"What is Oscar's favorite color?"}},{"question":{"choice2":"6","choice1":"17","category":"MATH","choice4":"4","choice3":"9","answer":"choice4","question":"What is 2 + 2?"}},{"question":{"choice2":"1","choice1":"0","category":"PERSONAL","choice4":"3","choice3":"2","answer":"choice2","question":"How many siblings does Imran have?"}},{"question":{"choice2":"Earth","choice1":"Mars","category":"ASTROLOGY","choice4":"Jupiter","choice3":"Saturn","answer":"choice4","question":"What is the biggest planet in our solar system?"}},{"question":{"choice2":"54","choice1":"47","category":"MATH","choice4":"49","choice3":"63","answer":"choice3","question":"What is 7 x 9?"}},{"question":{"choice2":"Giant Orange","choice1":"God","category":"ASTROLOGY","choice4":"Quasar","choice3":"Star","answer":"choice3","question":"What is our sun?"}},{"question":{"choice2":"Quasar","choice1":"Black Hole","category":"ASTROLOGY","choice4":"Helixical Formation","choice3":"Chocolate","answer":"choice1","question":"What is usually at the center of every galaxy?"}},{"question":{"choice2":"7","choice1":"3","category":"MATH","choice4":"4","choice3":"8","answer":"choice4","question":"How many pints are in a gallon?"}},{"question":{"choice2":"No Structured Query Language","choice1":"Never On SQL","category":"TECHNOLOGY","choice4":"New Orders Serve Quiet Leaders","choice3":"Not Only SQL","answer":"choice3","question":"What does NoSQL stand for?"}},{"question":{"choice2":"10x","choice1":"4x","category":"ASTROLOGY","choice4":"1x","choice3":"8x","answer":"choice1","question":"How man times greater is the sun's circumference than the moons? "}},{"question":{"choice2":"Onion","choice1":"Pepperoni","category":"PERSONAL","choice4":"Ham","choice3":"Bell Peppers","answer":"choice2","question":"What is Imran's favorite topping on pizza?"}},{"question":{"choice2":"9","choice1":"8","category":"ASTROLOGY","choice4":"6","choice3":"4","answer":"choice1","question":"How many planets do we have"}},{"question":{"choice2":"Battlefield","choice1":"Dragon's Dogma","category":"PERSONAL","choice4":"Assassins Creed","choice3":"Gears of War","answer":"choice2","question":"What is Imran's favorite Xbox game?"}},{"question":{"choice2":"Raleigh, NC","choice1":"Richmond, VA","category":"PERSONAL","choice4":"Calcutta, India","choice3":"Goa, India","answer":"choice4","question":"Where was Imran born?"}},{"question":{"choice2":"Cookies Preparation Utility","choice1":"Computer Processing Unit","category":"TECHNOLOGY","choice4":"Computing Preparation Unit","choice3":"Computer Processor Unit","answer":"choice3","question":"What does CPU stand for?"}},{"question":{"choice2":"Great Band","choice1":"Gigabyte","category":"TECHNOLOGY","choice4":"Green Bay","choice3":"Greatbyte","answer":"choice1","question":"What does GB stand for?"}},{"question":{"choice2":"Reliable Available Memory","choice1":"Remaining Available Memory","category":"TECHNOLOGY","choice4":"Random Available Memory","choice3":"Random Access Memory","answer":"choice3","question":"What does RAM stand for?"}},{"question":{"choice2":"blue","choice1":"yellow","category":"ASTROLOGY","choice4":"red","choice3":"orange","answer":"choice1","question":"What color is the sun?"}},{"question":{"choice2":"2","choice1":"1","category":"MATH","choice4":"4","choice3":"3","answer":"choice3","question":"How many sides does a triangle have?"}},{"question":{"choice2":"25","choice1":"30","category":"MATH","choice4":"40","choice3":"20","answer":"choice2","question":"What is 5 x 5?"}}]}

I have been all over the web, forums, text books, resources, etc. and I can not find a simple solution to just send the JSONObject intact within the request body.

Any help would be greatly appreciated!

Thanks!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13047
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you doing a GET or a POST form submission with those parameters?

Bill
 
Sanjeet Khan
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry, forgot to add that. I'm doing a POST for the form method.
 
Hikari Shidou
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you need to "url_decode()" it. I never tried to parse JSON with those codes in place of special chars, but maybe that's the cause.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic