js| Search Engine
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 the
Hex value of the ASCII (or ISO Latin-1) character.
Browsers always url-encode form values, so the
GetParameter method decodes automatically. But since
We ' re just passing this in to another server, and we need to
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 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);
}
}