how to use GWT code in JSP page

443 views
Skip to first unread message

Syed

unread,
Jul 11, 2006, 1:41:22 AM7/11/06
to Google Web Toolkit
i am trying to write an application using GWT that will open a jsp page
when i click on a node of tree. i used StackPanel and Tree with the
help of Mail example that is included in the samples of GWT. now i want
that JSP to use GWT as well. it is very simple. that JSP will have a
button on it and a modal dialog will be shown to the user when he
clicks on the button. but that means i have to write GWT code in the
JSP. i am really confused how to do that. can someone give me a very
simple example how to embed GWT code into JSP pages. i will be thankful.

拉尔夫

unread,
Jul 11, 2006, 5:39:26 AM7/11/06
to Google Web Toolkit
GWT is a toolkit ,with this tool help we can write javascript code in
java language .When we try to deploy this application onto any
webserver , We must compiling java coding program into javascript.so
the client can download the javascript and run it in client container.
if you want to call business writed in jsp which hosted in any j2ee
server,you must switch program to RPC.Jsp act Server RPC program and
the javascript act as Client RPC.

Syed

unread,
Jul 13, 2006, 10:30:44 AM7/13/06
to Google Web Toolkit
thanks for ur help. meanwhile i have done the job :) i have made a very
simple application that gets a string message from the server and shows
it in browser and i have used that in JSP. its quite simple as we just
have to add a meta tag and a script tag in our jsp page. if someone
needs help, do contact me. i dont know if there is any option to upload
files here otherwise i would have uploaded here.

Syed

unread,
Jul 14, 2006, 1:42:53 AM7/14/06
to Google Web Toolkit
Client Side Code:
(TestServer.java)
package com.dps.hrms.rpcservices.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class TestServer implements EntryPoint {

final TestServerServiceAsync calservice = (TestServerServiceAsync)
GWT.create(TestServerService.class);;
final Button button = new Button("Click Here");
final Label label = new Label("Message From Client");

public void onModuleLoad() {


ServiceDefTarget target = (ServiceDefTarget)calservice;
String hostURL = GWT.getModuleBaseURL();
// get contextpath from request object to use here .
hostURL = hostURL + "/test";
target.setServiceEntryPoint(hostURL);

button.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
calservice.getString(callback);
}
});
RootPanel.get("buttonSlot").add(button);
RootPanel.get("labelSlot").add(label);
}


private AsyncCallback callback = new AsyncCallback() {
public void onSuccess(Object result){
String s = (String)result;
label.setText("Message From Server");

}

public void onFailure(Throwable caught) {
caught.printStackTrace();
}

};


}


---------------------------------------------------------------------------------
(TestServerService.java)
package com.dps.hrms.rpcservices.client;

import com.google.gwt.user.client.rpc.RemoteService;

public interface TestServerService extends RemoteService {
public String getString();

}

--------------------------------------------------------------------------------------------
(TestServerServiceAsync.java)
package com.dps.hrms.rpcservices.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface TestServerServiceAsync {
public void getString(AsyncCallback callback);

}

-----------------------------------------------------------------------------------
(on server side "TestServerServiceImpl.java")
package com.dps.hrms.rpcservices.server;

import com.dps.hrms.rpcservices.client.TestServerService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;


public class TestServerServiceImpl extends RemoteServiceServlet
implements TestServerService {

public String getString () {
// System.out.println(this.getThreadLocalRequest().getContextPath()+"context
path");
return "Message From Server";
}


}


-------------------------------------------------------------------------------------------------

(Using all that in my JSP)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta name='gwt:module'
content='gwt/com.dps.hrms.rpcservices.TestServer/com.dps.hrms.rpcservices.TestServer'
/>


</head>

<body>
<script type="text/javascript" language='javascript'
src='gwt/com.dps.hrms.rpcservices.TestServer/gwt.js' ></script>
<h1> <div align="center"> Testing RPC </div> </h1> <hr>
<table border="1" width="50%" align="center">

<tr>
<td id="buttonSlot"> </td>
<td id="labelSlot"> </td>
</tr>
</table>

</table>
</body>
</html>

-------------------------------------------------------------------------------------------
(Mapping for the servlet in web.xml file)

<servlet>
<servlet-name>TestServerServiceImpl</servlet-name>
<servlet-class>com.dps.hrms.rpcservices.server.TestServerServiceImpl</servlet-class>

</servlet>

<servlet-mapping>
<servlet-name>TestServerServiceImpl</servlet-name>
<url-pattern>/gwt/com.dps.hrms.rpcservices.TestServer/test</url-pattern>

</servlet-mapping>

------------------------------------------------------------------------------------

hipp...@gmail.com

unread,
Jul 21, 2006, 10:34:54 PM7/21/06
to Google Web Toolkit
great

Reply all
Reply to author
Forward
0 new messages