parent
ea25492333
commit
f2ffb1c3a7
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
|
||||
|
||||
<persistence-manager-factory name="transactions-optional">
|
||||
<property name="javax.jdo.PersistenceManagerFactoryClass"
|
||||
value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
|
||||
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
|
||||
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
|
||||
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
|
||||
<property name="javax.jdo.option.RetainValues" value="true"/>
|
||||
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
|
||||
</persistence-manager-factory>
|
||||
</jdoconfig>
|
||||
@ -0,0 +1,71 @@
|
||||
package com.hmkcode.servlet;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.hmkcode.vo.Person;
|
||||
|
||||
public class JSONServlet extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// This will store all received articles
|
||||
List<Person> persons = new LinkedList<Person>();
|
||||
|
||||
/***************************************************
|
||||
* URL: /jsonservlet
|
||||
* doPost(): receives JSON data, parse it, map it and send back as JSON
|
||||
****************************************************/
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException{
|
||||
|
||||
// 1. get received JSON data from request
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
|
||||
String json = "";
|
||||
if(br != null){
|
||||
json = br.readLine();
|
||||
}
|
||||
|
||||
// 2. initiate jackson mapper
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
// 3. Convert received JSON to Article
|
||||
Person person = mapper.readValue(json, Person.class);
|
||||
|
||||
// 4. Set response type to JSON
|
||||
response.setContentType("application/json");
|
||||
|
||||
// 5. Add article to List<Article>
|
||||
if(persons.size() > 20)
|
||||
persons.remove(0);
|
||||
|
||||
persons.add(person);
|
||||
|
||||
// 6. Send List<Article> as JSON to client
|
||||
mapper.writeValue(response.getOutputStream(), persons);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
|
||||
//2. initiate jackson mapper
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
|
||||
// 4. Set response type to JSON
|
||||
resp.setContentType("application/json");
|
||||
|
||||
|
||||
// 6. Send List<Article> as JSON to client
|
||||
mapper.writeValue(resp.getOutputStream(), persons);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.hmkcode.vo;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String name;
|
||||
private String country;
|
||||
private String twitter;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return country;
|
||||
}
|
||||
|
||||
public void setCountry(String country) {
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getTwitter() {
|
||||
return twitter;
|
||||
}
|
||||
|
||||
public void setTwitter(String twitter) {
|
||||
this.twitter = twitter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [name=" + name + ", country=" + country + ", twitter="
|
||||
+ twitter + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
# A default log4j configuration for log4j users.
|
||||
#
|
||||
# To use this configuration, deploy it into your application's WEB-INF/classes
|
||||
# directory. You are also encouraged to edit it as you like.
|
||||
|
||||
# Configure the console as our one appender
|
||||
log4j.appender.A1=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n
|
||||
|
||||
# tighten logging on the DataNucleus Categories
|
||||
log4j.category.DataNucleus.JDO=WARN, A1
|
||||
log4j.category.DataNucleus.Persistence=WARN, A1
|
||||
log4j.category.DataNucleus.Cache=WARN, A1
|
||||
log4j.category.DataNucleus.MetaData=WARN, A1
|
||||
log4j.category.DataNucleus.General=WARN, A1
|
||||
log4j.category.DataNucleus.Utility=WARN, A1
|
||||
log4j.category.DataNucleus.Transaction=WARN, A1
|
||||
log4j.category.DataNucleus.Datastore=WARN, A1
|
||||
log4j.category.DataNucleus.ClassLoading=WARN, A1
|
||||
log4j.category.DataNucleus.Plugin=WARN, A1
|
||||
log4j.category.DataNucleus.ValueGeneration=WARN, A1
|
||||
log4j.category.DataNucleus.Enhancer=WARN, A1
|
||||
log4j.category.DataNucleus.SchemaTool=WARN, A1
|
||||
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//--------------------------------------
|
||||
Loading…
Reference in New Issue