zoukankan      html  css  js  c++  java
  • 11月6日学习日志

    今天学习了servlet读取Cookie。

    要读取 Cookie,需要通过调用 HttpServletRequest 的 getCookies( ) 方法创建一个 javax.servlet.http.Cookie 对象的数组。然后循环遍历数组,并使用 getName() 和 getValue() 方法来访问每个 cookie 和关联的值。

    实例:

    package com.runoob.test;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.URLDecoder;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class ReadCookies
     */
    @WebServlet("/ReadCookies")
    public class ReadCookies extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public ReadCookies() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            Cookie cookie = null;
            Cookie[] cookies = null;
            // 获取与该域相关的 Cookie 的数组
            cookies = request.getCookies();
             
             // 设置响应内容类型
             response.setContentType("text/html;charset=UTF-8");
        
             PrintWriter out = response.getWriter();
             String title = "Delete Cookie Example";
             String docType = "<!DOCTYPE html>\n";
             out.println(docType +
                       "<html>\n" +
                       "<head><title>" + title + "</title></head>\n" +
                       "<body bgcolor=\"#f0f0f0\">\n" );
              if( cookies != null ){
                out.println("<h2>Cookie 名称和值</h2>");
                for (int i = 0; i < cookies.length; i++){
                   cookie = cookies[i];
                   if((cookie.getName( )).compareTo("name") == 0 ){
                        cookie.setMaxAge(0);
                        response.addCookie(cookie);
                        out.print("已删除的 cookie:" + 
                                     cookie.getName( ) + "<br/>");
                   }
                   out.print("名称:" + cookie.getName( ) + ",");
                   out.print("值:" +  URLDecoder.decode(cookie.getValue(), "utf-8") +" <br/>");
                }
             }else{
                 out.println(
                   "<h2 class=\"tutheader\">No Cookie founds</h2>");
             }
             out.println("</body>");
             out.println("</html>");
            }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
  • 相关阅读:
    1077. 互评成绩计算 (20)
    1076. Wifi密码 (15)
    c语言之利用指针复制字符串的几种形式
    c语言之利用函数实现字符串的复制
    c语言之字符串中字符的存取方法
    E0144"const char *" 类型的值不能用于初始化 "char *" 类型的实体的三种解决方法
    c语言之使用指针*和地址&在二维数组中表示的含义
    c语言之指向二维数组元素的指针变量
    c语言之在函数内部改变数组的值从而影响外部数组的四种方式
    c语言之使用指针将数组进行反转
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14159981.html
Copyright © 2011-2022 走看看