Build your own portal

One thing that all server-side web programming languages have in common is that the web browser is oblivious to what they do before the result is returned. Whether the page was coded in JSP, ASP, PHP, etc., the result looks the same when you view the source. It is simple HTML, XML, or any other format that the browser can read.

Portals take advantage of this fact when they aggregate content from different sources into a single page. The content taken from each source is represented as a “portlet” – a smaller section hosted within the larger portal page layout.

A portal operates much like a proxy in the sense that it receives a request from a client, asks for information from one or more other servers, and returns the results to the client. All request/response messages are filtered through the portal.

The example we’ll use is a search engine portal, creating a single page with Yahoo, Google, and AltaVista portlets.

The following code segments are to be used within a JSP or servlet. They will require the and packages to be imported.

For the sake of simplicity, we’ll start with a page that just gives us the Yahoo content. First we need to create the URL:

URL url[] = { new URL “”) };

Notice that this URL object is defined as an array, even though it only holds the one address. This is so it will be easier to add the other search engines later.

Now we will loop through each element in our array of URLs (only one for now), open a connection to that URL, and read its contents into a StringBuffer:

StringBuffer buf = new StringBuffer(“”);

for (int i=0; i<url.length; i++) {

URLConnection urlCon = url[i].openConnection();

BufferedReader in = new BufferedReader(

new InputStreamReader(urlCon.getInputStream())


String inputLine;

while ((inputLine = in.readLine()) != null)



The StringBuffer now contains the source of the page we’re trying to generate, so we simply print it to the browser:


Upon viewing the results, you’ll notice that the page you’ve created looks exactly like Yahoo, even though your browser’s URL line is pointing to your own Java server. When you click a link or execute a search, it works as it normally would, taking you to a page on Yahoo’s server.

If this seems simple, it’s because Yahoo has made it easy for us. Let’s try running the same example against the Google site to see what kind of complications can arise. Simply change the element in your URL array to point to and refresh your page.

You’ll notice that although the page loads, the images don’t display, the links are all broken, and searching doesn’t work. This is because (at the time of this writing) Yahoo is using full paths in their HTML, but Google is using relative paths. A full path to an image would look something like

“,” where a relative path to an image would be “images/imagename.gif.” The problem is that the HTML for the Google page is trying to find its images and links on a path relative to your server, where they don’t exist. A workaround for this would involve parsing the HTML contained within the StringBuffer and prefixing all of the relative paths with the server name.

Let’s go ahead with generating the page with all three search engines. Change your URL array to look like this:

URL url[] = {

new URL(“”),

new URL(“”),

new URL(“”) };

In my browser, each of the search engines prints, one after the other. Not all browsers will display this result successfully, because the underlying HTML is malformed. Each search engine has its own opening and closing tags. One option is to generate your portal page as a frameset, and include each portlet in its own frame. Alternatively, you could use another parsing routine to strip out the superfluous tags and print all of the portlets within the same page.

Creating a portal page that includes other complete Web pages may present additional challenges with CSS and JavaScript. Typically, a portlet will be designed specifically for the purpose of being part of a larger page, and it will not be a complete HTML document on its own. Images and links will be sourced using full paths. Good portlets also occupy far less screen real estate than a complete page.

As always, I’d be happy to e-mail the source file for this example to anyone who wants it.

Cooney works as a programmer/analyst for a major Canadian book publisher. He can be reached at [email protected].

Would you recommend this article?


Thanks for taking the time to let us know what you think of this article!
We'd love to hear your opinion about this or any other story you read in our publication.

Jim Love, Chief Content Officer, IT World Canada

Featured Download

Related Tech News

Tech Jobs

Our experienced team of journalists and bloggers bring you engaging in-depth interviews, videos and content targeted to IT professionals and line-of-business executives.

Tech Companies Hiring Right Now