转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> JAVA开发 >> 正文
Java Servlet 编程及应用之五         ★★★★

Java Servlet 编程及应用之五

作者:闵涛 文章来源:闵涛的学习笔记 点击数:633 更新时间:2009/4/22 23:28:16
天极IT资讯短信服务 电脑小技巧
资费:包月5元
手机
介绍:细处着手,巧处用功。高手和菜鸟之间的差别就是:高手什么都知道,菜鸟知道一些。电脑小技巧收集最新奇招高招,让你轻松踏上高手之路。

Java关键字导航 网络 J2ME 手机游戏 JavaCard Struts 游戏 分析器 JAAS EJB JavaMail 设计模式 J2EE
  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 进行服务器端与客户端的双向交流,所以它涉及到安全性问题。


[网页制作]JavaScript的三种消息框:警告框、确认框、提示框  [网页制作]JavaScript运算符介绍
[网页制作]JavaScript比较和逻辑运算符介绍  [网页制作]script中的language=javascript和type=text/javas…
[JAVA开发]Java连接各种数据库的实例  [办公软件]浅谈Linux 下Java 1.5 汉字方块问题解决方法
[系统软件]Regular Expression 正则表达式-2 (Java)  [C语言系列]sqlserver在JAVA中的应用
[网页制作]Dreamweaver MX实例:构建Java插入模块  [网页制作]使用Dreamweaver轻松构建Java插入模块
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台