Custom search engine interface (cookie) examples
Abstract: customized search engine interface (cookie) examples
<tr> <td>
Below is a search engine interface for example, by modifying the front of HTTP status code by example. In this Servlet, the user interface is dynamically generated rather than the static HTML files provided. Servlet addition to the reading of the form data and send them to search engines, but also to contain the Cookie form data sent to the client. After the visit, the same form again, the value of these Cookie will be used to pre-filled form, the form automatically display recently used data.
SearchEnginesFrontEnd.java
The Servlet structure constitutes a major form from the user interface. Showing the first time when it and in front of static HTML pages provided by the interface almost. However, users select a value will be saved to Cookie (This page will be sent to CustomizedSearchEngines Servlet data, the latter set up by Cookie). After the user to visit the same page, even if the browser is activated after withdrawal, will be automatically filled in the form, a search on the content of the fill.
Attention to the use of the Servlet ServletUtilities.java, getCookieValue have already introduced, for generating HTML pages headWithTitle part. In addition, this also has been used in the preceding note LongLiveCookie category, we use it to create a void for a long period of Cookie.
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 (ServletUtilities.headWithTitle (title) +
"<BODY BGCOLOR=\"#FDF5E6\"> \ n" +
"<H1 ALIGN=\"CENTER\"> Searching the Web </ H1> \ 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
In front of SearchEnginesFrontEnd Servlet sends the data to the CustomizedSearchEngines Servlet. In many of the cases described above with HTTP status code examples similar, the difference being, in this case in addition to constructing a search engine to the URL redirect users to send a response, the preservation of user data sent to the Cookies.
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);
)
)
↑ Back
Tags: examples, interface, java interface, search






