add server side code

add server side code
This commit is contained in:
hmkcode
2014-02-01 11:10:46 +03:00
parent ea25492333
commit f2ffb1c3a7
14 changed files with 6487 additions and 0 deletions
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jsonservlet</servlet-name>
<servlet-class>com.hmkcode.servlet.JSONServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>jsonservlet</servlet-name>
<url-pattern>/jsonservlet</url-pattern>
</servlet-mapping>
</web-app>
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

@@ -0,0 +1,31 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC - View (JSON, XML, PDF, Excel)</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
.style1 {
color: #C0C0C0;
font-family: Tahoma;
font-size:11px;
}
</style>
</head>
<body>
<a href="http://hmkcode.com"><h1>hmkcode.com</h1></a>
<p class="style1"><em>The data in this demo is hard-coded in a Java class not as
mentioned in the article in xml file since GAE is not allowing reading file.</em></p>
<h1>Spring MVC - View (JSON, XML, PDF, Excel)</h1>
<div style="margin:10px;width:700px;text-align:center">
<a href="rest/controller/get.json" class="label label-info">JSON</a>
<a href="rest/controller/get.xml" class="label label-info">XML</a>
<a href="rest/controller/get.pdf" class="label label-info">PDF</a>
<a href="rest/controller/get.xlsx" class="label label-info">Excel</a>
</div>
</body>
</html>
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

File diff suppressed because one or more lines are too long
@@ -0,0 +1,43 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Java Servlet JSON</title>
<script src="./js/jquery.1.9.1.min.js"></script>
<!-- bootstrap just to have good looking page -->
<link href="./bootstrap/css/bootstrap.css" type="text/css" rel="stylesheet" />
<!-- we code these -->
<script src="./js/myfunctions.js"></script>
</head>
<body>
<h1 style="text-align:center"><a href="http://hmkcode.com">HMKCode.com</a></h1>
<h1 style="text-align:center">Java Servlet that Receives POST Request with JSON Data<br></h1>
<div id="alert" class="alert alert-warning" style="width:200px;display:none">
<b>Warning!</b> Invalid input.
</div>
<!-- article inputs -->
<div class="article" style="margin:10px;text-align:center">
<p>
<button class="btn btn-primary" type="button" onclick="getAjax()">Refresh</button>
</p>
</div>
<!-- display all articles -->
<div style="width:700px;padding:20px">
<h5 style="text-align:center"><i style="color:#ccc"><small>Visitors</small></i></h5>
<table id="added-articles" class="table" >
<tr>
<th>Name</th>
<th>Country</th>
<th>Twitter</th>
</tr>
</table>
</div>
</body>
</html>
File diff suppressed because one or more lines are too long
@@ -0,0 +1,77 @@
function sendAjax() {
$("#alert").css("display","none");
if($.trim($('#name').val()).length == 0){
$("#alert").css("display","block");
return;
}
// get inputs
var person = new Object();
person.name = $('#name').val();
person.country = $('#country').val();
person.twitter = $('#twitter').val();
$.ajax({
url: "/jsonservlet",
type: 'POST',
dataType: 'json',
data: JSON.stringify(person),
contentType: 'application/json',
mimeType: 'application/json',
success: function (data) {
$("tr:has(td)").remove();
$.each(data, function (index, person) {
if(person.name.length > 0 )
$("#added-articles").append($('<tr/>')
.append($("<td/>").append($("<span class='label label-default' style='margin:4px;padding:4px'/>").text(person.name)))
.append($("<td/>").append($("<span class='label label-success' style='margin:4px;padding:4px'/>").text(person.country)))
.append($("<td/>").append($("<span class='label label-info' style='margin:4px;padding:4px'/>").text(person.twitter)))
);
});
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
}
//--------------------------------------
function getAjax() {
$("#alert").css("display","none");
$.ajax({
url: "/jsonservlet",
type: 'GET',
dataType: 'json',
contentType: 'application/json',
mimeType: 'application/json',
success: function (data) {
$("tr:has(td)").remove();
$.each(data, function (index, person) {
$("#added-articles").append($('<tr/>')
.append($("<td/>").append($("<span class='label label-default' style='margin:4px;padding:4px'/>").text(person.name)))
.append($("<td/>").append($("<span class='label label-success' style='margin:4px;padding:4px'/>").text(person.country)))
.append($("<td/>").append($("<span class='label label-info' style='margin:4px;padding:4px'/>").text(person.twitter)))
);
});
},
error:function(data,status,er) {
alert("error: "+data+" status: "+status+" er:"+er);
}
});
}
//--------------------------------------