选择显示字体大小

java servlet 编程及应用之cookie的使用方法

  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   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons