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 进行服务器端与客户端的双向交流,所以它涉及到安全性问题。
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 进行服务器端与客户端的双向交流,所以它涉及到安全性问题。
解决方案 »
- action向 JSP页面传值问题
- jsp与activex间是如何相互传值的? 40分
- 添加到项目中报错了..
- 求组各位大侠,一个关于AJAX的校园新闻发布系统!
- doget and dopost的调用?
- 请教javabean放置tomcat的什么地方?
- 在extends SimpleFormController中onSubmit(Object command)能执行,而 onSubmit(HttpServletRequest不执行, 直接跳到"successView"
- 求助;jsp 文件上传
- 如何用jsp实现在网上下载软件和资料
- 相同的JGroups程序在本机启动两个Eclipse可以正常互通消息,但是放到局域网中就无法发送消息了
- <<struts in action >>????
- 搞不清楚这段程序是如何与数据库连接的??
把真实的COOKIE发到客户端的话,安全性是不是降低了那??
是发给客户端一个假的COOKIE好不好那???
谢谢
把真实的COOKIE发到客户端的话,安全性是不是降低了那??
是发给客户端一个假的COOKIE好不好那???
谢谢基本上说是的
如果你登录网站的时候,选择了保存cookie
那么你可以在客户端的机子上找到cookie文件
里面有你的用户和密码,
虽然有这些不太安全的因素,但session跟踪一定要有cookie支持或url重写支持
用户可以设定自己登录网站时的界面风格等,这些可以保存在cookie里等
在TOMCAT中为什么SERVLET要存如XML中那??
XML为什么要和JSP结合那??
XML在 JSP中的作用又是什么?
STRUT里面也用到了XML?/
到底XML是用来给处理文件定向吗,是用来帮忙找SERVLET文件吗?/刚学JSP,看到XML有点晕,请问大家了,谢谢哦
filippo1980(管药师★吾辈岂是池中物,一遇风云便化龙) ( 70)
: ForrestShao238(口木子) ( 20) : ForrestShao238(口木子) (10 )