浏览 4945 次
锁定老帖子 主题:结合DWR获取远程URL数据
精华帖 (0) :: 良好帖 (0) :: 新手帖 (15) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-04-28
bean.java package com.cw.entity; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Bean { private String[] citys = new String[] { "杭州", "上海", "香港", "武汉", "南京" }; public String[] getCitys() { return this.citys; } public String sendPost(String url) { String result = ""; try { URL httpurl = new URL(url); HttpURLConnection httpConn = (HttpURLConnection) httpurl .openConnection(); httpConn.setDoInput(true); BufferedReader in = new BufferedReader(new InputStreamReader( httpConn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } in.close(); } catch (Exception e) { e.printStackTrace(); System.out.println("没有结果!" + e); } return result; } } index.jsp <%@ page language="java" pageEncoding="gbk"%> <% 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 'index.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="dwr/interface/bean.js"></script> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/util.js"></script> <style type="text/css"> .tips-loading { PADDING-RIGHT: 3px; PADDING-LEFT: 3px; BACKGROUND: url(img/reg0.gif) lightyellow no-repeat 3px center; PADDING-BOTTOM: 1px; OVERFLOW: hidden; WIDTH: 300px; COLOR: #808080; TEXT-INDENT: 20px; LINE-HEIGHT: 16px; PADDING-TOP: 2px } .tips-pass { BACKGROUND: url(img/reg4.gif) #e9ffeb no-repeat 0px -2px; COLOR: #333 } </style> </head> <body> <div style="position:absolute; left: 14px; top: 49px; width: 968px; height: 70px; background:#CCCCCC"> <label style="position:absolute; left: 52px; top: 22px;"> <input type="text" name="url" size="50" value="http://www.baidu.com"> </label> <label style="position:absolute; left: 422px; top: 21px; width: 140px;"> <input name="Input" type="submit" value="获取内容" onClick="getData()"> </label> <div id="status" style="position:absolute; width:278px; left: 501px; top: 24px; height: 20px; background-color:#CCCC99"></div> </div> <p> </p> <textarea style="position: absolute; left: 13px; top: 125px; width: 968px; height: 419px;" id="showresult"></textarea> <script type="text/javascript"> function $(id){ return document.getElementById(id); } function getData(){ $("status").className="tips-loading"; $("status").innerHTML=" 正在读取数据..."; bean.sendPost(dwr.util.getValue("url"),function(data){ $("showresult").value=data; $("status").className="tips-pass"; $("status").innerHTML=" 读取完毕!"; }); } </script> </body> </html> dwr.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create creator="new" javascript="bean"> <param name="class" value="com.cw.entity.Bean" /> <include method="getCitys" /> <include method="sendPost"/> </create> </allow> </dwr> web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app 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"> <servlet> <servlet-name>dwr-invoker</servlet-name> <!-- <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>--> <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name> initApplicationScopeCreatorsAtStartup </param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>maxWaitAfterWrite</param-name> <param-value>500</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-05-07
封装了下 java 通过url类获取远程数据的方法.
挺好. |
|
返回顶楼 | |
发表时间:2008-05-07
dwr真是给人带来丰富的想象力。佩服!
|
|
返回顶楼 | |
发表时间:2008-05-13
function getData(){
$("status").className="tips-loading"; $("status").innerHTML=" 正在读取数据..."; bean.sendPost(dwr.util.getValue("url"),function(data){ $("showresult").value=data; }); bean.getCitys(function(data){ $("showresult").value=data; }); $("status").className="tips-pass"; $("status").innerHTML=" 读取完毕!"; } 同时运行两个方法时 getCitys的结果为什么被覆盖? |
|
返回顶楼 | |
发表时间:2008-05-13
还有,你这样通过url从远程获取的东西,一般什么时候会用到啊?
如果是页面的话,你只需要连接下就可以了啦.. 难道是个xml通过url从远程获取?? 还是.... |
|
返回顶楼 | |
发表时间:2008-05-14
这样获取远程的东西,其实现在很多公司都在用,
不过都是一些小公司。。。 如果你是做小说,股票,基金之类的网站,如果你公司没有足够的金钱和相关机构合作的话,是很难在最短时间内更新数据的。。。 当然,这也不能说你的网站也就这样挂掉,于是就这样去获取别人网站的东西,返回之后再解析之后就可以放到自己网站上去了,唉,以前没有少干过这样的事。。。 知道是违法的,那个后怕呀。。。 现在很多小说网站都是这样的,没有办法,要吃饭,只有去偷了。。。 |
|
返回顶楼 | |