结合DWR获取远程URL数据 博客分类: jsp DWRJavaBeanJSPXML
程序员文章站
2024-03-16 08:26:34
...
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>