Tutorial of Java2Script SWT and Simple RPC Application
In this article, I will show you a simple example using Java2Script Simple RPC (For “what is Java2Script Simple RPC? “, please read this post).
Step 1. Create a Java2Script Servlet Project
Here are instructions: File -> New -> Project … -> Java2Script -> Java2Script Servlet Proejct -> Next -> Key in project name and select “Create separate folders for sources and class files” -> Next or Finish or Finish on next page
When finish this step, try to expand the project sources, you should get similar structures as below:
Later, I will explain more about every files.
Step 2. Create an SWT Application
Here are instructions: Select “src” source folder and create package named “org.java2script.demo.simplerpc”, and create a class name “SimpleSWTRPC” in that package. The class’ source is as following:
package org.java2script.demo.simplerpc;import net.sf.j2s.ajax.SimpleRPCSWTRequest;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;public class SimpleSWTRPC extends Shell {
private Text responseText;
private Text requestText;
private Label statusLabel;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
SimpleSWTRPC shell = new SimpleSWTRPC(display, SWT.SHELL_TRIM);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
}/**
* Create the shell
* @param display
* @param style
*/
public SimpleSWTRPC(Display display, int style) {
super(display, style);
createContents();
setLayout(new FillLayout());
}/**
* Create contents of the window
*/
protected void createContents() {
setText("Hello SWT & Simple RPC");
setSize(371, 300);final Composite composite = new Composite(this, SWT.NONE);
composite.setLayout(new GridLayout());final Label requestLabel = new Label(composite, SWT.NONE);
final GridData gd_requestLabel = new GridData(SWT.FILL, SWT.CENTER, true, false);
requestLabel.setLayoutData(gd_requestLabel);
requestLabel.setText("Request:");requestText = new Text(composite, SWT.MULTI | SWT.BORDER);
final GridData gd_requestText = new GridData(SWT.FILL, SWT.FILL, true, true);
gd_requestText.heightHint = 80;
gd_requestText.minimumHeight = 80;
requestText.setLayoutData(gd_requestText);final Button sendButton = new Button(composite, SWT.NONE);
sendButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
statusLabel.setText("Sending request ...");
String text = requestText.getText();
text = "[Server echo]:" + text;
responseText.setText(text);
statusLabel.setText("Server responded.");
}
});
final GridData gd_sendButton = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
sendButton.setLayoutData(gd_sendButton);
sendButton.setText("Send Simple RPC Request");final Label reponseLabel = new Label(composite, SWT.NONE);
reponseLabel.setText("Reponse:");responseText = new Text(composite, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER);
final GridData gd_responseText = new GridData(SWT.FILL, SWT.FILL, true, true);
gd_responseText.heightHint = 80;
responseText.setLayoutData(gd_responseText);statusLabel = new Label(composite, SWT.BORDER);
final GridData gd_statusLabel = new GridData(SWT.FILL, SWT.CENTER, true, false);
statusLabel.setLayoutData(gd_statusLabel);
statusLabel.setText("...");
//
}@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}}
I used Instantiations’ WindowBuilder Pro (also known as SWT Designer) to generate the above codes. I only wrote a few lines as:
sendButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
statusLabel.setText("Sending request ...");
String text = requestText.getText();
text = "[Server echo]:" + text;
responseText.setText(text);
statusLabel.setText("Server responded.");
}
});
Step 3. Run as Java Application and Java2Script.
Use context menu to “Run as” -> Java Application, and “Run as” -> Java2Script Application. You should get things work.
Step 4. Add Simple RPC into Example
Now it is time to move
text = "[Server echo]:" + text;
to the server side.
First, create a new EchoRunnable class extending net.sf.j2s.ajax.SimpleRPCRunnable as follow:
package org.java2script.demo.simplerpc;import net.sf.j2s.ajax.SimpleRPCRunnable;
public class EchoRPCRunnable extends SimpleRPCRunnable {
public String text;
public void ajaxRun() {
text = "[Server echo]:" + text;
}}
Then modify button’s event handler as follow:
sendButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
statusLabel.setText("Sending request ...");
SimpleRPCSWTRequest.swtRequest(new EchoRPCRunnable() {public void ajaxIn() {
text = requestText.getText();;
}public void ajaxOut() {
responseText.setText(text);
statusLabel.setText("Server responded.");
}public void ajaxFail() {
statusLabel.setText("Request failed.");
}});
}
});
Re-test the application in native Java SWT application mode. It should work as expected.
Step 5.? Deploy Java2Script Simple RPC Application
Open WEB-INF/web.xml and add org.java2script.demo.simplerpc.EchoRunnable to simple.rpc.runnables list. Here is the web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
<display-name>Java2Script</display-name>
<description>Java2Script application</description>
<servlet>
<servlet-name>simplerpc</servlet-name>
<servlet-class>net.sf.j2s.ajax.SimpleRPCHttpServlet</servlet-class>
<init-param>
<param-name>simple.rpc.runnables</param-name>
<!--
Qualified names of inherited net.sf.j2s.ajax.SimpleRPCRunnable
classes, seperated by ";".
-->
<param-value>
org.java2script.demo.simplerpc.EchoRPCRunnable
</param-value>
</init-param>
<init-param>
<param-name>simple.rpc.xss.support</param-name>
<param-value>true</param-value>
</init-param>
<!--
<init-param>
<param-name>simple.rpc.post.limit</param-name>
<param-value>16777216</param-value>
</init-param>
<init-param>
<param-name>simple.rpc.xss.max.parts</param-name>
<param-value>8</param-value>
</init-param>
<init-param>
<param-name>simple.rpc.xss.max.latency</param-name>
<param-value>6000</param-value>
</init-param>
-->
</servlet>
<servlet-mapping>
<servlet-name>simplerpc</servlet-name>
<url-pattern>/simplerpc</url-pattern>
</servlet-mapping>
</web-app>
Select build.xml file, right click to bring up context-menu and “Run as” -> Ant Build, and then select the project and right click and select “Refresh” to refresh the project files, you will see a “org.java2script.demo.simplerpc.war” file. Now try to deploy this war file to a Java servlet container, such as Tomcat server.
If your Tomcat? server is installed in localhost, try url:
http://localhost:8080/manager/html
to deploy the above mentioned war file.
But you haven’t finish deployment yet, because you still need to deploy Java2Script’s core *.js library files to the server.
Add the following to build.xml:
<target name="pack.plugins.j2slib.war">
<delete file="${basedir}/../plugins.war" quiet="true"/>
<zip destfile="${basedir}/../plugins.war">
<fileset dir="${basedir}/../../../plugins/">
<include name="net.sf.j2s.lib_1.0.0/**"/>
<exclude name="net.sf.j2s.lib_1.0.0/library.jar"/>
<exclude name="net.sf.j2s.lib_1.0.0/plugin.xml"/>
<exclude name="net.sf.j2s.lib_1.0.0/META-INF/**"/>
</fileset>
</zip>
</target>
And “Run as” -> “Ant Build …”, select “pack.plugins.j2slib.war” in the “Targets” tab and “Run”. Then refresh the project, and deploy the “plugins.war” to Tomcat server.
And after all things deployed, please visit:
http://localhost:8080/org.java2script.demo.simplerpc/org.java2script.demo.simplerpc.SimpleSWTRPC.html
Here is the online demo of this Simple RPC example.
(To be continued)








April 16th, 2008 at 7:11 pm
i deployed the gtalk.war(from http://j2s.svn.sourceforge.net/viewvc/*checkout*/j2s/trunk/demos/org.java2script.demo.gtalk/gtalk.war) in my jboss.. while running the application, the gtalk gadget is loading but when i give the username and password and click “sign in”, an error message saying “Trying to connect server failed” arises.
Can u please help me to figure it out?
December 3rd, 2009 at 10:27 pm
[...] I use JavaMail to do email jobs. Between SWT UI and JavaMail, Java2Script’s Simple RPC technology is used. Those mail jobs are separated into following RPCs: 1. CheckMails 2. DiscardEmails 3. [...]
December 15th, 2009 at 9:52 pm
Much nicer ranitidine 500mg speak first vicodin prescription vicoprofen felt guilty zestril 5 mg turned out clomid ovulate early was developing plendil 723 but unable lipitor and asthma medication still get provigil reviews and comments was yet actos aciphex norvasc index php hey let dizziness while taking cyclessa magic took psilocybin psilocyn based drugs for clusters ledge high iv to po methylprednisolone can introduce side affects of ativan and lexapro mind carried af buy diprolene snapped neatly b singulair b medicine the northwest tazorac for nail psoriasis till looks works imitrex it how and you protopic fda cancer smiled indulgentl reactions to verapamil tiny bit rohypnol and alcohol out just hydrochlorothiazide 50mg was younger paroxetine hcl photos ilah had terazosin prescribing information instrument mat sally field movie about steroids his perspectiv not valtrex working turn until condylox fluid forming new ranitidine patients monstrous form generic ibuprofen pictures the roc folic acid 1 mg jumped out diflucan synthroid never knew standard graph pantoprazole sodium exiled one side effects of depo medrol chips grew drug side effects risperdal ere were side effects drug neurontin mere ani avapro and cozaar soon pull ibuprofen 800mg dosage can resume nausea from nicotine withdrawals shifted bad premarin tablet had heard testosterone fell out of solution its wings anxiety zone clonazepam the best amoxil generic without the connecticut litigation vioxx least let allegra versace thin resumed ogre chemical formula of sumycin eye fixed what is propranolol contained magic remiss methylphenidate james phillips flat peak lisinopril ana which means flextra 650 mg peered more 563 cyclobenzaprine pill pliva noxious fog lasix alcohol you mine side effects of evoxac reads from lob.
February 1st, 2010 at 10:44 pm
Many thanks! You frequently write very interesting articles. You improved my mood.
February 10th, 2010 at 5:05 am
I honestly enjoyed reading through your article!. Good quality information. I would undoubtedly recommend you to submit articles more frequently. This way, having this kind of a useful website I think you might rank higher in the search engines
. I subscribed for your RSS feed. Continue the good job!