Google on your WAP phone using Java Server Pages
Google on your WAP phone using Java Server Pages
| Author: | Ivan Idris |
| Submission Date: | 2006-04-20 |
| Website: | |
| Email: |
Introduction
Without doubt Google is the best search engine of this millenium. The Google people are constantly improving and adding services. They have created a Java API at http://www.google.com/apis/, which lets you access their search results and the good news is that it is totally free! It is essentially a Web Service, but you only need to know Java to use the API. You can search the Internet, get cached pages or use the Google spellchecker. This tutorial focuses on the search functionality. I asked myself, whether it will be possible to use Google with a mobile phone. There are of course a few obstacles here. First there is the bandwidth limitation and also not all websites are suited for mobile browsers. One of the protocols that allow mobile browsing is WAP. The webpages will have to be written in WML. More about WML later in this tutorial. If you have a website, you will probably want to know how your website ranks on Google for certain keywords. Now you can use an application, that I have built and which I will use as an example in this tutorial. WML WML is based on XML and optimized for requirements of mobile phones. One of the optimization strategies is to split requested data and display it with cards. So one long page becomes a card deck of which you see the top card on your mobile. It is up to the developer to provide navigation showing one card at a time. Here is a WML page in JSP style:
<%@ page language=\"java\"
contentType=\"text/vnd.wap.wml\" %>
<?xml version=\"1.0\"?>
<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"
\"http://www.wapforum.org/DTD/wml_1.1.xml\">
<wml>
<card id=\"searchrank\">
<do type=\"accept\">
<go href=\"showrankWML.jsp\" method=\"post\">
<postfield name=\"url\" value=\"$(url)\" />
<postfield name=\"keywords\" value=\"$(keywords)\" />
</go>
</do>
<p>URL:
<input type=\"text\" name=\"url\"
maxlength=\"20\" format=\"*m\" />
</p>
<p>Keywords:
<input type=\"text\" name=\"keywords\"
maxlength=\"20\" format=\"*m\" />
</p>
</card>
</wml>
Explanation
The first line tells the server that the content type of the page is WML.
<%@ page language=\"java\"
contentType=\"text/vnd.wap.wml\" %>
Next line declares it XML.
<?xml version=\"1.0\"?>
Next line defines the DTD.
<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"
\"http://www.wapforum.org/DTD/wml_1.1.xml\">
The root element is wml then you see one card tag. You can have several cards here. There are few tags that you will be familiar with - p and input. The input tag however has an attribute 'format'. The format indicates what kind of characters can be entered in the fields. In this case all characters are accepted. This page displays a form, which is posted to another jsp. There are two parameters - the URL of a website we want to rank and keywords. The do tag defines an action for a pressed key and the go and postfields indicate which values to send. In this case, when the user presses OK after filling in all the parameters – the form data is posted to showrankWML.jsp just like with a normal HTML form.
Googling in Java style
Googling with Java requires installing the Google SOAP packages. Also you will have to request a developer key. Don't worry you get it all for free. Here is the JSP search page in WML format:
<%@ page language=\"java\" contentType=\"text/vnd.wap.wml\" %>
<%@ page import = \"com.google.soap.search.GoogleSearch\"%>
<%@ page import = \"com.google.soap.search.GoogleSearchResult\"%>
<%@ page import = \"com.google.soap.search.GoogleSearchFault\"%>
<%@ page import = \"com.google.soap.search.GoogleSearchResultElement\"%>
<%
String searchTerm = request.getParameter(\"keywords\");
String theURL = \"\";
String theTitle = \"\";
int pos = 1000;
try {
GoogleSearch search = new GoogleSearch();
search.setKey(\"-- your key here please-- \");
search.setQueryString(searchTerm);
for(int j = 0; j < 5; j++) {
int startResult = j * 10;
search.setStartResult(startResult);
int maxResult = 10;
search.setMaxResults(maxResult);
GoogleSearchResult result = search.doSearch();
GoogleSearchResultElement[] resultElements =
result.getResultElements();
this.getServletContext().log(
\"resultElements length: \"+ searchTerm);
int startIndex = result.getStartIndex() - 1
- startResult;
int endIndex = result.getEndIndex() - 1
- startResult;
for (int i = startIndex; i <= endIndex; i++) {
GoogleSearchResultElement resultElement =
resultElements[i];
String title = resultElement.getTitle();
String url = resultElement.getURL();
if(url.matches(\".*\" +
request.getParameter(\"url\")
+ \".*\")) {
pos = (i + 1 + j * 10);
theTitle = title;
theURL = url;
break;
}
}
}
} catch (GoogleSearchFault gsf) {
this.getServletContext().log(\"Google Search Fault: \" +
gsf.getMessage());
}
%>
<?xml version=\"1.0\"?>
<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"
\"http://www.wapforum.org/DTD/wml_1.1.xml\">
<wml>
<card id=\"showposition\">
<p>Pos: <%= pos %></p>
<p>URL: <%= theURL %></p>
</card>
</wml>
The code speaks for itself. You will first need to set the parameters of your search by registering your developer key and defining the keywords to search for. The search results are stored in a GoogleSearchResultElement array. In this case I am only interested in the URL and title of a page, so I get those with the getTitle() and getURL() methods of the GoogleSearchResultElement class.
Google lets you get 10 search results for one query. Obviously this isn't enough, because probably your webpages will not be in the top 10 all the time. If they were you wouldn't need this application anyway. So the search is performed multiple times, by increasing the start index of the search with every iteration. All we need to do now is to display the rank position. We only need one card for that. Ok, our application is ready. Now you can rank your webpages with Google. Happy Googling!
| Rate this article: Lowest = 1 to Highest = 5 |
|
| View PDF Bookmark Printer Friendly | |
