Interaction between client and server implemented by GWT

Source: Internet
Author: User

Environment: GWT 2.2 + eclipse 3.6

Project name: personrpc

Package name: COM. chuyue. RPC

 

1. client code:

 

Import com. Google. GWT. User. Client. rpc. remoteservice;
Import com. Google. GWT. User. Client. rpc. remoteservicerelativepath;

@ Remoteservicerelativepath ("person") // This cannot be small
Public interface myservice extends remoteservice {
Public Person getperson (string name, int age) throws exception;
}

 

Import com. Google. GWT. User. Client. rpc. asynccallback;

Public interface myserviceasync {

Void getperson (string name, int age, asynccallback <person> callback );
 
}

 

Import com. Google. GWT. User. Client. rpc. isserializable;

Public class person implements isserializable {
Private string name;
Private int age;
 
Public Person (){

}
Public Person (string name, int age ){
Super ();
This. Name = Name;
This. Age = age;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public int getage (){
Return age;
}
Public void setage (INT age ){
This. Age = age;
}
 
}

 

 

Import com. Google. GWT. Core. Client. entrypoint;
Import com. Google. GWT. Core. Client. GWT;
Import com. Google. GWT. event. Dom. Client. clickevent;
Import com. Google. GWT. event. Dom. Client. clickhandler;
Import com. Google. GWT. User. Client. window;
Import com. Google. GWT. User. Client. rpc. asynccallback;
Import com. Google. GWT. User. Client. UI. Button;
Import com. Google. GWT. User. Client. UI. rootpanel;
Import com. Google. GWT. User. Client. UI. textbox;

Public class personrpc implements entrypoint {
 
Private Static final myserviceasync myservice = GWT. Create (myservice. Class );
 
Public void onmoduleload (){

Rootpanel = rootpanel. Get ();

Final textbox namefield = new Textbox ();
Final textbox agefiled = new Textbox ();
Final button sendbtn = new button ("send ");

Namefield. settext ("zhangsan ");
Agefiled. settext ("12 ");


Final string namevalue = namefield. gettext ();
String agevalue = agefiled. gettext ();

Integer iage = new INTEGER (agevalue );
Final int ageval = iage. intvalue ();

Class myhander implements clickhandler {

@ Override
Public void onclick (clickevent event ){
Sendserver ();
}

Private void sendserver (){
Myservice. getperson (namevalue, ageval, new asynccallback <person> (){
@ Override
Public void onsuccess (person result ){
// Todo auto-generated method stub
Window. Alert (result. getname () + "" + result. getage ());
}

@ Override
Public void onfailure (throwable caught ){
// Todo auto-generated method stub
Window. Alert ("error result ");
}
});
}

}

Myhander = new myhander ();
Sendbtn. addclickhandler (myhander );


Rootpanel. Add (namefield );
Rootpanel. Add (agefiled );
Rootpanel. Add (sendbtn );
}
}

 

2. server code:

Import com. chuyue. rpc. Client. myservice;
Import com. chuyue. rpc. Client. person;
Import com. Google. GWT. User. server. rpc. remoteserviceservlet;

@ Suppresswarnings ("serial ")
Public class myserviceimpl extends remoteserviceservlet implements myservice {

@ Override
Public Person getperson (string name, int age) throws exception {
Person = new person ();
Person. setname (name + "aaaaaaaaaaa ");
Person. setage (age + 100 );
Return person;
}

}

 

3. configuration file: personrpc. GWT. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Module rename-To = 'personrpc '>
<Inherits name = 'com. Google. GWT. User. user'/>

<Inherits name = 'com. Google. GWT. User. theme. Standard. standard'/>

<Entry-point class = 'com. chuyue. rpc. Client. personrpc '/>

<Source Path = 'client'/>
<Source Path = 'shared '/>

</Module>

 

4. The configuration of Web. XML is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>

<Web-app>

<! -- Servlets -->
<Servlet>
<Servlet-Name> myserviceimpl </servlet-Name>
<Servlet-class> com. chuyue. rpc. server. myserviceimpl </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> myserviceimpl </servlet-Name>
<URL-pattern>/personrpc/person </url-pattern>
</Servlet-mapping>

<! -- Default page to serve -->
<Welcome-file-List>
<Welcome-File> personrpc.html </welcome-File>
</Welcome-file-List>

</Web-app>

 

 

 

This is a simple demo that enables interaction between the client and the server.

 

 

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.