DWR

阅读更多
DWR一个外国人实现的很有前途的AJAX框架。
多余的话就不说了,请看DWR的例子程序:
web.xml
1
2
3
4
5
DWR (Direct Web Remoting)
A demo of how to call Java on the server directly from Javascript on the client
8

10    dwr-invoker
11    DWR Servlet
12    Direct Web Remoter Servlet
13    uk.ltd.getahead.dwr.DWRServlet
14   
20   
21      debug
22      true
23   

24   
25      scriptCompressed
26      false
27   

28    1
29 

30
31 
32    dwr-invoker
33    /dwr/*
34 

35
36
servlet(uk.ltd.getahead.dwr.DWRServlet)里:
1     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
2     {
3         try
4         {
5             builder.set(req, resp, getServletConfig(), getServletContext(), container);
6             ServletLoggingOutput.setExecutionContext(this);
7
8             processor.handle(req, resp);//该方法对所有request路径/dwr/*有效,在引用JS的时候,使用这个路径执行dwr生成的javascript代码

    finally
11         {
12             builder.unset();
13             ServletLoggingOutput.unsetExecutionContext();
14         }
15     }
index.html
1
2
3
4
5   DWR - Test Home
6  
7  
8  
9  
34
35
36
37 .这一部分经过了SERVLET处理:
 
 
  dwrservlet.doPost方法内processor.handle(req, resp)这个方法如下:
1     public void handle(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
2     {
3         String pathInfo = req.getPathInfo();
4         String servletPath = req.getServletPath();
5
6         if (nullPathInfoWorkaround && pathInfo == null)
7         {
8             pathInfo = req.getServletPath();
9             servletPath = HtmlConstants.PATH_ROOT;
10             log.debug("Default servlet suspected. pathInfo=" + pathInfo + "; contextPath=" + req.getContextPath() + "; servletPath=" + servletPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
11         }
12
13         if (pathInfo == null ||
14             pathInfo.length() == 0 ||
15             pathInfo.equals(HtmlConstants.PATH_ROOT))
16         {
17             resp.sendRedirect(req.getContextPath() + servletPath + HtmlConstants.FILE_INDEX);
18         }
19         else if (pathInfo.startsWith(HtmlConstants.FILE_INDEX))
20         {
21             index.handle(req, resp);
22         }
23         else if (pathInfo.startsWith(HtmlConstants.PATH_TEST))
24         {
25             test.handle(req, resp);
26         }
27         else if (pathInfo.startsWith(HtmlConstants.PATH_INTERFACE))
28         {
29             iface.handle(req, resp);//这个方法是我们要关注的
             }
     。。。。。。。
     } iface.handle(req, resp);//这个方法是我们要关注的,来自DefaultInterfaceProcessor
1     public void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
2     {
3         String pathinfo = req.getPathInfo();
4         String servletpath = req.getServletPath();
5         if (pathinfo == null)
6         {
7             pathinfo = req.getServletPath();
8             servletpath = HtmlConstants.PATH_ROOT;
9         }
10         String scriptname = pathinfo;
11         scriptname = LocalUtil.replace(scriptname, HtmlConstants.PATH_INTERFACE, HtmlConstants.BLANK);
12         scriptname = LocalUtil.replace(scriptname, HtmlConstants.EXTENSION_JS, HtmlConstants.BLANK);
13         Creator creator = creatorManager.getCreator(scriptname);
14
15         //resp.setContentType("text/javascript");
16         PrintWriter out = resp.getWriter();
17         out.println();
18
19         out.println("function " + scriptname + "() { }"); //从这里开始DWR自动生成javascript
             String   path = overridePath;
22         if (path == null)
23         {
24             path = req.getContextPath() + servletpath;
25         }
26         out.println(scriptname + "._path = '" + path + "';"); //$NON-NLS-1$ //$NON-NLS-2$
27
28         Method[] methods = creator.getType().getMethods();
29         for (int i = 0; i < methods.length; i++)
30         {
31             Method method = methods[i];
32             String methodName = method.getName();
33
34             // We don't need to check accessControl.getReasonToNotExecute()
35             // because the checks are made by the doExec method, but we do check
36             // if we can display it
37             String reason = accessControl.getReasonToNotDisplay(req, creator, scriptname, method);
38             if (reason != null && !allowImpossibleTests)
39             {
40                 continue;
41             }
42
43             // Is it on the list of banned names
44             if (jsutil.isReservedWord(methodName))
45             {
46                 continue;
47             }
48
49             out.print('\n');
50             out.print(scriptname + '.' + methodName + " = function("); //$NON-NLS-1$
51             Class[] paramTypes = method.getParameterTypes();
52             for (int j = 0; j < paramTypes.length; j++)
53             {
54                 if (!LocalUtil.isServletClass(paramTypes[j]))
55                 {
56                     out.print("p" + j + ", "); //$NON-NLS-1$ //$NON-NLS-2$
57                 }
58             }
59             out.println("callback) {"); //$NON-NLS-1$
60
61             out.print("    DWREngine._execute(" + scriptname + "._path, '" + scriptname + "', '" + methodName + "\', "); //实现javascript调用java内的方法
                 for (int j = 0; j < paramTypes.length; j++)
63             {
64                 if (LocalUtil.isServletClass(paramTypes[j]))
65                 {
66                     out.print("false, "); //$NON-NLS-1$
67                 }
68                 else
69                 {
70                     out.print("p" + j + ", "); //$NON-NLS-1$ //$NON-NLS-2$
71                 }
72             }
73             out.println("callback);"); //$NON-NLS-1$
74
75             out.println('}');
76         }
77
78         out.flush();
79     }

你可能感兴趣的:(DWR,JavaScript,Web,Servlet,J#)