Java and flex Study Notes (21) -- WebService for communication between flex and Java

Source: Internet
Author: User

Today, the third method of communication between flex and Java is finally completed. I didn't figure it out because I don't know how to create a WebService. The free weather forecast service is used on the Internet. All right, you just want to learn this communication style for weather forecast, but I am not sure. Fortunately, we finally got it out today.


We know that flex and Java have three communication methods:


1. Flex communicates with common Java classes through remoteobject.


2. Flex communicates with the server class through httpservice, such as servlet.


3. Flex communicates with Java through WebService.


I will not talk about WebService, but flex uses WebService to communicate with Java in a very simple way.


Create a WebService address.


Create a web project webservicedemo in eclipse and create a loginservice. Java class in it. The Code is as follows:


package com.ldfsoft.service; public classLoginService {    public boolean login(Stringusername,String passworld){       boolean result=false;       if("admin".equals(username)&&"123".equals(passworld)){           result=true;       }       return result;    }}

 

This class has a method login (), which is very simple.


Right-click the class and choose WebService à ---> creata web service, as shown in:




Select the default configuration and click "finish". eclipse will automatically create a WebService address for us.


Next we will test the WebService we just created. In the address bar, enter http: // localhost: 8000/webservicedemo/services/loginservice? WSDL. If the page is shown below, the configuration is successful:




Then, use WebService to communicate in flex.


Create a new Flex page webservicedemo. mxml with the following code:


<? XML version = "1.0" encoding = "UTF-8"?> <S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx "minwidth =" 955 "minheight =" 600 "pagetitle =" WebService test "> <FX: style> @ namespace s" Library: // ns.adobe.com/flex/spark "; @ namespace MX "Library: // ns.adobe.com/flex/mx ";. TXT {fontsize: 18 ;}</FX: style> <FX: SCRIPT> <! [CDATA [import MX. controls. alert; importmx. RPC. events. faultevent; importmx. RPC. events. resultevent; protected function operation=resulthandler (Event: resultevent): void {// todo auto-generated method stub varresult: Boolean = event. result as Boolean; If (result) {alert. show ("Login successful ~ "," Prompt ");} else {alert. Show (" Logon Failed ~ "," Prompt ") ;}} protected functionoperation=faulthandler (Event: faultevent): void {// todo auto-generated method stub alert. show (event. fault. message, "prompt");} protected functionlogin_clickhandler (Event: mouseevent): void {// todo auto-generated method stub // send request webser. login. send () ;}]]> </FX: SCRIPT> <FX: declarations> <! -- Note that the number and sequence of parameters in WebService must be the same as those in the publishing address --> <s: WebService id = "webser" WSDL = "http: // localhost: 8000/webservicedemo/services/loginservice? WSDL "> <s: Operation name =" login "result =" operationtransferresulthandler (event) "fault =" operationtransferfaulthandler (event) "> <s: Request> <username> {username. text} </username> <passworld> {password. text} </passworld> </S: Request> </S: Operation> </S: WebService> </FX: declarations> <s: label x = "351" Y = "158" text = "account:" fontsize = "18" stylename = "TXT"/> <s: textinput x = "439" Y = "154" id = "username" stylename = "TXT"/> <s: label x = "351" Y = "201" text = "Password:" fontsize = "18" id = "password_txt" stylename = "TXT"/> <s: textinput x = "441" Y = "196" id = "password" stylename = "TXT" displayaspassword = "true"/> <s: button x = "461" Y = "273" Height = "34" label = "login" fontsize = "18" id = "login" stylename = "TXT" Click = "login_clickhandler (Event) "/> </S: Application>

Next, run the page. If the user name is "admin" and the password is "123" and the login is successful, otherwise, the communication is successful.




Note: If you understand WebService, you will understand that they send data by assembling the data into XML. In this example, the WebService is created in the webservicedemo project, while the flex page is in another project. We can know that WebService can be a bridge for communication between different projects!


Original article, reproduced please indicate the source: http://www.dianfusoft.com/




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.