My current project involves working on a basic example of encoding and decoding JSON using Java. The goal is to send signup page details to a JavaScript function, which then encodes these values into JSON format before sending them to a servlet.
While I have managed to successfully send the data, I am facing issues with decoding (parsing) and displaying it at the servlet end. As someone who is new to both JSON and Java, my primary objective is to retrieve values from a JSON array in a servlet, which I plan to store in a database later on.
/*Sample JavaScript code snippet*/
function signup() {
var request = createCORSRequest("GET", "http://localhost:8080/jsondem/pass");
/* Retrieve input field values */
var name = document.getElementById('name').value;
var mobileNo = document.getElementById('mobileNo').value;
var emailId = document.getElementById('emailId').value;
var password = document.getElementById('password').value;
alert(name);
alert(mobileNo);
alert(emailId);
alert(password);
/* Construct data object for JSON conversion */
var data = {"name": name, "password": password, "mobileNo": mobileNo, "emailId": emailId};
alert(JSON.stringify(data));
var sendData = function(data){
alert(JSON.stringify(data));
$.ajax({
url:'http://localhost:8080/jsondem/pass',
type: 'GET',
contentType: 'application/json',
data: JSON.stringify(data),
success: function(response) {
alert(response);
},
error: function(response) {
alert('error'+response);
}
});
};
sendData(data);
}
In order to handle the JSON data received in my local server through a servlet, I have implemented the following solution:
/*Sample servlet's doGet Method*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
new JavaHttpUrlConnectionReader();
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
class JavaHttpUrlConnectionReader {
public JavaHttpUrlConnectionReader() {
try {
String myUrl = "http://localhost:8080/jsondem/pass";
myUrl = URLEncoder.encode(myUrl, "UTF-8");
doHttpUrlConnectionAction(myUrl);
} catch (Exception e) {
System.out.println(e);
}
}
private void doHttpUrlConnectionAction(String myUrl) throws Exception {
URL url = null;
BufferedReader reader = null;
StringBuilder stringBuilder;
JSONParser jsonParser = new JSONParser();
try {
url = new URL(myUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(15*1000);
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
String name = (String) jsonObject.get("name");
System.out.println("Name: " + name);
long mobileNo = (long) jsonObject.get("mobileNo");
System.out.println("Mobile Number: " + mobileNo);
String emailId = (String) jsonObject.get("emailId");
System.out.println("Email Id: " + emailId);
String password = (String) jsonObject.get("password");
System.out.println("Password: " + password);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
}
I have encountered some challenges in handling the output as shown below. Any assistance would be greatly appreciated. https://i.sstatic.net/MUzqH.png