Package coreservlets;
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 {
String searchstring = request. getparameter ("searchstring ");
If (searchstring = NULL )//
(Searchstring. Length () = 0 )){
Reportproblem (response, "missing search string .");
Return;
}
// The urlencoder changes spaces to "+" signs and other
// Non-alphanumeric characters to "% XY", where xy is
// Hex Value of the ASCII (or ISO Latin-1) character.
// Browsers always URL-encode form values, so
// Getparameter method decodes automatically. But since
// We're just passing this on to another server, we need
// Re-encode it.
Searchstring = urlencoder. encode (searchstring );
String numresults = request. getparameter ("numresults ");
If (numresults = NULL )//
(Numresults. Equals ("0 "))//
(Numresults. Length () = 0 )){
Numresults = "10 ";
}
String searchengine =
Request. getparameter ("searchengine ");
If (searchengine = NULL ){
Reportproblem (response, "missing search engine name .");
Return;
}
Searchspec [] commonspecs = searchspec. getcommonspecs ();
For (INT I = 0; I <commonspecs. length; I ++ ){
Searchspec = commonspecs;
If (searchspec. getname (). Equals (searchengine )){
String url =
Searchspec. makeurl (searchstring, numresults );
Response. sendredirect (URL );
Return;
}
}
Reportproblem (response, "Unrecognized search engine .");
}
Private void reportproblem (httpservletresponse response,
String message)
Throws ioexception {
Response. senderror (response. SC _not_found,
"<H2>" + message + "</H2> ");
}
Public void dopost (httpservletrequest request,
Httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
}