servlet与jsp基础教程(9)-处理Cookie

发布时间:2020-03-02编辑:脚本学堂
servlet与jsp基础教程(9)-处理Cookie

9.4.实例:定制的搜索引擎界面
下面也是一个搜索引擎界面的例子,通过修改前面HTTP状态代码的例子得到。在这个Servlet中,用户界面是动态生成而不是由静态HTML文件提供的。Servlet除了负责读取表单数据并把它们发送给搜索引擎之外,还要把包含表单数据的Cookie发送给客户端。以后客户再次访问同一表单时,这些Cookie的值将用来预先填充表单,使表单自动显示最近使用过的数据。

SearchEnginesFrontEnd.java
该Servlet构造一个主要由表单构成的用户界面。第一次显示的时候,它和前面用静态HTML页面提供的界面差不多。然而,用户选择的值将被保存到Cookie(本页面将数据发送到CustomizedSearchEngines Servlet,由后者设置Cookie)。用户以后再访问同一页面时,即使浏览器是退出之后再启动,表单中也会自动填好上一次搜索所填写的内容。

注意该Servlet用到了ServletUtilities.java,其中getCookieValue前面已经介绍过,headWithTitle用于生成HTML页面的一部分。另外,这里也用到了前面已经说明的LongLiveCookie类,我们用它来创建作废期限很长的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" +
"HotBotn" +
"<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
前面的SearchEnginesFrontEnd Servlet把数据发送到CustomizedSearchEngines Servlet。本例在许多方面与前面介绍HTTP状态代码时的例子相似,区别在于,本例除了要构造一个针对搜索引擎的URL并向用户发送一个重定向应答之外,还要发送保存用户数据的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);
}
}