GWT is added to the server code to return data from the server

Source: Internet
Author: User


1. Create a remote

The client already exists, and the server code must be created again. The GWT is also remote, similar to the client

 

Right-click the project, choose new --> other, and select GWT remote service under cypal studio, as shown below:


After finishing, check the project directory. The loginserviceimpl class exists in COM. mooing. server, that is, our server servlet, and this servlet information is also injected into web. xml.

The project code structure is as follows:


 

For Asynchronous calls, the client must define an interface that inherits the remoteservice interface. In our example, open the created loginservice and we can see that the remoteservice interface has been inherited, this is automatically generated when you create the instance;

 

2. Next we add a simulated login verification method checklogin () on the client, as shown below:

PackageCom. mooing. client;

 ImportCom. Google. GWT. Core. Client. GWT;

ImportCom. Google. GWT. User. Client. rpc. remoteservice;

ImportCom. Google. GWT. User. Client. rpc. remoteservicerelativepath;

@ Remoteservicerelativepath ("loginaction ")

PublicinterfaceLoginserviceExtendsRemoteservice
{

BooleanChecklogin ();

Publicstaticclass
Util {

PublicstaticLoginserviceasync
Getinstance (){

ReturnGWT.Create(Loginservice.Class);

}

}

}

 

3. After saving, we will see that this method is automatically added in loginserviceasync;

PackageCom. mooing. client;

 ImportCom. Google. GWT. User. Client. rpc. asynccallback;

 PublicinterfaceLoginserviceasync {

VoidChecklogin (Asynccallback<Boolean> callback );

}

 

 

After the client defines the interface, we must implement this interface on the server. In our example, the loginserviceimpl class implements the interface loginservice, at the same time, you will notice that loginserviceimpl also inherits the remoteserviceservlet class, while remoteserviceservlet is a subclass of httpservlet, so that our client requests can be submitted to loginserviceimpl. The loginserviceimpl code is as follows:

 

PackageCom. mooing. server;

 ImportCom. mooing. Client. loginservice;

ImportCom. Google. GWT. User. server. rpc. remoteserviceservlet;

 PublicclassLoginserviceimplExtendsRemoteserviceservletImplements
Loginservice {

PrivatestaticfinallongSerialversionuid
= 418668644816301168l;

PublicString checklogin (){

Return"This string from server ";

}

}

 

 

4. Finally, we need to create a client test class. The method above creates a new client module class called logintest. In this project, we use this class for asynchronous calls to the server. The Code is as follows:

 

PackageCom. mooing. client;

 

ImportCom. Google. GWT. Core. Client. entrypoint;

ImportCom. Google. GWT. Core. Client. GWT;

ImportCom. Google. GWT. User. Client. rpc. asynccallback;

ImportCom. Google. GWT. User. Client. rpc. servicedeftarget;

ImportCom. Google. GWT. User. Client. UI. Button;

ImportCom. Google. GWT. User. Client. UI.Clicklistener;

ImportCom. Google. GWT. User. Client. UI. label;

ImportCom. Google. GWT. User. Client. UI. rootpanel;

ImportCom. Google. GWT. User. Client. UI. widget;

 

PublicclassLogintestImplements
Entrypoint {

PublicvoidOnmoduleload ()
{

FinalLoginserviceasync loginservice = (loginserviceasync)
GWT.Create(Loginservice.Class);

Servicedeftarget target = (servicedeftarget) loginservice;

String staticresponseurl = GWT.Getmodulebaseurl();

Staticresponseurl + = "/loginaction ";

Target. setserviceentrypoint (staticresponseurl );

FinalLabel Label =New
Label ();

FinalButtonbutton =New
Button ("Get string ");

Button.Addclicklistener(NewClicklistener ()
{

PublicvoidOnclick (widget sender ){

Loginservice. checklogin (NewAsynccallback ()
{

PublicvoidOnsuccess (Object result)
{

Label. settext (string) result );

}

 PublicvoidOnfailure (throwable caught)
{

Label. settext (caught. getmessage ());

}

});

}


})
;

Rootpanel.Get(). Add (button );

Rootpanel.Get(). Add (Label );

}

}

 

Note: you must add logintest. GWT. xml

<Servletclass = "com. mooing. server. loginserviceimpl "Path ="/loginaction "/>. In the code, staticresponseurl + ="/loginaction "; corresponds to the path and web. XML.

 

Run the test. During this operation, the module must configure the logintest client.


After you click "run", the returned string is displayed. All Asynchronous interaction processes of GWT have been completed.

So far, the client requests and the server returns data. Although it is a simple sentence, it represents the entire GWT process. Next, you need to be familiar with various GWT controls, panels, styles, and so on.


References:

Http://wenku.baidu.com/view/76a6b46c58fafab069dc02bf.html

Http://www.doc88.com/p-316736407370.html




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.