JSP Source code example 4 (search engine)

Source: Internet
Author: User
Tags alphanumeric characters
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);
}
}




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.