Home arrow Java arrow Page 4 - 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 - Registering Our New Class with jPetStore
(Page 4 of 4 )

Finally, we alert jPetStore to the new controller’s existence. If jPetStoreis not coded for extensibility, we have to modify the application code in order to get it to work. For instance, if there are methods ofjPetStorethat create instances ofSearchProductsControllerdirectly, we must change each of those lines to create aSearchPagesControllerinstead.

It turns out, however, thatjPetStoreis quite ready for extensibility—partly because it is based on the Spring framework. In order to telljPetStoreabout our new controller, we modify a single configuration file (petstore-servlets.xml). This file tells Spring what objects to create and how to wire them together to make a sensible application. Now, we just need to find the configuration setting used to launch theSearchProductsControllerand point it to our newSearchPagesControllerinstead.

  <bean name="/shop/searchProducts.do"
     
class="org.springframework.samples. jpetstore.web.spring.SearchPagesController">
     
<property name="petStore"><ref bean="petStore"/></property>
  </bean>

We’re telling the application to map requests for “/shop/searchProducts.do” to a new instance ofSearchPagesController. At the same time, we tell it, provide theSearchPagesControllerwith the current instance ofpetStore(in a property calledpetStore).

Principles in action
  1. Keep it simple: the controller logic is a simple invocation of Spider; the controller interface is very simple (one method)
  2. Choose the right tools: Spring and the Spider
  3. Do one thing and do it well: since the Spider is so well-encapsulated, it’s easy to add to an existing service; the controller deals with invoking the Spider and the JSP only needs to display the results—MVC pattern well-demonstrated
  4. Strive for transparency: the site doesn’t care how it is indexed; it can easily switch between data-driven and HTML-driven search technologies
  5. Allow for extension: we quickly expanded our search capabilities by adding a new tool with minimal code; the configuration abilities of jPetStore allow for no-code recognition of new service

Please check back next week for the continuation of this article.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials