Custom Search Engine Interface (cookie) examples

Source: Internet
Author: User
Tags html page interface return
Cookie| Search Engine

Below is also an example of a search engine interface, by modifying the example of the previous HTTP status code. In this servlet, the user interface is dynamically generated rather than provided by static HTML files. In addition to being responsible for reading form data and sending it to search engines, the servlet sends cookies that contain form data to the client. When the customer accesses the same form again, the value of these cookies is used to pre-filled the form so that the form automatically displays the most recently used data.

Searchenginesfrontend.java

The servlet constructs a user interface that is primarily composed of forms. The first time it is displayed, it is similar to the interface provided in the previous static HTML page. However, the value that the user selects will be saved to the cookie (this page sends the data to the Customizedsearchengines Servlet, which sets the cookie). When a user accesses the same page later, the form will automatically fill in the contents of the previous search, even if the browser is exited and then started.

Note that the servlet uses Servletutilities.java, which Getcookievalue described earlier and headwithtitle used to generate part of the HTML page. In addition, we used the Longlivecookie class, which we have described earlier, to create a cookie with a long expiration date.
Package Hall;

Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.net.*;

public class Searchenginesfrontend extends HttpServlet {
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {
cookie[] cookies = request.getcookies ();
String searchstring =
Servletutilities.getcookievalue (Cookies,
"SearchString",
"Java programming");
String Numresults =
Servletutilities.getcookievalue (Cookies,
"Numresults",
"10");
String SearchEngine =
Servletutilities.getcookievalue (Cookies,
"SearchEngine",
"Google");
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
String title = "Searching the Web";
Out.println (title) + Servletutilities.headwithtitle
"<body bgcolor=\" #FDF5E6 \ ">\n" +
"

"\ n" +
"<form action=\"/servlet/hall. Customizedsearchengines\ ">\n" +
"<CENTER> \ n" +
"Search string:\n" +
"<input type=\" text\ "name=\" searchstring\ "\ n" +
"Value=\" "+ searchstring +" \ "> <BR> \ n" +
"Results to show per page:\n" +
"<input type=\" text\ "name=\" numresults\ "\ n" +
"Value=" + numresults + "size=3> <BR> \ n" +
"<input type=\" radio\ "name=\" searchengine\ "\ n" +
"Value=\" google\ "" +
Checked ("Google", SearchEngine) + ">\n" +
"Google |\n" +
"<input type=\" radio\ "name=\" searchengine\ "\ n" +
"Value=\" infoseek\ "" +
Checked ("InfoSeek", SearchEngine) + ">\n" +
"InfoSeek |\n" +
"<input type=\" radio\ "name=\" searchengine\ "\ n" +
"Value=\" lycos\ "" +
Checked ("Lycos", SearchEngine) + ">\n" +
"Lycos |\n" +
"<input type=\" radio\ "name=\" searchengine\ "\ n" +
"Value=\" hotbot\ "" +
Checked ("HotBot", SearchEngine) + ">\n" +
"Hotbot\n" +
"<BR> \ n" +
"<input type=\" submit\ "value=\" search\ ">\n" +
"</CENTER> \ n" +
"</FORM> \ n" +
"\ n" +
"</BODY> \ n" +
"</HTML> \ n");
}

private string checked (string name1, String name2) {
if (Name1.equals (name2))
Return ("CHECKED");
Else
Return ("");
}
}

Customizedsearchengines.java

The previous Searchenginesfrontend servlet sends data to the Customizedsearchengines servlet. This example is similar in many ways to the previous example of HTTP status code, except that this example sends cookies to save user data in addition to constructing a URL for the search engine and sending a redirect response to the user.
Package Hall;

Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.net.*;

public class Customizedsearchengines extends HttpServlet {
public void doget (HttpServletRequest request,
HttpServletResponse response)
Throws Servletexception, IOException {

String searchstring = Request.getparameter ("searchstring");
Cookie Searchstringcookie =
New Longlivedcookie ("SearchString", searchstring);
Response.addcookie (Searchstringcookie);
SearchString = Urlencoder.encode (searchstring);
String numresults = Request.getparameter ("Numresults");
Cookie Numresultscookie =
New Longlivedcookie ("Numresults", numresults);
Response.addcookie (Numresultscookie);
String searchengine = Request.getparameter ("SearchEngine");
Cookie Searchenginecookie =
New Longlivedcookie ("SearchEngine", searchengine);
Response.addcookie (Searchenginecookie);
searchspec[] Commonspecs = Searchspec.getcommonspecs ();
for (int i=0; i<commonspecs.length; i++) {
Searchspec searchspec = commonspecs[i];
if (Searchspec.getname (). Equals (SearchEngine)) {
String URL =
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);
}
}



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.