I am new to developing web applications. I have successfully created a dynamic web project using Java EE on a Glassfish server. Now, I am trying to enable clients to send data to the server using JSON and receive data from the server in either JSON or XML format. Although I have grasped the server-side programming concepts through online research, I am facing difficulties implementing the code. Currently, I am using AJAX to send JSON data from the client side. However, I seem to be missing some code for the server side. My plan is to use JSP to read the JSON data, utilize a bean that generates data, and send the processed data back to the client. Below is a snippet of my code, but I am uncertain about its functionality. Any advice would be highly appreciated as I value your assistance!
Below is the AJAX code used on the client side to send two input numbers from a form:
$(function() {
$("#myform").submit(function() {
var lat = $("#num1").val();
var lon = $("#num2").val();
alert("form");
if (num1 == '' || num2 == '') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
$.ajax({
type : "POST",
url : "marker.jsp",
contenttype : 'application/json; charset=utf-8',
data : {
"num1" : "wtf",
"num2" : $("#num2").val(),
},
success : function(msg) {
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
alert(msg);
}
});
}
return false;
});
});
Upon accessing the JSP page, I noticed that only null values are displayed. Here's a snippet of the server-side code where I intended to send XML data initially. I am unsure if the request.getParameter method functions correctly and how to send the data back using XML or JSON format. Your guidance on this matter is greatly appreciated!
<?xml version="1.0" encoding="UTF-8"?>
<%@ page contentType="text/xml" %>
<%@ page import="javax.naming.InitialContext,net.roseindia.ejb3.stateless.*,javax.ejb.EJB,java.util.*"%>
...server-side script continues here...