马士兵老师的~~
cookie是服务器将信息保存在客户端的一个表示方式 名-值
服务器只能写入文档文件
每个浏览器都有唯一的标识号 且每个浏览器只允许访问与自身相关的cookie的内容
cookie分为两种,一种为浏览器型,不设置生命周期或者设为-1时,cookie的存在与浏览器该页面是否存在相关,另一种为永久型,即设置了生命周期
定义一个cookie setcookie 应利用response来设置 而获取cookie 应该使用 request来获取
页面对cookie的访问与页面间的“继承”关系有关。。。
代码
SetCookies.java、
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SetCookies extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
for(int i=0;i<3;i++)
{
Cookie cookie=new Cookie("Session-Cookie-"+i,"Cookie-Value-S"+i);
response.addCookie(cookie);
cookie=new Cookie("Persistent-Cookie-"+i,"Cookie-Value-P"+i);
cookie.setMaxAge(3600);
response.addCookie(cookie);
}
response.setContentType("text/html;charset=gb2312");
PrintWriter pw=response.getWriter();
pw.println("<html><head><title>设置Cookies</title></head><body bgcolor='#FDF5E6'>");
pw.println("<p>There are six Cookies associated with this page.To see them," +
" visit the <a href='ShowCookies'>ShowCookies servlet</a></p>");
pw.println("<p>Three of the cookies are associated only with the current session,while three are persistent." +
" Quit the browser,restart,ans return to the ShowCookies servlet to verify that the three long-lived ones persist across sessions;</p>");
pw.println("</body></html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
ShowCookies.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowCookies extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie []cookies=request.getCookies();
response.setContentType("text/html;charset=gb2312");
PrintWriter pw=response.getWriter();
pw.println("<html><head><title>获取客户端Cookies</title></head><body bgcolor='#FDF5E6'>");
pw.println("<h1 align='center'>Active Cookies</h1><br/>");
pw.println("<table align='center' border=1 ><tr><td>Cookie Name</td><td>Cookie Value</td></tr>");
for(Cookie cookie:cookies)
{
pw.println("<tr><td>"+cookie.getName()+"</td><td>"+cookie.getValue()+"</td></tr>");
}
pw.println("</table></body></html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
web.xml配置
<servlet>
<servlet-name>SetCookies</servlet-name>
<servlet-class>SetCookies</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SetCookies</servlet-name>
<url-pattern>/servlet/SetCookies</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ShowCookies</servlet-name>
<servlet-class>ShowCookies</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowCookies</servlet-name>
<url-pattern>/servlet/ShowCookies</url-pattern>
</servlet-mapping>
页面