浏览 4639 次
锁定老帖子 主题:DWR实现跨域访问
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (2)
|
|
---|---|
作者 | 正文 |
发表时间:2009-02-23
web.xml配置信息 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- start config dwr can be request by remote --> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>allowGetForSafariButMakeForgeryEasier</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>allowScriptTagRemoting</param-name> <param-value>true</param-value> </init-param> <!-- end config dwr can be request by remote --> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- dwr.xml的配置信息 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd"> <dwr> <allow> <create javascript="TestCross" creator="new"> <param name="class" value="com.topking.test.Operator"/> </create> </allow> </dwr> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- java类代码 package com.topking.test; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; public class Operator { public String greeting(String src){ WebContext context = WebContextFactory.get(); String ip = context.getHttpServletRequest().getRemoteAddr().toString();//获取客户端IP return "hello "+src+" Your IP : "+ip; } } ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 页面代码 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'TestDWRCross.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type='text/javascript' src='/Ajax_chat/dwr/engine.js'></script> <script type='text/javascript' src='/Ajax_chat/dwr/util.js'></script> <script type="text/javascript" src='http://www.myspace.com:8080/DWR_cross/dwr/interface/TestCross.js'></script> <script type="text/javascript"> function sendMsg(){ DWREngine.setMethod(DWREngine.ScriptTag); TestCross.greeting(DWRUtil.getValue("userName"),function(data){alert(data);}); } </script> </head> <body> Input Your Name:<input type="text" id="userName"/> <input type="button" value="Submit" onclick="sendMsg();"/> <br> </body> </html> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-02-24
最后修改:2009-02-24
混淆概念,
跨域的不是dwr, 是js ok? 管理员呢,隐了吧 |
|
返回顶楼 | |
发表时间:2009-02-25
kimmking 写道 混淆概念, 跨域的不是dwr, 是js ok? 管理员呢,隐了吧 如果不配置DWR,怎么实现JS的跨域?! |
|
返回顶楼 | |