• Post Reply Bookmark Topic Watch Topic
  • New Topic

Capturing HTML output in a String  RSS feed

 
Chris Garrison
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to have a link (to another URL) in a servlet. Instead of forwarding to that link, I want to capture the HTML output of the other URL.
So:
Servlet A has link to Servlet B
Link is clicked.
I somehow in Servlet A capture the output in HTML of Servlet B in a String.
Then from servlet A - out.print(hmtlString);
Any ideas?
I'm working with these dang portals.
Thanks.
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output from a servlet can be treated just like any other HTTP stream. This example program screenscrapes text from a cgi script but will work the same with any web source.
[ August 19, 2003: Message edited by: Tom Blough ]
[ August 19, 2003: Message edited by: Tom Blough ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to look into the java.net package URL, URLConnection and HttpURLConnection classes. Once you have created a connection you can open an InputStream to read the output of servlet B.
I think we have had several extended discussions of this sort of thing recently.
Bill
 
Chris Garrison
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input. I did several searches (FYI) and they were a help. Unfortunatly, I'm kinda of a newby so it's tough.
Thanks for pointing me to java.net. I think I'm well on my way.
I'm gettin the following exception:
java.net.UnknownServiceException: protocol doesn't support input
at java.net.URLConnection.getInputStream(URLConnection.java:666)
at UFSTest.doGet(UFSTest.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

Heres the servlet/doGet code:
(note: UFSTestHelper is a class I created b/c I had to extend HttpURLConnection - I defined all (previously) abstract methods to return true?!?!?)
anyway - here's the code
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class UFSTest extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<HTML>");
out.println("<HEAD>");
out.println("</HEAD><BODY>");
URL url = new URL("https://XXXXXXXXXXXXXXXX");
UFSTestHelper huc = new UFSTestHelper(url);
huc.connect();

InputStream is = huc.getInputStream();
out.print(is);

out.print("</BODY></HTML>");
}.

The HTML I'm trying to capture is coming from a page that uses struts. I don't know if that makes a difference??!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!