`
onehao
  • 浏览: 13011 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Servlet与JSP核心编程 ---- 9

阅读更多

8.11 使用cookie记录用户的偏好

 

cookie最常用的应用之一就是用来记录用户的偏好,对于简单的用户设置,可以直接将用户偏好存储在cookie中,对于更为复杂的应用,一般在cookie中存储唯一的用户标识,而将实际的偏好存储在数据库中。

 

• RegistrationForm servlet

– 使用cookie的值预先填写表单的字段

– 如果未找到相应的cookie则使用默认值

– 课程的后面将会在JSP中重做这项工作

• Registration servlet

– 基于接收到的请求参数创建cookie

– 如果得到所有的参数则显示这些值

– 如果任何参数缺失,则重定向到表单

 

package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Servlet that displays an HTML form to collect user's
* first name, last name, and email address. Uses cookies
* to determine the initial values of each of those
* form fields.
*/public class RegistrationForm extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String actionURL =
"/servlet/coreservlets.RegistrationServlet";
String firstName =
CookieUtilities.getCookieValue(request, "firstName", "");
String lastName =
CookieUtilities.getCookieValue(request, "lastName", "");
String emailAddress =
CookieUtilities.getCookieValue(request, "emailAddress",
"");
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
String title = "Please Register";
out.println
(docType +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<CENTER>\n" +
"<H1>" + title + "</H1>\n" +
"<FORM ACTION=\"" + actionURL + "\">\n" +
"First Name:\n" +
" <INPUT TYPE=\"TEXT\" NAME=\"firstName\" " +
"VALUE=\"" + firstName + "\"><BR>\n" +
"Last Name:\n" +
" <INPUT TYPE=\"TEXT\" NAME=\"lastName\" " +
"VALUE=\"" + lastName + "\"><BR>\n" +
"Email Address: \n" +
" <INPUT TYPE=\"TEXT\" NAME=\"emailAddress\" " +
"VALUE=\"" + emailAddress + "\"><P>\n" +
"<INPUT TYPE=\"SUBMIT\" VALUE=\"Register\">\n" +
"</FORM></CENTER></BODY></HTML>");
}
}

 

 

package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Servlet that processes a registration form containing
* a user's first name, last name, and email address.
* If all the values are present, the servlet displays the
* values. If any of the values are missing, the input
* form is redisplayed. Either way, the values are put
* into cookies so that the input form can use the
* previous values.
*/
public class RegistrationServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
boolean isMissingValue = false;
String firstName = request.getParameter("firstName");
if (isMissing(firstName)) {
firstName = "Missing first name";
isMissingValue = true;
}
String lastName = request.getParameter("lastName");
if (isMissing(lastName)) {
lastName = "Missing last name";
isMissingValue = true;
}
String emailAddress = request.getParameter("emailAddress");
if (isMissing(emailAddress)) {
emailAddress = "Missing email address";
isMissingValue = true;
}
Cookie c1 = new LongLivedCookie("firstName", firstName);
response.addCookie(c1);
Cookie c2 = new LongLivedCookie("lastName", lastName);
response.addCookie(c2);
Cookie c3 = new LongLivedCookie("emailAddress",
emailAddress);
response.addCookie(c3);
String formAddress =
"/servlet/coreservlets.RegistrationForm";
if (isMissingValue) {
response.sendRedirect(formAddress);
} else {
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
String title = "Thanks for Registering";
out.println
(docType +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<CENTER>\n" +
"<H1 ALIGN>" + title + "</H1>\n" +
"<UL>\n" +
" <LI><B>First Name</B>: " +
firstName + "\n" +
" <LI><B>Last Name</B>: " +
lastName + "\n" +
" <LI><B>Email address</B>: " +
emailAddress + "\n" +
"</UL>\n" +
"</CENTER></BODY></HTML>");
}
}
/** Determines if value is null or empty. */
private boolean isMissing(String param) {
return((param == null) ||
(param.trim().equals("")));
}
}

 

小结

• cookie涉及将名称/值对从服务器发送到浏览器

,并在之后访问相同的页面、站点或域时返回。

• 我们可以

– 跟踪会话(使用高层API)

– 对安全性要求较低的网站,使用cookie可以避免用户

每次都需要登录

– 根据用户的不同定制网站

– 有选择地投放内容或广告

• 设置cookie

– 调用Cookie的构造函数,设置时效,调用

response.addCookie

• 读取cookie

– 调用request.getCookie,检查得到的结果是否为null,

在数组中查找匹配的名称,使用相关的值

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics