网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  java servlet 编程及应用之cookie的使用方法     
  文章作者:未知  文章来源:水木森林  
  查看:61次  录入:管理员--2007-11-16  
 

  cookie 是一小块可以嵌入http 请求和响应中的数据,它在服务器上产生,并作为响应头域的一部分返回用户。浏览器收到包含cookie 的响应后,会把cookie 的内容用“关键字/值” 对的形式写入到一个客户端专为存放cookie 的文本文件中。浏览器会把cookie 及随后产生的请求发给相同的服务器,服务器可以再次读取cookie 中存cookie 可以进行有效期设置,过期的cookie 不会发送给服务器。


  servlet api 提供了一个cookie 类,封装了对cookie 的一些操作。servlet 可以创建一个新的cookie,设置它的关键字、值及有效期等属性,然后把cookie 设置在httpservletresponse 对象中发回浏览器,还可以从httpservletrequest 对象中获取cookie。

  编程思路:cookie 在实际的servlet 编程中是很广泛应用,下面是一个从servlet 中获取cookie 信息的例子。

  showcookies.java 的源代码如下:

 

import javax.servlet.*;
import javax.servlet.http.*;

/**
* <p>this is a simple servlet that displays all of the
* cookies present in the request
*/

public class showcookies extends httpservlet
{
 public void doget(httpservletrequest req, httpservletresponse resp)
 throws servletexception, java.io.ioexception
 {

  // set the content type of the response
  resp.setcontenttype("text/html;charset=gb2312");

  // get the printwriter to write the response
  java.io.printwriter out = resp.getwriter();

  // get an array containing all of the cookies
  cookie cookies[] = req.getcookies();

  // write the page header
  out.println("<html>");
  out.println("<head>");
  out.println("<title>servlet cookie information</title>");
  out.println("</head>");
  out.println("<body>");

  if ((cookies == null) || (cookies.length == 0)) {
   out.println("没有 cookies ");
  }
  else {
   out.println("<center><h1>响应消息中的cookies 信息 </h1>");
   // display a table with all of the info
   out.println("<table border>");
   out.println("<tr><th>name</th><th>value</th>" + "<th>comment</th><th>max age</th></tr>");
   for (int i = 0; i < cookies.length; i++) {
    cookie c = cookies[i];
    out.println("<tr><td>" + c.getname() + "</td><td>" +
    c.getvalue() + "</td><td>" + c.getcomment() + "</td><td>" + c.getmaxage() + "</td></tr>");
  }
  out.println("</table></center>");
 }
 // wrap up
 out.println("</body>");
 out.println("</html>");
 out.flush();
}

/**
* <p>initialize the servlet. this is called once when the
* servlet is loaded. it is guaranteed to complete before any
* requests are made to the servlet
* @param cfg servlet configuration information
*/

public void init(servletconfig cfg)
throws servletexception
{
 super.init(cfg);
}

/**
* <p>destroy the servlet. this is called once when the servlet
* is unloaded.
*/

public void destroy()
{
 super.destroy();
}
}
 

  注意:cookie 进行服务器端与客户端的双向交流,所以它涉及到安全性问题。
 
 
上一篇: 基于java的idea加密算法探讨    下一篇: 实战java多线程编程精要之限制优先级
  相关文档
thinking in java第三版读书笔记-第八章:接口与内部类 11-17
对于初学者学习java语言的建议 11-17
基础--websphere下jms的配置和和应用 01-24
开发框架:spring技巧之活用factorybean 02-27
能够在非中文平台上显示汉字吗 11-17
soa存储库背后的力量 11-17
深入浅出基于java的责任链模式 11-17
根据ip找地址的java实现 11-17
springframework中面向方面编程二 11-17
ramdom的应用 11-17
grails + ejb domain models 11-17
java语言的一些关键特性 11-17
利用java操作excel文件 11-17
一个显示日期的简单小程序,主要是学习静态变量的用法 11-17
在jsp中使用javamail邮件发送收取(1) 11-17
eclipse插件的外挂安装方法 11-17
2006-2007年度java平台开发工具的应用状况 11-17
java开发中的事件驱动模型实例详解 11-17
linux 平台的 jvm 性能评测 11-17
weblogic uddi client api实例学习(一) 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息