📄 aspcool_com--九、处理cookie.htm
字号:
<BR> package hall; <BR>
<BR> import java.io.*; <BR> import
javax.servlet.*; <BR> import javax.servlet.http.*;
<BR> import java.net.*; <BR>
<BR> public class SearchEnginesFrontEnd extends
HttpServlet { <BR> public void doGet(HttpServletRequest
request, <BR> HttpServletResponse response)
<BR> throws ServletException, IOException {
<BR> Cookie[] cookies = request.getCookies();
<BR> String searchString = <BR>
ServletUtilities.getCookieValue(cookies, <BR>
"searchString", <BR> "Java Programming");
<BR> String numResults = <BR>
ServletUtilities.getCookieValue(cookies, <BR>
"numResults", <BR> "10"); <BR> String
searchEngine = <BR>
ServletUtilities.getCookieValue(cookies, <BR>
"searchEngine", <BR> "google"); <BR>
response.setContentType("text/html"); <BR> PrintWriter
out = response.getWriter(); <BR> String title =
"Searching the Web"; <BR>
out.println(ServletUtilities.headWithTitle(title) + <BR>
"<BODY BGCOLOR=\"#FDF5E6\">\n" + <BR> "<H1
ALIGN=\"CENTER\">Searching the Web</H1>\n" + <BR> "\n" +
<BR> "<FORM
ACTION=\"/servlet/hall.CustomizedSearchEngines\">\n" +
<BR> "<CENTER>\n" + <BR> "Search String:\n"
+ <BR> "<INPUT TYPE=\"TEXT\" NAME=\"searchString\"\n" +
<BR> " VALUE=\"" + searchString + "\"><BR>\n" +
<BR> "Results to Show Per Page:\n" + <BR>
"<INPUT TYPE=\"TEXT\" NAME=\"numResults\"\n" + <BR> "
VALUE=" + numResults + " SIZE=3><BR>\n" + <BR> "<INPUT
TYPE=\"RADIO\" NAME=\"searchEngine\"\n" + <BR> "
VALUE=\"google\"" + <BR> checked("google", searchEngine)
+ ">\n" + <BR> "Google |\n" + <BR> "<INPUT
TYPE=\"RADIO\" NAME=\"searchEngine\"\n" + <BR> "
VALUE=\"infoseek\"" + <BR> checked("infoseek",
searchEngine) + ">\n" + <BR> "Infoseek |\n" +
<BR> "<INPUT TYPE=\"RADIO\" NAME=\"searchEngine\"\n" +
<BR> " VALUE=\"lycos\"" + <BR>
checked("lycos", searchEngine) + ">\n" + <BR> "Lycos
|\n" + <BR> "<INPUT TYPE=\"RADIO\"
NAME=\"searchEngine\"\n" + <BR> " VALUE=\"hotbot\"" +
<BR> checked("hotbot", searchEngine) + ">\n" +
<BR> "HotBot\n" + <BR> "<BR>\n" +
<BR> "<INPUT TYPE=\"SUBMIT\" VALUE=\"Search\">\n" +
<BR> "</CENTER>\n" + <BR> "</FORM>\n" +
<BR> "\n" + <BR> "</BODY>\n" +
<BR> "</HTML>\n"); <BR> } <BR>
<BR> private String checked(String name1, String name2)
{ <BR> if (name1.equals(name2)) <BR>
return(" CHECKED"); <BR> else <BR>
return(""); <BR> } <BR> } <BR>
<BR> <BR> <BR>
<BR> CustomizedSearchEngines.java <BR>
<BR> 前面的SearchEnginesFrontEnd
Servlet把数据发送到CustomizedSearchEngines
Servlet。本例在许多方面与前面介绍HTTP状态代码时的例子相似,区别在于,本例除了要构造一个针对搜索引擎的URL并向用户发送一个重定向应答之外,还要发送保存用户数据的Cookies。
<BR> package hall; <BR>
<BR> import java.io.*; <BR> import
javax.servlet.*; <BR> import javax.servlet.http.*;
<BR> import java.net.*; <BR>
<BR> public class CustomizedSearchEngines extends
HttpServlet { <BR> public void doGet(HttpServletRequest
request, <BR> HttpServletResponse response)
<BR> throws ServletException, IOException {
<BR> <BR> String searchString =
request.getParameter("searchString"); <BR> Cookie
searchStringCookie = <BR> new
LongLivedCookie("searchString", searchString); <BR>
response.addCookie(searchStringCookie); <BR>
searchString = URLEncoder.encode(searchString); <BR>
String numResults = request.getParameter("numResults");
<BR> Cookie numResultsCookie = <BR> new
LongLivedCookie("numResults", numResults); <BR>
response.addCookie(numResultsCookie); <BR> String
searchEngine = request.getParameter("searchEngine");
<BR> Cookie searchEngineCookie = <BR> new
LongLivedCookie("searchEngine", searchEngine); <BR>
response.addCookie(searchEngineCookie); <BR>
SearchSpec[] commonSpecs = SearchSpec.getCommonSpecs();
<BR> for(int i=0; i<commonSpecs.length; i++) {
<BR> SearchSpec searchSpec = commonSpecs[i];
<BR> if (searchSpec.getName().equals(searchEngine)) {
<BR> String url = <BR>
searchSpec.makeURL(searchString, numResults); <BR>
response.sendRedirect(url); <BR> return;
<BR> } <BR> } <BR>
response.sendError(response.SC_NOT_FOUND, <BR> "No
recognized search engine specified."); <BR> }
<BR> <BR> public void
doPost(HttpServletRequest request, <BR>
HttpServletResponse response) <BR> throws
ServletException, IOException { <BR> doGet(request,
response); <BR> } <BR> } <BR>
<BR> <BR> <BR>
</P></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" bgColor=#ffcc99
border=0><TBODY>
<TR>
<TD height=24>
<CENTER>相关文章</CENTER></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=0 width="100%" align=top bgColor=#ffffcc
border=0>
<TBODY>
<TR>
<TD vAlign=top width="50%">前1篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=338&bbsuser=jsp"
target=_blank>八、设置HTTP应答头</A> <BR>前2篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=337&bbsuser=jsp"
target=_blank>七、HTTP应答状态</A> <BR>前3篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=336&bbsuser=jsp"
target=_blank>六、访问CGI变量</A> <BR>前4篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=335&bbsuser=jsp"
target=_blank>五、读取HTTP请求头</A> <BR></TD>
<TD vAlign=top width="50%">后1篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=340&bbsuser=jsp"
target=_blank>十、会话状态</A> <BR>后2篇 <A
href="http://www.aspcool.com/lanmu/dot.asp?ID=341&bbsuser=jsp"
target=_blank>十一、JSP及语法概要</A> <BR></TD></TR></TBODY></TABLE><BR>
<CENTER><A
href="http://www.aspcool.com/lanmu/republic.asp?ID=339&bbsuser=jsp&motive=九、处理Cookie">发表评论</A>
<A
href="http://www.aspcool.com/lanmu/register.asp">注册新用户</A></CENTER><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" bgColor=#ff8080
border=0><TBODY>
<TR>
<TD width="50%" height=24>
<CENTER>对该文的评论</CENTER></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<SCRIPT language=JavaScript
src="C:\Documents and Settings\selin\桌面\ASPcool_com--九、处理Cookie.files\ads(1).htm"
type=text/JavaScript> </SCRIPT>
<BR><BR>
<TABLE cellSpacing=0 cellPadding=0 width=768 bgColor=#ffe7ce border=0>
<TBODY>
<TR>
<TD>
<P align=right><A href="http://www.aspcool.com/">返回首页</A> <A
href="javascript:window.close()">关闭窗口</A></P></TD></TR></TBODY></TABLE>
<TABLE height=27 cellSpacing=0 cellPadding=0 width=768 border=0>
<TBODY>
<TR bgColor=#c4ecff>
<TD width="100%">
<P align=left><STRONG><FONT color=#6f6fb7> 导航</FONT></STRONG>
>> <A href="http://www.aspcool.com/">ASP酷首页</A>-<A
href="http://www.chaxiu.com/">茶秀虚拟社区</A>-<A
href="http://www.aspcool.com/freebbs/apply.asp">申请BBS</A>-<A
href="http://www.aspcool.com/lanmu/mybook.htm">免费代码教程</A>- <A
href="http://www.aspcool.com/guestbook/apply.asp">申请留言板</A> -<A
href="http://www.aspcool.com/lanmu/browse.asp?bbsuser=jsp"> JSP教程专栏</A>
</P></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=773 bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD><A href="http://www.aspcool.com/"><IMG height=60
src="ASPcool_com--九、处理Cookie.files/top.gif" width=140 border=0></A></TD>
<TD width=10></TD>
<TD align=left>
<CENTER><IFRAME name=inpop marginWidth=0 marginHeight=0
src="ASPcool_com--九、处理Cookie.files/ad.htm" frameBorder=0 width=468
scrolling=no height=60 0?></IFRAME></CENTER></TD>
<TD width=10></TD>
<TD align=right width=150>
<DIV align=center><A href="http://www.aspcool.com/"><IMG height=60
src="ASPcool_com--九、处理Cookie.files/top.gif" width=140 border=0></A>
</DIV></TD></TR></TBODY></TABLE><BR>
<TABLE cellSpacing=0 cellPadding=0 width=773 align=center bgColor=#3366cc
border=0>
<TBODY>
<TR>
<TD> </TD></TR></TBODY></TABLE><BR>
<CENTER><A href="http://www.aspcool.com/about.asp">关于本站<A> - <A
href="http://www.aspcool.com/ads.asp">广告联系<A> - <A
href="http://bbs.aspcool.com/">技术论坛<A> - <A
href="http://www.aspcool.com/help.asp">使用说明<A> - <A
href="http://www.aspcool.com/copyright.asp">版权说明<A> - <A
href="http://www.aspcool.com/link.asp">联系方式<A>
<P class=page align=center>© 2003 版权所有 ASP酷技术网(<A href="http://www.aspcool.com/"
target=_blank>http://www.aspcool.com/</A>) <BR>技术问题请到<A
href="http://bbs.aspcool.com/" target=_blank>技术论坛</A> <A
href="http://blog.joycode.com/tim/contact.aspx" target=_blank>给站长发信</A><BR></P>
<SCRIPT language=javascript>
scroll(0,110)
</SCRIPT>
<SCRIPT src="ASPcool_com--九、处理Cookie.files/adleft.js"></SCRIPT>
<SCRIPT src="ASPcool_com--九、处理Cookie.files/adright.js"></SCRIPT>
</CENTER></CENTER></CENTER></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -