Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Implement Document Storage and Search on Google Java App Engine : Page 4

Learn how to use the Java version of the Google App Engine by implementing search and document storage on a Java web application running on the platform.


advertisement

JSPs for the Sample Web Application

Usually, when I start writing a new application using JSPs, I embed snippets of Java code in the JSPs and eventually factor common code snippets out into custom JSP tag libraries, add extra behavior to model classes, and so on. I have not performed this cleanup process for the sample web application.

The first JSP file you will look at, index.jsp, lists all documents in the system. It also contains some optional debug code (that I usually leave commented out) to list all instances of the IndexToken class (see Figure 2). The first part of index.jsp includes the required classes, defines the HTML headers, and includes menu.jsp—a single include file for page navigation:

<%@ page import="javax.jdo.*, java.util.*, com.kbsportal.model.*,com.kbsportal.persistence.PMF" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>KBSportal Java App Engine Search Demo</title> </head> <body> <%@ include file="menu.jsp" %>

Figure 2. List All Documents: Debug code lists all instances of the IndexToken class and shows some index tokens.



You have already seen sample JDOQL queries in the implementation of the IndexToken class. Here, the query returns all objects in the document class:

<h2>All documents:</h2> <% PersistenceManager pm = PMF.get().getPersistenceManager(); Query query = pm.newQuery(Document.class); try { List<Document> results = (List<Document>) query.execute(); if (results.iterator().hasNext()) { for (Document d : results) { System.out.println("key: "+d.getUri() + ", title: "+d.getTitle()); %> <h3><%=d.getTitle()%></h3> <p><%=d.getContent()%></p> <% } } } finally { query.closeAll(); } %>

This sample uses a query object instead of a string JDOQL query so as not to restrict query results for this JSP. However, if you wanted to return only documents with a specific title, you could filter query results using this code:

String title_to_find = "Dogs and Cats" query.setFilter("title == " + title_to_find);

The bottom part of the file index.jsp contains some debug code that you may want to enable when you experiment with this sample web application. This debug code is almost identical to the above code snippet, except that it queries all instances of the IndexToken class:

query = pm.newQuery(IndexToken.class); try { List<IndexToken> results = (List<IndexToken>) query.execute(); if (results.iterator().hasNext()) { for (IndexToken indexToken : results) {

Figure 3. Form for Adding a Document to the Datastore: A JSP implements an HTML input form for entering new "documents" into the system.

The file new_document.jsp implements an HTML input form for entering new "documents" into the system (see Figure 3). The following code snippet (from new_document.jsp) checks to see if form data is present in the page request. If so, it adds an instance of class Document to the datastore:

<% String url = request.getParameter("url"); String title = request.getParameter("title"); String text = request.getParameter("text"); if (url!=null && title!=null && text!=null) { PersistenceManager pm = PMF.get().getPersistenceManager(); try { Document doc = new Document(url, title, text); pm.makePersistent(doc); IndexToken.indexString(doc.getUri(), doc.getTitle() + " " + doc.getContent()); } finally { pm.close(); } } %>

The makePersistent method is called directly to save the document to the datastore. The static method IndexToken.indexString adds each search token created from the document's title and contents to the datastore.

Figure 4. Remove All Documents and Index Tokens from the Datastore: The sample application calls for a simple way to clear out all test "documents" added to the datastore.

Because this sample application is publicly hosted on Google's hosting service, it calls for a simple way to clear out all test "documents" added to the datastore. The JSP file delete_all.jsp deletes all documents and index tokens from the datastore (see Figure 4).

PersistenceManager pm = PMF.get().getPersistenceManager(); Query query = pm.newQuery(Document.class); try { List<Document> results = (List<Document>) query.execute(); if (results.iterator().hasNext()) { for (Document d : results) { pm.deletePersistent(d); } } } finally { query.closeAll(); } query = pm.newQuery(IndexToken.class); try { List<IndexToken> results = (List<IndexToken>) query.execute(); if (results.iterator().hasNext()) { for (IndexToken indexToken : results) { pm.deletePersistent(indexToken); } } } finally { query.closeAll(); }

The JSP file search.jsp contains an HTML form for entering a search query (see Figure 5). Here is the code that performs the search operation:

String query = ""; String results = "<b>Results:</b><br/>"; Object obj = request.getParameter("search"); if (obj != null) { query = "" + obj; List<SearchResult> hits = IndexToken.search(query); for (SearchResult hit : hits) { results += "<p>" + hit + "</p>"; } }

Figure 5. Search Results: The JSP file search.jsp contains an HTML form for entering a search query.

The toString method added to the SearchResults class formats the search results:

public String toString() { return url + " - " + score + ": " + title; }

A low-Cost Deployment Option

The Java App Engine platform offers a no-cost (or low cost for busy web sites) deployment option for Java developers. While the App Engine may not be the best deployment platform for some web applications, it is well worth the time to experiment with it and add another deployment option to your developer toolbox.



Mark Watson is a Java consultant and the author of 14 books on Java, artificial intelligence, C++, and intelligent agents.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap