Showing posts with label Web services. Show all posts
Showing posts with label Web services. Show all posts

Saturday, December 26, 2009

Web Service truy xuất cơ sở dữ liệu

Trong bài này, chúng ta sẽ làm 1 ứng dụng web service truy xuất đến cơ sở dữ liệu.

Phần 1: tạo Cơ sở dữ liệu

Tạo Databse tên LogonWS, tạo 1 bảng tên Users với cấu trúc như hình sau
Column Name Data Type

userID


nvarchar(30)

Password


nvarchar(50)

Nhập vài mẫu tin để thử.

Tạo 1 DSN với tên LogonWS tham chiếu đến database này.

Phần 2: Tạo Web Service

File->New->Dynamic Web Project, đặt tên cho project là WS_DB



Nhấn Finish

Thêm vào 1 package tên vovanhai.wordpress.com

Thêm vào lớp tên LongonService.java có nội dung như sau:

package vovanhai.wordpress.com;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class LogonService {

/**

* Đăng nhập hệ thống

* @param userName

* @param password

* @return 1: thành công; 0: sai Password; -1: sai username

*/

public int Logon(String userName,String password){

Connection con = null;

Statement stm = null;

ResultSet rs = null;

int result = 0;

String selectStatement = “select * from Users where userID=’”+userName+“‘”;

try{

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

con = DriverManager.getConnection(“jdbc:odbc:LogonWS”,“sa”,“”);

stm = con.createStatement();

rs = stm.executeQuery(selectStatement);

if(!rs.next())

result = -1;//sai username

else{

String psw=rs.getString(“password”);

if(psw.equals(password)){

result= 1; //thành công

}

else

result= 0;//sai password

}

}catch(Exception ex){

ex.printStackTrace();

}

return result;

}

}

Kết quả như hình sau khi xem trong Package Explorer



Nhấn phải chuột lên lớp LogonService.java trong project Explorer, chọn New->Others…



Chọn Web Service, nhấn Next. Kết quả như hình



Nhấn Finish để hoàn tất công việc. Eclipse sẽ tựn động cài các thư viện và phát sinh các trong web quản lý của Axis2. Đồng thời eclipse cũng mở cho chúng ta cửa sổ Web Service Exploere như hình, và chúng ta có thể kiểm tra web service của chúng ta trên đó.



Bây giờ chúng ta mở rộng thư mục axis2-web của project trong cửa sổ Project Explorer, tìm đến trang index.jsp, nhấn chuột phải lên trang này, chọn Run As->Run on Server, chọn Tomcat rồi nhấn Finish, kết quả như hình sau





Nhấn chọn Service ta được



Ta có thể xem WSDL file bằng cách nhấn vào link LogonService. Ghi nhớ lại URL này.

http://localhost:8086/WS_DB/services/LogonService?wsdl

Vậy là chúng ta có 1 Web Service truy xuất CSDL.

Bây giờ ta thử phát triển 1 ứng dụng JSP Web Client truy xuất Web Service này, xem ở đây

Tạo Web services với JAX-WS 2.0 và Java SE 6 Platform

Phần 1: tạo Web Service và publish nó

Tạo lớp .java với nội dung

package hello;

import javax.jws.WebService;

@WebService

public class CircleFunctions {
public double getArea(double r) {
return java.lang.Math.PI * (r * r);
}

public double getCircumference(double r) {
return 2 * java.lang.Math.PI * r;
}
}

Chúng ta có thể dùng phương thức publish() của lớp javax.xml.ws.Endpoint để publish lớp vừa tạo thành 1 web service trong context root được chỉ định. Ở đây ta tạo lớp PubWS.java có nội dung sau:

package hello;
import javax.xml.ws.Endpoint;
public class PubWS{
public static void main(String[] args) {
Endpoint.publish(
"http://localhost:8080/WebServiceExample/circlefunctions",
new CircleFunctions());
}
}

Bây giờ ta hãy compile source code bằng lệnh sau
javac -d . *.java

Phải đảm bảo rằng không có lỗi nào xảy ra.

Sau đó ta dùng tool wsgen để sinh ra các lớp hỗ trợ cho việc tạo webservice cũng như sinh WSDL file đồng thời public lên Web Server, cú pháp như sau:
wsgen -cp . hello.CircleFunctions

Bây giờ ta có thể public webservice bằng cách thự thi lệnh sau:
java hello.PubWS

Như vậy ta đang có 1 webservice đang được thực thi trên web server.
Mở trình duyệt, duyệt đến địa chỉ http://localhost:8080/WebServiceExample/circlefunctions?WSDL ta sẽ có kết quả như hình:
Ws_J2SE



Và, chúng ta có thể viết client ứng dụng web service này được rồi đó!
Phần 2: Tạo client

1. Tạo thư mục client, mở cửa sổ command, chạy lệnh sau để sinh ra các lớp đặc tả dịch vụ web service từ WSDL URL
wsimport http://localhost:8080/WebServiceExample/circlefunctions?WSDL

2. Tạo lớp client với nội dung sau

import hello.*;
import javax.xml.ws.WebServiceRef;
public class Client{
@WebServiceRef(wsdlLocation = "http://localhost:8080/WebServiceExample/circlefunctions?WSDL")
public static void main(String[] args){
Client client = new Client();
client.test();
}
public void test(){
try{
CircleFunctionsService service=new CircleFunctionsService();
CircleFunctions port =service.getCircleFunctionsPort();
double ret =port.getArea(3.0d);
System.out.println("Area result = " + ret);
}catch(Exception e){
e.printStackTrace();
}
}
}

3. Biên dịch file Client.java bằng lệnh sau
javac Client.java

Bạn chắc là không có lỗi nào xảy ra chứ?
4. Thực thi ứng dụng
java Client

Kết quả thu được của bạn như sau
D:\Bai giang Aptech\Aptech – Semester 4\XML Webservices with Java\examples\ex03_
JAX_WS\client>java Client
Area result = 28.274333882308138

D:\Bai giang Aptech\Aptech – Semester 4\XML Webservices with Java\examples\ex03_
JAX_WS\client>pause
Press any key to continue . . .
Chúc thành công!