Wednesday, 16 May 2018


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 {
               
                public JSONArray getUserData(String id)
                {
                                JSONArray ja=new JSONArray();
                               
                                try
                                {
                                PreparedStatement ps=null;     
                                Connection con = ConnectionUtility.getConnection();
                                if(id.equalsIgnoreCase("all"))
                                {
                                ps=con.prepareStatement("select * from restdata");
                                }
                                else
                                {
                                ps=con.prepareStatement("select * from restdata where id=?");
                                ps.setString(1, id);
                                }
                               
                                ResultSet rs = ps.executeQuery();
                                while(rs.next())
                                {
                                                JSONObject jb=new JSONObject();
                                                jb.put("id", rs.getString(1));
                                                jb.put("name", rs.getString(2));
                                                jb.put("password", rs.getString(3));
                                                jb.put("phone", rs.getString(4));
                                                jb.put("email", rs.getString(5));
                                                jb.put("country", rs.getString(6));
                                                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 id)
                {
                                String res=null;
                                UserDao ud=new UserDao();
                                JSONArray ja = ud.getUserData(id);
                                res=ja.toString();
                                return res;
                               
                               
                }

}

//http://localhost:6789/fourthrestserver/services/rh/getData
=========================================================================














 ====================================================================

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/fourthrestserver/services/rh/getData");
                                                ClientResponse cres = wr.accept("application/text").post(ClientResponse.class,"101");
                                                String r = cres.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);
                                                                String id=j.getString("id");
                                                                String name=j.getString("name");
                                                                String pass=j.getString("password");
                                                                String phone=j.getString("phone");
                                                                String email=j.getString("email");
                                                                String country=j.getString("country");
                                                                System.out.println("Id="+id+" "+"Name="+name+" "+"Password="+pass+" "+"phone="+phone+"Email="+email+"country="+country);
                                                }
                                }
                                catch (Exception e) {
                                                e.printStackTrace();
                                }
                }

}

=======================================================================


o/p:------------------


Id=101 Name=govind Password=123456 phone=9039456100Email=govindkhan@gmail.comcountry=india

Id=102 Name=ajit Password=198765 phone=9023456711Email=ajitraj@gmail.comcountry=india
Id=103 Name=mani Password=198765 phone=8856786754Email=mani@gmail.comcountry=india




2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice efforts to teach other. Excellent work Brother.

    ReplyDelete