当前位置: 代码迷 >> Web前端 >> Cookie的施用
  详细解决方案

Cookie的施用

热度:136   发布时间:2012-10-14 14:55:08.0
Cookie的使用

Cookie的使用

?

一、cookie的作用

在我们平常写的B/S程序中,会经常用到cookie,主要有以下一些作用:

1、?记录用户名和密码

以及该用户需要保存的一些信息,如购物购站,使用cookie,可以让用户自动登录到站点等。

2、?定制站点

可以使用cookie来记录用户的偏好。

3、?定向广告

可以使用cookie来记录用记经常访问的主题,并向他们显示与这些主题相关的广告。

?

二、向客户程序发送cookie

1、?创建cookie对象

Cookie c = new Cookie(“userId”,”landril”);

2、?设置最大时效,默认该cookie是存储在浏览器的内在中,用户关闭浏览器则被删除,下面的方法是将cookie存储在硬盘上。

c.setMaxAge(60*60*24);//一天,如果设置为0则是删除该cookie

3、?cookie放入到HTTP响应报头,可以使用HttpServletResponseaddCookie方法,此方法不修改之前指定的Set-Cookie报头,而是创建新的报头。

response.addCookie(c);

?????? 注意:设置cookie的步骤为创建cookie对象,设置最大时效,将cookie放入响应报头,即发送到客户程序,记住一定要将cookie发送到客户程序

?

三、从客户端读取cookie

1、?调用HttpServletRequestgetCookies得到一个Cookie对象的数组

2、?对数组进行循环,调用cookiegetName方法,获取具体的cookie的值

Cookie[] cookies = request.getCookies();

if(cookies != null){

for(int i=0;i<cookies.length;i++){

??????? Cookie c = cookies[i];

??????? if(“userId”.equals(c.getName())){

??????? System.out.println(c.getValue());

}

}

}

?

四、cookie的常用方法

1、?setComment()/getComment():指定或查找与该cookie相关的注释

2、?setDomain()/getDomain():设置或读取该cookie适用的域

3、?setMaxAge()/getMaxAge():操作cookie保留的时间,多长时间后过期

4、?getName():读取cookie的名称

5、?setPath()/getPath():设置或取得cookie适用的路径

cookie.setPath(“/”);指定服务器的所有页面都应该收到该cookie

6、?setSource()/getSource():指定cookie是否只能通过加密连接(SSL

默认false,表示cookie适用所有连接

7、?setValue()/getValue():指定或获取cookie的值

?

五、使用cookie

?

1RepeatServlet.java

public class RepeatServlet extends HttpServlet {

?

??? public void doGet(HttpServletRequest request, HttpServletResponse response)

?????? ??? throws ServletException, IOException {

??? ??? boolean newa = true;

??? ??? Cookie[] cookies = request.getCookies();

??? ??? if(cookies != null){

?????? ??? for (int i = 0; i < cookies.length; i++) {

?????????? ??? Cookie c = cookies[i];

?????????? ??? if ((c.getName().equals("repeat")) && (c.getValue().equals("true"))) {

?????????? ?????? newa = false;

?????????? ??? ??? break;

?????????? ??? }

?????????? }

?????? }

??????

??? ??? String title;

?????? if (newa) {

?????? ??? Cookie rtn = new Cookie("repeat"," true");

?????? ??? rtn.setMaxAge(60*60*24*365);

?????? ??? response.addCookie(rtn);

?????? ??? title = "First Welcome";

?????? } else {

?????? ??? title = "Welcom Back";

?????? }

??????

??? ??? response.setContentType("text/html");

??? ??? PrintWriter out = response.getWriter();

??? ??? out.println("<html><body><h3>");

??? ??? out.println(title);

??? ??? out.println("</h3></body></html>");

??? }

?

??? public void doPose(HttpServletRequest request, HttpServletResponse response)

?????? ??? throws ServletException, IOException {

??? ??? doGet(request,response);

??? }

}

?

2CookieUtil.java

public class CookieUtil{

?

??? public static String getValue(HttpServletRequest request,String cName,String value){

??? ??? Cookie[] cookies = request.getCookies();

??? ??? if(cookies != null){

?????? ??? for (int i = 0; i < cookies.length; i++) {

?????????? ??? Cookie cookie = cookies[i];

?????????? ??? if (cName.equals(cookie.getName())) {

?????????? ??? ??? return cookie.getValue();

?????????? ??? }

?????????? }

?????? }

??? ??? return value;

??? }

???

??? public static Cookie getCookie(HttpServletRequest request,String cName){

??? ??? Cookie[] cookies = request.getCookies();

??? ??? if(cookies != null){

?????? ??? for (int i = 0; i < cookies.length; i++) {

?????????? ??? Cookie cookie = cookies[i];

?????????? ??? if (cName.equals(cookie.getName())) {

?????????? ??? ??? return cookie;

?????????? ??? }

?????????? }

?????? }

??? ??? return null;

??? }

}

?

3AccessCountServle.java 记录访问数

public class AccessCountServlet extends HttpServlet {

?

??? public void doGet(HttpServletRequest request, HttpServletResponse response)

?????? ??? throws ServletException, IOException {

??????

??? ??? String acount = CookieUtil.getValue(request, "acc", "1");

?????? int count = Integer.parseInt(acount);

??? ??? Cookie cookie = new Cookie("acc",String.valueOf(count+1));

??? ??? cookie.setMaxAge(60*60*24*365);

??? ??? response.addCookie(cookie);

??????

??? ??? response.setContentType("text/html");

??? ??? PrintWriter out = response.getWriter();

??? ??? out.println("<html><body>Hello,the number is<h3>");

??? ??? out.println(count);

??? ??? out.println("</h3></body></html>");

??? }

???

??? public void doPost(HttpServletRequest request, HttpServletResponse response)

??? throws ServletException, IOException {

??? ??? doGet(request,response);

??? }

}

  相关解决方案