我正在用jsp制作一个基于web的应用程序,其中我需要从其他网站获取数据。我一点也不知道怎么做。我在scrapperwiki中搜索,但没有任何与jsp相关的内容。
那么,如何使用jsp从其他网站获取数据呢?
发布于 2012-11-17 00:24:23
观察网页的来源并对其进行解析。有一个project可以让这一切变得简单。
发布于 2012-11-17 00:16:43
最简单的方法是使用URLConnection:
<%@ page language="Java" import="java.net.*,java.io.*"%>
<%
try {
URL url = new URL("http://other-website/url/test.txt");
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(false);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line=null;
line = br.readLine();
while ( line != null ) {
%>
<%=line%>
<BR>
<%
line = br.readLine();
}
br.close();
} catch(Exception e) {
e.printStackTrace();
}
%> 当然,最好将这些代码放在java控制器servlet中,而不是将其用作JSP中的scriptlet,或者使用任何MVC框架而不是纯Servlet +JSP的框架。
https://stackoverflow.com/questions/13419611
复制相似问题