Como carregar um site externo dentro de um div
Autor: Cristiano Pires | Publicado: 2011-12-14T15:14:00-02:00

Tutorial de como inserir uma página externa em um div.
Vou utilizar o método HttpURLConnection do Java. Pro Javascript JQuery.
Vou abrir uma conexão à uma página externa de dentro de um Servlet Java e exibir o resultado em um&nbsp;HttpServletResponse&nbsp;que por fim será exibido em um Div.

Primeiro o arquivo html.
Vou criar um botão que ao ser clicado vai enviar um url pra um método javascript que por sua vez vai chamar o servlet e abrir a página externa no div.

&lt;!DOCTYPE&nbsp;html&nbsp;PUBLIC&nbsp;"-//W3C//DTD&nbsp;HTML&nbsp;4.01&nbsp;Transitional//EN"&nbsp;"http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&nbsp;&nbsp;&lt;meta&nbsp;http-equiv="Content-Type"&nbsp;content="text/html;&nbsp;charset=ISO-8859-1"&gt;
&nbsp;&nbsp;&lt;link&nbsp;rel="stylesheet"&nbsp;type="text/css"&nbsp;href="/pages/style/style.css"&nbsp;/&gt;
&nbsp;&nbsp;&lt;script&nbsp;type="text/javascript"&nbsp;src="/pages/script/jquery-1.6.2.min.js"&gt;&lt;/script&gt;
&nbsp;&nbsp;&lt;script&nbsp;type="text/javascript"&nbsp;src="/pages/script/script.js"&gt;&lt;/script&gt;
&nbsp;&nbsp;&lt;title&gt;Generate&lt;/title&gt;
&lt;/head&gt;

&nbsp;&nbsp;&lt;body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;input&nbsp;type="button"&nbsp;id="btnSend"&nbsp;value="clique&nbsp;aqui"&nbsp;onclick="show('http://www.google.com.br');"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div&nbsp;id="result"&nbsp;style="border:&nbsp;1px&nbsp;solid&nbsp;black;&nbsp;width:&nbsp;450px;&nbsp;height:&nbsp;500px;&nbsp;text-align:&nbsp;center;"&gt;&lt;/div&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div&nbsp;id="error"&gt;&lt;/div&gt;
&nbsp;&nbsp;&lt;/body&gt;

&lt;/html&gt;


O arquivo script.js, que contém o método show(url)A função vai desabilitar o botão submit, e em seguida chamar o servlet com o url (método GET). Caso haja algum erro na requisição, eu exibo o tipo do erro no html.

function&nbsp;show(url)&nbsp;{
&nbsp;&nbsp;$('#btnSend').attr('disabled',&nbsp;'disabled');
&nbsp;&nbsp;$("#result").load(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'/Generate/ServletRedirect?host='.concat(url),
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;function(response,&nbsp;status,&nbsp;xhr)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(status&nbsp;==&nbsp;"error")&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;msg&nbsp;=&nbsp;"Ocorreu&nbsp;um&nbsp;erro:&nbsp;";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$("#error").html(msg&nbsp;+&nbsp;xhr.status&nbsp;+&nbsp;"&nbsp;"&nbsp;+&nbsp;xhr.statusText);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});

}

Por fim o Servlet que vai utilizar o método&nbsp;HttpURLConnection pra conectar a página externa

import&nbsp;java.io.BufferedReader;
import&nbsp;java.io.IOException;
import&nbsp;java.io.InputStream;
import&nbsp;java.io.InputStreamReader;
import&nbsp;java.net.HttpURLConnection;
import&nbsp;java.net.URL;

import&nbsp;javax.servlet.ServletException;
import&nbsp;javax.servlet.http.HttpServlet;
import&nbsp;javax.servlet.http.HttpServletRequest;
import&nbsp;javax.servlet.http.HttpServletResponse;

public&nbsp;class&nbsp;ServletRedirect&nbsp;extends&nbsp;HttpServlet&nbsp;{
&nbsp;&nbsp;private&nbsp;static&nbsp;final&nbsp;long&nbsp;serialVersionUID&nbsp;=&nbsp;1L;

&nbsp;&nbsp;protected&nbsp;void&nbsp;doGet(HttpServletRequest&nbsp;request,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletResponse&nbsp;response)&nbsp;throws&nbsp;ServletException,&nbsp;IOException&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;StringBuilder&nbsp;stringBuilder&nbsp;=&nbsp;new&nbsp;StringBuilder();
&nbsp;&nbsp;&nbsp;&nbsp;HttpURLConnection&nbsp;connection&nbsp;=&nbsp;null;

&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL&nbsp;url&nbsp;=&nbsp;new&nbsp;URL(request.getParameter("host"));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection&nbsp;=&nbsp;(HttpURLConnection)&nbsp;url.openConnection();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.setRequestProperty("Accept-Charset",&nbsp;"ISO-8859-1");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.setRequestMethod("GET");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.setRequestProperty("Content-Type","text/plain");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.connect();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader&nbsp;reader&nbsp;=&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(connection.getInputStream()));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;line&nbsp;=&nbsp;null;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((line&nbsp;=&nbsp;reader.readLine())&nbsp;!=&nbsp;null)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stringBuilder.append(line&nbsp;+&nbsp;"\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.close();
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;error&nbsp;=&nbsp;((HttpURLConnection)&nbsp;connection).getErrorStream();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader&nbsp;reader&nbsp;=&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(error));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;line&nbsp;=&nbsp;null;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((line&nbsp;=&nbsp;reader.readLine())&nbsp;!=&nbsp;null)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stringBuilder.append(line&nbsp;+&nbsp;"\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reader.close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;response.setContentType("text/html"));
&nbsp;&nbsp;&nbsp;&nbsp;response.getWriter().write(stringBuilder.toString());
&nbsp;&nbsp;}

}

Iniciei a conexão com&nbsp;HttpURLConnection. Como o método utilizado foi GET, peguei o parâmetro da url.
A linha&nbsp;connection.setRequestProperty("Accept-Charset",&nbsp;"ISO-8859-1"); codifica a página, caso haja acentuação. No meu caso ISO-8859.
Em seguida leio toda a página em um&nbsp;StringBuilder e&nbsp;marco a saída (response) como html (response.setContentType("text/html");) e mando o resultado.

Se eu utilizasse o método POST, mudaria o seguinte:

connection.setDoOutput(true);&nbsp;//&nbsp;Triggers&nbsp;POST.
connection.setRequestProperty("Content-Type",&nbsp;"application/x-www-form-urlencoded;charset="&nbsp;+&nbsp;"ISO-8859-1");

OutputStream&nbsp;output&nbsp;=&nbsp;null;
try&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output&nbsp;=&nbsp;connection.getOutputStream();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.write(query.getBytes(charset));
}&nbsp;finally&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(output&nbsp;!=&nbsp;null)&nbsp;try&nbsp;{&nbsp;output.close();&nbsp;}&nbsp;catch&nbsp;(IOException&nbsp;logOrIgnore)&nbsp;{}
}

BufferedReader&nbsp;reader&nbsp;=&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(connection.getInputStream()));


É isso.&nbsp;

<!-- Coloque esta tag onde você deseja que o botão +1 seja renderizado -->


<!-- Coloque esta chamada de renderização conforme necessário -->

  (function() {
    var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
    po.src = 'https://apis.google.com/js/plusone.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
  })();