Home arrow Java arrow Creating a User Interface for a Search Service

Creating a User Interface for a Search Service

Last week, we started building a search service for a website. This week, we're going to cover the user interface and the indexer. This article is excerpted from chapter 10 of the book Better, Faster, Lighter Java, written by Bruce A. Tate and Justin Gehtland (O'Reilly; ISBN: 0596006764).

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 4
November 22, 2006
  1. · Creating a User Interface for a Search Service
  2. · Changes to the Original Code to Fit the JSP
  3. · Setting Up the Indexer
  4. · Making Use of the Configuration Service

print this article

Creating a User Interface for a Search Service
(Page 1 of 4 )

The User Interface (JSP)

The user interface is fairly straightforward. Instead of just dumping our results to the console or creating an XML document of the results (as in the web service implementation from Chapter 9), this time we need to write a JSP that iterates over the results and displays them as hyperlinks in a table.

The originaljPetStore search feature used aPagedListHolderfor its results because it displayed the image associated with each returned product in the table. Since the images were arbitrary in size,jPetStoredidn’t want to display too many entries on a given page since it might result in a lot of vertical scrolling. Our results consist of a hyperlink to the returned URL and the relative rank of the given result; therefore, we’ll use a simple table to display our results.

Again, we are faced with the rewrite-or-replace question. Just like last time, we have three questions to consider:

  1. Do we have access to the original source? We must, since JSPs are just text files in both development and deployment mode.
  2. Will we ever want to reuse the existing service? We do, but in this case, a JSP is so easy to recreate that it won’t make much difference.
  3. Does the current version implement some standard interface? Not as such, since JSPs are just mixes of static HTML and dynamic content.

Because of the rather trivial nature of the changes and because JSPs are easily edited in place (no compilation necessary), we’ll just repurpose the existing SearchProducts.jsp file. This strategy saves us from having to change any more configuration settings:

  <%@ include file="IncludeTop.jsp" %>

  <table align="left" bgcolor="#008800" border="0" cellspacing="2" cellpadding="2">
<td bgcolor="#FFFF88">
<a href="<c:url value="/shop/index.do"/>">
<b><font color="BLACK" size="2"> &lt;&lt; Main Menu</font></b>
  <table align="center" bgcolor="#008800" border="0" cellspacing="2"

      <tr bgcolor="#CCCCCC"> <td><b>URL</b></td> <td><b>Rank</b></td> </tr>
      <c:forEach var="page" items="${hits}">
<tr bgcolor="#FFFF88">
<td><a href="<c:out value="${page.url}"/>">
<c:out value="${page.url}"/></a>
<c:out value="${page.score}"/>

  <%@ include file="IncludeBottom.jsp" %>

The JSP files in the application have a standard header and footer defined in IncludeTop.jsp and IncludeBottom.jsp. All we have to do is render the results in between the include directives. Start by creating a JSP-styleforEachloop, with an enumerator calledpagepointing at each member of theHashMapcalled “hits.” For each hit, we render a table row containing the URL (the value of which is both the text to display and the HREF to point it to) and the relative rank of the hit. JSP handles hooking up the variables and properties using reflection. However, when implementing this page, we come across the first (and only) reason to change some of the original Spider code.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials