Last active
September 3, 2015 09:17
-
-
Save the-shank/686fc1c0f00590ef98ca to your computer and use it in GitHub Desktop.
Managing Servlet JSON Response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
List<String> list = new ArrayList<String>(); | |
list.add("item1"); | |
list.add("item2"); | |
list.add("item3"); | |
String json = new Gson().toJson(list); | |
response.setContentType("application/json"); | |
response.setCharacterEncoding("UTF-8"); | |
response.getWriter().write(json); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { // When the HTML DOM is ready loading, then execute the following function... | |
$('#somebutton').click(function() { // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event... | |
$.get('someservlet', function(responseJson) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response JSON... | |
var $ul = $('<ul>').appendTo($('#somediv')); // Create HTML <ul> element and append it to HTML DOM element with ID "somediv". | |
$.each(responseJson, function(index, item) { // Iterate over the JSON array. | |
$('<li>').text(item).appendTo($ul); // Create HTML <li> element, set its text content with currently iterated item and append it to the <ul>. | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
Map<String, String> options = new LinkedHashMap<String, String>(); | |
options.put("value1", "label1"); | |
options.put("value2", "label2"); | |
options.put("value3", "label3"); | |
String json = new Gson().toJson(options); | |
response.setContentType("application/json"); | |
response.setCharacterEncoding("UTF-8"); | |
response.getWriter().write(json); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { // When the HTML DOM is ready loading, then execute the following function... | |
$('#somebutton').click(function() { // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event... | |
$.get('someservlet', function(responseJson) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response JSON... | |
var $select = $('#someselect'); // Locate HTML DOM element with ID "someselect". | |
$select.find('option').remove(); // Find all child elements with tag name "option" and remove them (just to prevent duplicate options when button is pressed again). | |
$.each(responseJson, function(key, value) { // Iterate over the JSON object. | |
$('<option>').val(key).text(value).appendTo($select); // Create HTML <option> element, set its value with currently iterated key and its text content with currently iterated item and finally append it to the <select>. | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
List<Product> products = someProductService.list(); | |
String json = new Gson().toJson(products); | |
response.setContentType("application/json"); | |
response.setCharacterEncoding("UTF-8"); | |
response.getWriter().write(json); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { // When the HTML DOM is ready loading, then execute the following function... | |
$('#somebutton').click(function() { // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event... | |
$.get('someservlet', function(responseJson) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response JSON... | |
var $table = $('<table>').appendTo($('#somediv')); // Create HTML <table> element and append it to HTML DOM element with ID "somediv". | |
$.each(responseJson, function(index, product) { // Iterate over the JSON array. | |
$('<tr>').appendTo($table) // Create HTML <tr> element, set its text content with currently iterated item and append it to the <table>. | |
.append($('<td>').text(product.id)) // Create HTML <td> element, set its text content with id of currently iterated product and append it to the <tr>. | |
.append($('<td>').text(product.name)) // Create HTML <td> element, set its text content with name of currently iterated product and append it to the <tr>. | |
.append($('<td>').text(product.price)); // Create HTML <td> element, set its text content with price of currently iterated product and append it to the <tr>. | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment