`

gwt Client-Server 交互原形模型

阅读更多
gwt Client-Server 交互原形模型
 
  1. <!--web.xml-->  
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <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">  
  4.     <display-name>ccgwt</display-name>  
  5.     <welcome-file-list>  
  6.         <welcome-file>index.html</welcome-file>  
  7.         <welcome-file>index.htm</welcome-file>  
  8.         <welcome-file>index.jsp</welcome-file>  
  9.         <welcome-file>default.html</welcome-file>  
  10.         <welcome-file>default.htm</welcome-file>  
  11.         <welcome-file>default.jsp</welcome-file>  
  12.     </welcome-file-list>  
  13.     <servlet>  
  14.         <servlet-name>dealwith</servlet-name>  
  15.         <servlet-class>response.DealWith</servlet-class>  
  16.     </servlet>  
  17.     <servlet-mapping>  
  18.         <servlet-name>dealwith</servlet-name>  
  19.         <url-pattern>*.do</url-pattern>  
  20.     </servlet-mapping>  
  21. </web-app>  
  22.   
  23. <!--index.jsp-->  
  24. <%@ page contentType="text/html; charset=GBK"%>  
  25. <html xmlns="http://www.w3.org/1999/xhtml">  
  26. <head>  
  27. <title>Ajax-Demo</title>  
  28. <script type="text/javascript">  
  29.      function createXMLHttpRequest(){  
  30.          if (window.ActiveXObject){  
  31.           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
  32.                 }  
  33.                 else if(window.XMLHttpRequest){  
  34.                  xmlHttp = new XMLHttpRequest();  
  35.                 }  
  36.         }  
  37.         function sendRequest(){  
  38.          createXMLHttpRequest();  
  39.                 var url;  
  40.                 url="getServerTime.do?person=Ihavegotyou";  
  41.                 xmlHttp.open("POST",url,true);  
  42.                 xmlHttp.onreadystatechange = handleStateChange;  
  43.                 xmlHttp.send();  
  44.         }  
  45.           
  46.         function handleStateChange(){  
  47.          if (xmlHttp.readyState == 4){  
  48.           if (xmlHttp.status == 200){  
  49.         document.getElementById("serverTime").value=xmlHttp.responseText;  
  50.                         }  
  51.                 }  
  52.         }  
  53.    
  54. </script>  
  55.   
  56. </head>  
  57. <form action="#">  
  58.     <input disabled type="TEXT" id="serverTime" value="这里显示服务器时间">   
  59.     <input align="absmiddle" type="button" value="点击获取服务器时间" onclick="sendRequest();" />  
  60.  </form>  
  61. </body>  
  62. </html>  




DealWith.java
java 代码
  1. package response;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. public class DealWith extends HttpServlet {
  5. private static final String CONTENT_TYPE = "text/xml; charset=GBK";
  6. private static final String URIS[] = { "getServerTime.do", "others.do" };
  7. public void init() throws ServletException {
  8. }
  9. public void doPost(HttpServletRequest request, HttpServletResponse response) {
  10. response.setContentType(CONTENT_TYPE);
  11. if (request.getRequestURI().indexOf(URIS[0]) != -1) {
  12. try {
  13. java.util.Calendar cal = java.util.Calendar.getInstance();
  14. java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat(
  15. "yyyy-MM-dd hh:mm:ss");
  16. String time = fmt.format(cal.getTime());
  17. response.getWriter().print(time);
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }
  23. public void doGet(HttpServletRequest request, HttpServletResponse response) {
  24. doPost(request, response);
  25. }
  26. // Clean up resources
  27. public void destroy() {
  28. }
  29. }
清楚上面的模型,就会更加理解GWT RPC(Remote Procedure Calls)的几个步骤:
  • 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.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics