Js
In this example, the first HTML form that appears is used to select search engines, search strings, and the number of search results displayed per page. After the form is submitted, the servlet extracts the three variables, constructs the URL that contains the variables according to the search engine's requirements, and then redirects the user to the URL. If the user does not select the search engine correctly, or use another form to send a name of a search engine that you do not know, return a 404 page that prompts the search engine to find it.
Searchengines.java
Note: This servlet uses the Searchspec class given later, and Searchspec's function is to construct URLs suitable for different search engines.
Package Hall;
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.net.*;
public class Searchengines extends HttpServlet {
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
GetParameter automatically decodes URL-encoded query strings. As we
To send the query string to another server, use the
Urlencoder for URL encoding
String searchstring =
Urlencoder.encode (Request.getparameter ("searchstring"));
String Numresults =
Request.getparameter ("Numresults");
String SearchEngine =
Request.getparameter ("SearchEngine");
searchspec[] Commonspecs = Searchspec.getcommonspecs ();
for (int i=0; i<commonspecs.length; i++) {
Searchspec searchspec = commonspecs[i];
if (Searchspec.getname (). Equals (SearchEngine)) {
String URL =
Response.encodeurl (Searchspec.makeurl (SearchString,
Numresults));
Response.sendredirect (URL);
Return
}
}
Response.senderror (response. Sc_not_found,
"No recognized search engine specified.");
}
public void DoPost (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}
Searchspec.java
Package Hall;
Class Searchspec {
Private String name, BaseURL, Numresultssuffix;
private static searchspec[] Commonspecs =
{New Searchspec ("Google",
"Http://www.google.com/search?q=",
"&num="),
New Searchspec ("InfoSeek",
"Http://infoseek.go.com/Titles?qt=",
"&nh="),
New Searchspec ("Lycos",
"Http://lycospro.lycos.com/CGI-bin/pursuit?query=",
"&maxhits="),
New Searchspec ("HotBot",
"Http://www.hotbot.com/?MT=",
"&dc=")
};
Public Searchspec (String name,
String BaseURL,
String Numresultssuffix) {
THIS.name = name;
This.baseurl = BaseURL;
This.numresultssuffix = Numresultssuffix;
}
public string Makeurl (string searchstring, String numresults) {
Return (BaseURL + searchstring + numresultssuffix + numresults);
}
Public String GetName () {
return (name);
}
public static searchspec[] Getcommonspecs () {
return (COMMONSPECS);
}
}
Searchengines.html
The following is an HTML form that invokes the above servlet.
! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en"
Access multiple search engines
Search keywords:
Display several query results per page:
value=10 size=3>
Value= "Google" >
Google |
Value= "InfoSeek" >
InfoSeek |
Value= "Lycos" >
Lycos |
Value= "HotBot" >
HotBot