Home arrow Java arrow Page 3 - Adding a Search Service to a Java Application
JAVA

Adding a Search Service to a Java Application


The ability to search a website can be invaluable to visitors. Even if you have a website that searches a product database, your visitors might want to search the contents of your entire site. This article helps you set up such a service. It 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 16, 2006
TABLE OF CONTENTS:
  1. · Adding a Search Service to a Java Application
  2. · Extending jPetStore
  3. · Implementing the Interface
  4. · Registering Our New Class with jPetStore

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Adding a Search Service to a Java Application - Implementing the Interface
(Page 3 of 4 )

To replace this class, we’re going to write our own controller class called SearchPagesController. It must implement the Controller interface, which defines ourhandleRequestmethod.

  public class SearchPagesController implements Controller{
     ...
  }

Here’s our controller’shandleRequestmethod:

  public ModelAndView handleRequest(HttpServletRequest request,
           HttpServletResponse response) throws Exception {
    
if (request.getParameter("search") != null) {
         String keyword = request.getParameter("keyword");
         if (keyword == null || keyword.length() == 0) {
             return new ModelAndView("Error", "message", "Please enter a
            
keyword to search for, then press the search button.");
         }
         else {
               
ConfigBean cfg = new ConfigBean();
                     String indexpath = "";
                     try
                     {
                       
indexpath = cfg.getCurIndexPath();
                     }
                     catch(Exception ex)
                     {
                      
return new ModelAndView("Error", "message",
             "Could not find current index path.");
                     }

                     QueryBean qb = new QueryBean(indexpath, keyword, "contents");

                     qb.execute();

                     HashMap hits = new HashMap(qb.getResults().length);
                     for(int i =0;i<qb.getResults().length;i++)
                     {
                       hits.put("hits", qb.getResults()[i]);
                     }
                     return new ModelAndView("SearchProducts", hits);
         }
     }
  }
 

For our search functionality, we won’t use the paged results. We simply list all the results on a single page; as a result, we don’t have to deal with the Next Page and Previous Page code. Our controller again checks for null keywords and returns an error if it finds them empty. Otherwise, the service is used almost identically as the console application in the Chapter 9 was used. First, create an instead ofConfigBeanto find the most current index of the site, then create aQueryBean based on that index path. Finally, execute the query and put all theHitBeaninstances into aHashMapto return to the View.

The usage pattern is identical to that in the last chapter; the only difference is the format of our returned data. Instead of passing the native array ofHitBeansback, theModelAndViewobject requires aHashMap. It’s easy enough to create the one from the other, and now we have an entirely new access point for the Spider application with almost no work.

There is one last detail we need to work out. The originalSearchProductsControllerhas a field calledpetStoreof typePetStoreFacade that the Spring framework populates for it. In order to be a complete replacement for the original, our new controller needs to expose the same property and accessor methods, even though they aren’t officially found on a standalone interface anywhere in the application. You will often find examples of this when you’re extending or modifying an application.

  private PetStoreFacade petStore;

     public void setPetStore(PetStoreFacade petStore){
        this.petStore = petStore;
     }


blog comments powered by Disqus
JAVA ARTICLES

- 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 ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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