I am posting a jQuery AJAX POST to a servlet and the data is in the form of JSON String.
Its getting posted successfully but on the Servlet side I need to read these key-val pairs into a Session Object and store them. I tried using JSONObject class but I am not able to get it.
Heres the code snippet
$(function(){
$.ajax(
{
data: mydata, //mydata={"name":"abc","age":"21"}
method:POST,
url: ../MyServlet,
success: function(response){alert(response);
}
});
On the Servlet side
public doPost(HTTPServletRequest req, HTTPServletResponse res)
{
HTTPSession session = new Session(false);
JSONObject jObj = new JSONObject();
JSONObject newObj = jObj.getJSONObject(request.getParameter("mydata"));
Enumeration eNames = newObj.keys(); //gets all the keys
while(eNames.hasNextElement())
{
// Here I need to retrieve the values of the JSON string
// and add it to the session
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…