gwt Client-Server 交互原形模型
DealWith.java
清楚上面的模型,就会更加理解GWT RPC(Remote Procedure Calls)的几个步骤:
- <!--web.xml-->
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <display-name>ccgwt</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>dealwith</servlet-name>
- <servlet-class>response.DealWith</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>dealwith</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- </web-app>
- <!--index.jsp-->
- <%@ page contentType="text/html; charset=GBK"%>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Ajax-Demo</title>
- <script type="text/javascript">
- function createXMLHttpRequest(){
- if (window.ActiveXObject){
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- }
- else if(window.XMLHttpRequest){
- xmlHttp = new XMLHttpRequest();
- }
- }
- function sendRequest(){
- createXMLHttpRequest();
- var url;
- url="getServerTime.do?person=Ihavegotyou";
- xmlHttp.open("POST",url,true);
- xmlHttp.onreadystatechange = handleStateChange;
- xmlHttp.send();
- }
- function handleStateChange(){
- if (xmlHttp.readyState == 4){
- if (xmlHttp.status == 200){
- document.getElementById("serverTime").value=xmlHttp.responseText;
- }
- }
- }
- </script>
- </head>
- <form action="#">
- <input disabled type="TEXT" id="serverTime" value="这里显示服务器时间">
- <input align="absmiddle" type="button" value="点击获取服务器时间" onclick="sendRequest();" />
- </form>
- </body>
- </html>
DealWith.java
java 代码
- package response;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class DealWith extends HttpServlet {
- private static final String CONTENT_TYPE = "text/xml; charset=GBK";
- private static final String URIS[] = { "getServerTime.do", "others.do" };
- public void init() throws ServletException {
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response) {
- response.setContentType(CONTENT_TYPE);
- if (request.getRequestURI().indexOf(URIS[0]) != -1) {
- try {
- java.util.Calendar cal = java.util.Calendar.getInstance();
- java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat(
- "yyyy-MM-dd hh:mm:ss");
- String time = fmt.format(cal.getTime());
- response.getWriter().print(time);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response) {
- doPost(request, response);
- }
- // Clean up resources
- public void destroy() {
- }
- }
- Creating Services
How to build a service interface from scratch. - Implementing Services
Implement your service interface as a servlet. - Actually Making a Call
How to actually make a remote procedure call from the client. - Serializable Types(不必要)
Using GWT's automatic serialization well. - Handling Exceptions
Handle exceptions due to failed calls or thrown from the server. - Getting Used to Asynchronous Calls
Asynchronous calls are tricky at first, but ultimately your users will thank you.