JSP implementation HTTP Reply State instance

Source: Internet
Author: User
Tags html form variables query return string
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







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.