Restful webservice
jquery, json,ajax example program:
By,
G.madhav
TUTIORALS
FROM MADHAV:
Index.jsp
<%@ page
language="java" import="java.util.*"
pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma"
content="no-cache">
<meta http-equiv="cache-control"
content="no-cache">
<meta http-equiv="expires"
content="0">
<meta http-equiv="keywords"
content="keyword1,keyword2,keyword3">
<meta http-equiv="description"
content="This is my page">
<!--
<link rel="stylesheet"
type="text/css" href="styles.css">
-->
<script type="text/javascript"
src="js/jquery.min.js"></script>
</head>
<body>
<a href="services/dept"> click hear to call dept
service</a><br>
<button id="mad">mad</button>
<script type="text/javascript">
$('#mad').click(function(){
testService();
});
function testService()
{
$.ajax({
type: "GET",
url: "services/dept",
contentType: "application/json;
charset=utf-8",
dataType: "json",
success: function(msg) {
// Do magic here
alert("madhav");
document.writeln("Book id: " + msg.phonetype);
}
});
}
</script>
</body>
</html>
DeptResource.java
package
madhav.api;
import
javax.ws.rs.GET;
import
javax.ws.rs.Produces;
import
javax.ws.rs.Path;
import
org.codehaus.jettison.json.JSONException;
import
org.codehaus.jettison.json.JSONObject;
@Path("/dept")
public
class DeptResource {
@GET
@Produces("application/json")
public JSONObject getClichedMessage() {
JSONObject jsonObj = null;
try
{
jsonObj = new
JSONObject("{\"phonetype\":\"N95\"}");
}
catch(Exception
e)
{
}
return jsonObj;
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<display-name>JAX-RS REST Servlet</display-name>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS REST Servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Output from rest client(firefox):
Output:
Fromt- end as html:
Related topics:
2. Restfulwebservice java, jquery, json,ajax example
3. Structr2 hibernate 3 integration example program
4. springmvc spring provided hibernate example
5. struct 2 spring dao
6. struts 2hibernate integration example
7. Jquery autocomplete from database restful webservice json,ajax
TUTIORALS
FROM MADHAV:
Hi,
ReplyDeleteThank you for this tutorial, can you please send me the source code or just tell me what jars I need to run it ?
Thanks