ShowCookies

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

//读取客户端的Cookie

public class ShowCookies extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=gb2312");
    PrintWriter out = response.getWriter();
    String title = "Active Cookies";
    out.println("<html><head><title>获取客户端Cookie</title></head>" +
                "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
                "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
                "<TR BGCOLOR=\"#FFAD00\">\n" +
                "  <TH>Cookie Name\n" +
                "  <TH>Cookie Value");
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
      Cookie cookie;
      for(int i=0; i<cookies.length; i++) {
        cookie = cookies[i];
        out.println("<TR>\n" +
                    "  <TD>" + cookie.getName() + "</TD>\n" +
                    "  <TD>" + cookie.getValue() + "</TD></TR>\n" );
      }
    }
    out.println("</TABLE></BODY></HTML>");
  }
}

 

你可能感兴趣的:(html,servlet)