웹상에서 다른 사이트의 소스 가져오기
----OtherDomainRead.html ----
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Button Test</title>
<script language="javascript" type="text/javascript" src="js/prototype.js"></script>
<script language="javascript" type="text/javascript" src="js/other.js"></script>
</head>
<body>
<form id="imgform" name="imgform">
<input id="url" name="url" type="text" value="http://www.kma.go.kr/weather_main.jsp" />
<image src="originalfile.jpg" id="submitImg" name="submitImg"/>
</form>
<div id="result"></div>
</body>
</html>
---- other.js ----
Event.observe(window, "load", function() {
Event.observe("submitImg", "click", Submitter.clickHandler);
});
var Submitter = {
clickHandler : function(event) {
var URL = "otherDomain.jsp?url="+$("url").value;
new Ajax.Request(URL, {
onComplete : function(xmlHttp) {
$("result").innerHTML = xmlHttp.responseText;
}
});
}
};
---- otherDomain.jsp ---- 맘대로 바꾸셔도 되는 부분이며, 여기에서 실제로 내용을 읽어오는 부분입니다.
<%@page import="java.net.MalformedURLException, java.io.IOException, java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader, java.io.OutputStreamWriter, java.io.BufferedWriter"%>
<%@page import="java.net.URLConnection, java.net.URL"%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%
BufferedWriter output = null;
BufferedReader in = null;
URLConnection conn = null;
try {
String strUrl = request.getParameter("url");
String otherDomain = strUrl.substring(0, strUrl.lastIndexOf("/"));
URL url = new URL(strUrl);
conn = url.openConnection();
conn.setDoOutput(true);
output = new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ) );
in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );
String current;
StringBuffer res = new StringBuffer(100);
while ( (current = in.readLine()) != null ) {
res.append(current)
.append(" ");
}
current = res.toString().replaceAll("/images", otherDomain+"/images");
current = current.replaceAll(""/", """+otherDomain+"/");
current = current.replaceAll("src="/", "src=""+otherDomain+"/");
out.print(current);
System.out.println(current);
} catch ( MalformedURLException ex ) {
ex.printStackTrace();
} catch ( IOException ex ) {
ex.printStackTrace();
} finally {
if(null != output)
output.close();
if(null != in)
in.close();
}
%>