Webservice :-Rest Example
Server:-----Dynamic
project
Web.xml
Index.jsp
ConnectionUtility.java
UserDao.java
RequestHandler.java
Required jar
file:----
asm-3.1.jar
jaxb-api-2.1.jar
jersey-bundle-1.2.jar
jersey-core-1.2.jar
jersey-server-1.14.jar
jersey-servlet-1.12.jar
json-1.5.jar
jsr311-api-1.1.1.jar
postgresql-9.3-1103.jdbc3.jar
//web.xml
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>8RestServer</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ab</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>ab</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
//index.jsp
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert
title here</title>
</head>
<body>
server
ready..................
</body>
</html>
// ConnectionUtility.java
package govind;
import
java.sql.Connection;
import
java.sql.DriverManager;
public
class ConnectionUtility {
private
ConnectionUtility()
{
}
static
Connection con=null;
static
{try
{
Class.forName("org.postgresql.Driver");
con=
DriverManager.getConnection("jdbc:postgresql://localhost:5433/govind",
"postgres", "manager");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public
static Connection getConnection()
{
return con;
}
}
}
// UserDao.java
package govind;
import java.sql.Connection;
import
java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.json.JSONArray;
import org.json.JSONObject;
public class UserDao {
JSONArray ja=new JSONArray();
public JSONArray getUserData()
{
try
{
Connection
con=ConnectionUtility.getConnection();
PreparedStatement ps = con.prepareStatement("select
* from servcurd");
ResultSet
rs = ps.executeQuery();
while(rs.next())
{
JSONObject
jb=new JSONObject();
jb.put("id",rs.getInt(1));
jb.put("name",rs.getString(2));
jb.put("password",rs.getString(3));
jb.put("email",rs.getString(4));
jb.put("country",rs.getString(5));
ja.put(jb);
}
}
catch (Exception e) {
e.printStackTrace();
}
return ja;
}
}
// RequestHandler.java
package
govind;
import
javax.ws.rs.POST;
import
javax.ws.rs.Path;
import
org.json.JSONArray;
@Path("/rh")
public class
RequestHandler {
@POST
@Path("/getdata")
public
String getData()
{
String res=null;
UserDao ud=new
UserDao();
JSONArray ja = ud.getUserData();
res=ja.toString();
return res;
}
}
Client.........(Normal
Java Project)
Required Jar file:-------
asm-3.1.jar
jaxb-api-2.1.jar
jersey-client-1.8.jar
jersey-core-1.8.jar
lib/json-1.5.jar
===============================================================================
// ClientGovind.java
package govind;
import
org.json.JSONArray;
import
org.json.JSONObject;
import
com.sun.jersey.api.client.Client;
import
com.sun.jersey.api.client.ClientResponse;
import
com.sun.jersey.api.client.WebResource;
public class ClientGovind {
public static
void main(String[] args) {
try
{
Client client
= Client.create();
WebResource wr
= client.resource("http://localhost:6789/thirdserverprogram/services/rh/getdata");
ClientResponse res
= wr.accept("application/text").post(ClientResponse.class);
String r
=res.getEntity(String.class);
//System.out.println(r);
JSONArray ja=new JSONArray(r);
for(int i=0;i<ja.length();i++)
{
JSONObject j
= ja.getJSONObject(i);
int
id= j.getInt("id");
String name=j.getString("name");
String address=
j.getString("password");
String email=j.getString("email");
String country=j.getString("country");
System.out.println("Id="+id+" "+"
Name="+name+" "+"Address="+address+"
Email="+email+" "+"Country="+country);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
o/p:----------------
Id=6 Name=saumya Address=saumya123
Email=saumya@gmail.com Country=India
Id=9 Name=ajit Address=2345ajit
Email=ajitraj@gmail.com Country=India
Id=12 Name=mani Address=mani1234 Email=mani1234@gmail.com
Country=India
Id=3 Name=sanjay Address=sanjay12346
Email=sanjay@gmail.com Country=India
Id=11 Name=sandhya Address=sandhya1234
Email=sandhya@gmail.com Country=India
Id=13 Name=rajan kumar Address=rajan1234
Email=rajan1234@gmail.com Country=India
Id=14 Name=Murari chaudhary Address=murari
Email=murari@gmail.com Country=India
Id=15 Name=Govind Ballabh Khan Address=govind1234
Email=govindkhan@gmail.com Country=India
Id=16 Name=piyushkumar Address=1wer23
Email=piyushkumar@gmail.com Country=Srilanka
This blog provide incredible stuff... Thanks Govind bhai
ReplyDelete