JSP source code example 4 (search engine)

Source: Internet
Author: User
Tags alphanumeric characters

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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.