Tạo JSP Web Service Client
Bài này có sử dụng kết quả của bài Web Service truy xuất cơ sở dữ liệu.
Giả sử chúng ta có Web Service Logon và địa chỉ đến file WSDL là http://localhost:8086/WS_DB/services/LogonService?wsdl. Bây giờ ta sẽ học cách tạo các trang JSP đơn giản theo đặc tả sau:
Trang index.jsp phải có 2 text box cho người dùng gõ user name và password, 1 nút nhấn cho việc đăng nhập và 1 nút nhấn cho việc reset. Nếu người dùng cung cấp username & password đúng so với csdl thì trang sẽ được chuyển sang trang success.jsp, còn không thì chuyển sang trang logonfailed.jsp. Trang succes.jsp sẽ hiện lên 1 message thông báo chào mừng bạn đã logon thành công. Trang loginfailed.jsp sẽ có 1 thông báo lỗi truy cập và cung cấp 1link để quay trở lại trang index.jsp.
1. Trong Eclipse, ta tạo 1 project mới có kiểu project là “Dynamic Web Project”. Đặt tên cho nó là JSP_WS_Client. Chọn Server là Tomcat 6.
2.Tham chiếu đến Web Service ta đã tạo bằng cách nhấn chuột phải lên project, chọn New->Others, chọn Web Service Client như hình

Nhấn Next.

Đưa WSDL URL của chúng ta vào ô Service Definition. Nhấn Finish. Eclipse sẽ tự động sinh cho chúng ta các lớp LogonServiceCallbackHandler.java và LogonServiceStub.java như hình

3. Tạo 1 trang JSP mới bằng cách nhấn phải chuột lên Project, chọn New->JPS, đặt tên cho nó là index.jsp. Thiết kế nó với code jsp như sau
| <?xml version=”1.0″ encoding=”ISO-8859-1″ ?><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml”> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″ /> <title>Welcome to My Login page</title> </head> <body> <form action=”process.jsp”> <table align=”center”> <tr> <td align=”right”>User Name:</td> <td><input type=”text” name=”us”/> </td> </tr> <tr> <td align=”right”>Password:</td> <td><input type=”password” name=”psw”/> </td> </tr> <tr> <td><input type=”submit” title=”Logon”/> </td> <td><input type=”reset” title=”Reset”/> </td> </tr> </table> </form> </body> </html> |
4.Thêm vào trang process.jsp có nội dung sau
| <%@page import=”vovanhai.wordpress.com.LogonServiceStub”%><% try{ String us=request.getParameter(“us”); String psw=request.getParameter(“psw”); LogonServiceStub stub=new LogonServiceStub(); LogonServiceStub.Logon lg=new LogonServiceStub.Logon(); lg.setUserName(us); lg.setPassword(psw); LogonServiceStub.LogonResponse rp= stub.Logon(lg); int kq=rp.get_return(); if(kq==1) response.sendRedirect(“Success.jsp”); else response.sendRedirect(“logonfailed.jsp”); }catch(Exception ex){ ex.printStackTrace(); } %> |
5. Thêm vào trang Success.jsp có nội dung sau
| <?xml version=”1.0″ encoding=”ISO-8859-1″ ?> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml”> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″ /> <title>Insert title here</title> </head> <body> <h1>Welcome!</h1> <h2>You are logged in successfully</h2> </body> </html> |
6. Thêm vào trang logonfailed.jsp có nội dung sau
| <?xml version=”1.0″ encoding=”ISO-8859-1″ ?> <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml”> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″ /> <title>Insert title here</title> </head> <body> <h1>Failed</h1> <h3> Click <a href=”index.jsp”> here </a>to try another times! </h3> </body> </html> |
7. Chạy thử ứng dụng
Nhấn chuột phải lên trang index.jsp, chọn Run as->Run on Server, chọn Tomcat 6, nhấn Finish, kết quả như sau

Khi nhập username, password đúng, kết quả như sau

Khi nhập username hoặc password sai, kết quả như sau

Bạn có thể dùng ASP.Net truy xuất đến java web service này như ví dụ tại đây
Chúc các bạn thành công!
dong said
OK.
Nhưng khi tạo như vậy, đường link của service ví dụ :
http://192.168.117.12:8086/WS_DB/services/LogonService?wsdl
sẽ dính chặt vào file class.
Làm cách nào để chuyển nó ra XML?. Thanks. Đừng dùng tool
hong ha said
hi anh! em cũng làm như anh thế mà server em là glassfish nhưng khi tạo ra client thì tạo ra 5 file lun: HelloWorld.java,HelloWorldPortBindingStub,HelloWorldProxy,HelloWorldservice,HelloWorldLocator.Như vậy viết vào trang jsp như thế nào để lấy được service HelloWorld.!Cảm ơn anh !
Mong anh trả lời giúp !
Tai said
E chào thầy, lần đầu e làm với jsp client thì ok, từ từ lần sau trở đi thì bị lỗi này: “CalculatorStub.CalculateResponse cannot be resolved or is not a type”. Mong thầy fix lỗi dùm e nhé. Thks.
Tran Chinh said
Thầy ơi em đã làm theo bài hướng dẫn này nhưng em gặp lỗi ngay khi tạo trang process.jsp nó ko import duoc và vì thế các khai báo cũng ko có hiệu quả luôn.
Tran Chinh said
hjhj em da phat hien duoc loi~ rui` thay oi.
vi hom truoc em ko doc ky~: thay bao~ tao class LogonService.java nhung thay lai viet la LongonService.java nhung khi tao thi thay lai tao LogonService.java
vi em ko doc ky~ nen em copy LongonService.java vi the em da tao class LongonService.java nen khi import …LogonService.java no bao loi~ hjhj
dinh said
thay oi co cach nao de su dung prime face trong trang jsp duoc ko vay.e tao trang jsp nhung khong the cua prime face duoc.mog thay chi dum e.
Võ Văn Hải said
“e su dung prime face trong trang jsp…”
Em phải thêm thư viện PrimeFace vào project của em.
Ngọc Quang said
Anh vui lòng cho minh hỏi nếu làm theo cách tao webservice client như trên thì khi mình viết thêm function ở web service thì muốn update lại web service client phải như thế nào (mình có đọc môt bài Consume a .NET Web Service with Axis2 tools của anh) và nếu duoc anh có thể viết một bài client truy xuất web service đơn giản theo bài viết Consume a .NET Web Service with Axis2 tools đuợc không anh? Trân thành cảm ơn
halinh said
thưa thấy, em làm như hướng dẫn và đã tạo dc file wsdl, nhưng khi chạy ở client nó lại cứ báo là ko tìm thấy lớp com.mysql.jdbc.Driver mặc dù em đã add file Driver này vào thư viện và em cũng đã test file này với ứng dụng khác. thầy có thể có em hỏi, làm như trên có chú ý gì khi add thư viện để truy xuất vào database mysql không ạ. Mong thầy sớm hồi âm. em cảm ơn thầy nhiều.
halinh said
ah, em không dùng cầu nối odbc mà em dùng cái thư việ Driver down về rùi add vào thư viện, thầy xem giúp em
String selectStatement = “select * from user where username=’”+userName+”‘”;
System.out.println(selectStatement);
try{
Class.forName(“com.mysql.jdbc.Driver”);
con = (Connection) DriverManager.getConnection(“jdbc:mysql://localhost:3306/login”, “root”, “”);
stm = (Statement) con.createStatement();
rs = stm.executeQuery(selectStatement);
if(rs==null){
result=-1;//sai user name
}else{
if(rs.next()){
String psw = rs.getString(“passw”);
result=1;//thanh cong
}else {
result=0;//sai pass
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;