I am trying to perform a simple json encoding and decoding example with Java.
In this example I am sending signup page details to a Javascript. In Javascript I am encoding those values in json format and sending them to a servlet.
I don't know exactly where I'm going wrong but I'm unable to decode (parse) and print that data at the servlet end.
I'm new to json and Java and I just want to first print values from a json array in a servelet so that I can later put them in a database.
/*this is my javascript code*/
function signup()
{
var request = createCORSRequest( "GET", "http://localhost:8080/jsondem/pass" );
/*pass is the servlet*/
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);
/*i m just printing values for cross checking*/
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);
}
Following is my servlet where I want to take the json data uploaded on localhost into a servlet and I want to print it
/*this is my servlet's doGet Method*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
new JavaHttpUrlConnectionReader();
}
/*I could add all the content in servlet itself but I have done it in separate class 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";
// if your url can contain weird characters you will want to
// encode it here, something like this:
myUrl = URLEncoder.encode(myUrl, "UTF-8");
doHttpUrlConnectionAction(myUrl);
}
catch (Exception e)
{
System.out.println(e);
}
}
/*i m doing this by calling a method doHttpUrlConnectionAction*/
private void doHttpUrlConnectionAction(String myUrl) throws Exception {
URL url = null;
BufferedReader reader = null;
StringBuilder stringBuilder;
JSONParser jsonParser = new JSONParser();
try
{
// create the HttpURLConnection
url = new URL(myUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// just want to do an HTTP GET here
connection.setRequestMethod("GET");
// uncomment this if you want to write output to this url
//connection.setDoOutput(true);
// give it 15 seconds to respond
connection.setReadTimeout(15*1000);
connection.connect();
// read the output from the server
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();
}
}
}
}
}