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)








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?
Pingback: Hacking so Existing » Blog Archive » Implementing Gmail-alike Web Mail Client by Java2Script
ibuprofen is a good pain reliever but i heard that this one have nasty side effects too `"~
"-` I am really thankful to this topic because it really gives great information -~`
With amzing grace! Just fanciful! Your composition manner is admirable and the way you covered the subject with grace is notable. I'm intrigued, I assume you happen an authority on this subject. I am subscribing to your forthcoming updates from now on.
Amazingly, your piece goes to the heart of the subject. Your readability leaves me desirous to know more. Just so you already, i'll immediately seize your feed to keep up to this point with your online weblog. Sounding Out thanks is merely my little way of saying what a masterpiece for a fantastic resource. Take On my greatest needs for your next post.
I won't debate along with your endings simply believe you are exact on the money! You've assemble a logical situation for your ideas and now I know particulars on this extraordinary subject. Value your this supreme post and i will arrive back much more.
It was a really nice idea! Just wanna say thank you for the information you have fanned. Just continue publishing this kind of post. I will be your loyal reader. Thanks once more.
I agree with you comment fried!
39. you're really a good webmaster. The site loading speed is incredible. It seems that you are doing any unique trick. Moreover, The contents are masterpiece. you've done a great job on this topic!
Pingback: HCG Levels in Early Pregnancy
My coder is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he's tryiong none the less. I've been using Movable-type on several websites for about a year and am worried about switching to another platform. I have heard good things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any help would be greatly appreciated!
Pingback: how to make money at 13
Pingback: Web.com Coupon Code
Pingback: Pay Per Click Campaigns