<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FServlet%2FCookie</id>
		<title>Java Tutorial/Servlet/Cookie - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FServlet%2FCookie"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Servlet/Cookie&amp;action=history"/>
		<updated>2026-04-16T15:40:12Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/Servlet/Cookie&amp;diff=4894&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Servlet/Cookie&amp;diff=4894&amp;oldid=prev"/>
				<updated>2010-06-01T05:06:52Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:06, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/Servlet/Cookie&amp;diff=4893&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/Servlet/Cookie&amp;diff=4893&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Add Cookie Servlet ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.PrintWriter;&lt;br /&gt;
import javax.servlet.ServletException;&lt;br /&gt;
import javax.servlet.http.Cookie;&lt;br /&gt;
import javax.servlet.http.HttpServlet;&lt;br /&gt;
import javax.servlet.http.HttpServletRequest;&lt;br /&gt;
import javax.servlet.http.HttpServletResponse;&lt;br /&gt;
public class AddCookieServlet extends HttpServlet {&lt;br /&gt;
  public void doPost(HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;
      throws ServletException, IOException {&lt;br /&gt;
    String data = request.getParameter(&amp;quot;data&amp;quot;);&lt;br /&gt;
    Cookie cookie = new Cookie(&amp;quot;MyCookie&amp;quot;, data);&lt;br /&gt;
    response.addCookie(cookie);&lt;br /&gt;
    response.setContentType(&amp;quot;text/html&amp;quot;);&lt;br /&gt;
    PrintWriter pw = response.getWriter();&lt;br /&gt;
    pw.println(&amp;quot;&amp;lt;B&amp;gt;MyCookie has been set to&amp;quot;);&lt;br /&gt;
    pw.println(data);&lt;br /&gt;
    pw.close();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Get Cookies Servlet ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.PrintWriter;&lt;br /&gt;
import javax.servlet.ServletException;&lt;br /&gt;
import javax.servlet.http.Cookie;&lt;br /&gt;
import javax.servlet.http.HttpServlet;&lt;br /&gt;
import javax.servlet.http.HttpServletRequest;&lt;br /&gt;
import javax.servlet.http.HttpServletResponse;&lt;br /&gt;
public class GetCookiesServlet extends HttpServlet {&lt;br /&gt;
  public void doGet(HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;
      throws ServletException, IOException {&lt;br /&gt;
    Cookie[] cookies = request.getCookies();&lt;br /&gt;
    response.setContentType(&amp;quot;text/html&amp;quot;);&lt;br /&gt;
    PrintWriter pw = response.getWriter();&lt;br /&gt;
    pw.println(&amp;quot;&amp;lt;B&amp;gt;&amp;quot;);&lt;br /&gt;
    for (int i = 0; i &amp;lt; cookies.length; i++) {&lt;br /&gt;
      String name = cookies[i].getName();&lt;br /&gt;
      String value = cookies[i].getValue();&lt;br /&gt;
      pw.println(&amp;quot;name = &amp;quot; + name + &amp;quot;; value = &amp;quot; + value);&lt;br /&gt;
    }&lt;br /&gt;
    pw.close();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Get/Set Cookie ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import javax.servlet.*;&lt;br /&gt;
import javax.servlet.http.*;&lt;br /&gt;
public class MyServlet extends HttpServlet {&lt;br /&gt;
    public void doGet(HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;
    throws IOException, ServletException&lt;br /&gt;
    {&lt;br /&gt;
        response.setContentType(&amp;quot;text/html&amp;quot;);&lt;br /&gt;
        PrintWriter out = response.getWriter();&lt;br /&gt;
        &lt;br /&gt;
        out.println(&amp;quot;&amp;lt;HTML&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;HEAD&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;TITLE&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;A Web Page&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/TITLE&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/HEAD&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;BODY&amp;quot;);&lt;br /&gt;
        Cookie[] cookies = request.getCookies();&lt;br /&gt;
        boolean foundCookie = false;&lt;br /&gt;
        for(int i = 0; i &amp;lt; cookies.length; i++) { &lt;br /&gt;
            Cookie cookie1 = cookies[i];&lt;br /&gt;
            if (cookie1.getName().equals(&amp;quot;color&amp;quot;)) {&lt;br /&gt;
                out.println(&amp;quot;bgcolor = &amp;quot; + cookie1.getValue());&lt;br /&gt;
                foundCookie = true;&lt;br /&gt;
            }&lt;br /&gt;
        }  &lt;br /&gt;
        if (!foundCookie) {&lt;br /&gt;
            Cookie cookie1 = new Cookie(&amp;quot;color&amp;quot;, &amp;quot;cyan&amp;quot;);&lt;br /&gt;
            cookie1.setMaxAge(24*60*60);&lt;br /&gt;
            response.addCookie(cookie1); &lt;br /&gt;
        }&lt;br /&gt;
        out.println(&amp;quot;&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;H1&amp;gt;Setting and Reading Cookies&amp;lt;/H1&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;This page will set its background color using a cookie when reloaded.&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/BODY&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/HTML&amp;gt;&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  List Servlet Cookie Information ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.PrintWriter;&lt;br /&gt;
import javax.servlet.ServletException;&lt;br /&gt;
import javax.servlet.http.Cookie;&lt;br /&gt;
import javax.servlet.http.HttpServlet;&lt;br /&gt;
import javax.servlet.http.HttpServletRequest;&lt;br /&gt;
import javax.servlet.http.HttpServletResponse;&lt;br /&gt;
public class Cookies extends HttpServlet {&lt;br /&gt;
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,&lt;br /&gt;
      IOException {&lt;br /&gt;
    resp.setContentType(&amp;quot;text/html&amp;quot;);&lt;br /&gt;
    req.getSession();&lt;br /&gt;
    PrintWriter out = resp.getWriter();&lt;br /&gt;
    Cookie cookies[] = req.getCookies();&lt;br /&gt;
    out.println(&amp;quot;&amp;lt;html&amp;gt;&amp;quot;);&lt;br /&gt;
    out.println(&amp;quot;&amp;lt;head&amp;gt;&amp;quot;);&lt;br /&gt;
    out.println(&amp;quot;&amp;lt;title&amp;gt;Servlet Cookie Information&amp;lt;/title&amp;gt;&amp;quot;);&lt;br /&gt;
    out.println(&amp;quot;&amp;lt;/head&amp;gt;&amp;quot;);&lt;br /&gt;
    out.println(&amp;quot;&amp;lt;body&amp;gt;&amp;quot;);&lt;br /&gt;
    if ((cookies == null) || (cookies.length == 0)) {&lt;br /&gt;
      out.println(&amp;quot;&amp;lt;center&amp;gt;&amp;lt;h1&amp;gt;No Cookies found&amp;lt;/h1&amp;gt;&amp;quot;);&lt;br /&gt;
    } else {&lt;br /&gt;
      out.println(&amp;quot;&amp;lt;center&amp;gt;&amp;lt;h1&amp;gt;Cookies found&amp;lt;/h1&amp;gt;&amp;quot;);&lt;br /&gt;
      for (int i = 0; i &amp;lt; cookies.length; i++) {&lt;br /&gt;
        Cookie c = cookies[i];&lt;br /&gt;
        out.println(c.getName() + &amp;quot;::&amp;quot; + c.getValue() + &amp;quot;::&amp;quot;&lt;br /&gt;
            + c.getComment() + &amp;quot;::&amp;quot; + c.getMaxAge() + &amp;quot;&amp;lt;BR/&amp;gt;&amp;quot;);&lt;br /&gt;
      }&lt;br /&gt;
      out.println(&amp;quot;&amp;lt;/table&amp;gt;&amp;lt;/center&amp;gt;&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    out.println(&amp;quot;&amp;lt;/body&amp;gt;&amp;quot;);&lt;br /&gt;
    out.println(&amp;quot;&amp;lt;/html&amp;gt;&amp;quot;);&lt;br /&gt;
    out.flush();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Parse a Cookie: header into individual tokens according to RFC 2109. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.&lt;br /&gt;
 * &lt;br /&gt;
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.&lt;br /&gt;
 * &lt;br /&gt;
 * The contents of this file are subject to the terms of either the GNU&lt;br /&gt;
 * General Public License Version 2 only (&amp;quot;GPL&amp;quot;) or the Common Development&lt;br /&gt;
 * and Distribution License(&amp;quot;CDDL&amp;quot;) (collectively, the &amp;quot;License&amp;quot;).  You&lt;br /&gt;
 * may not use this file except in compliance with the License. You can obtain&lt;br /&gt;
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html&lt;br /&gt;
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific&lt;br /&gt;
 * language governing permissions and limitations under the License.&lt;br /&gt;
 * &lt;br /&gt;
 * When distributing the software, include this License Header Notice in each&lt;br /&gt;
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.&lt;br /&gt;
 * Sun designates this particular file as subject to the &amp;quot;Classpath&amp;quot; exception&lt;br /&gt;
 * as provided by Sun in the GPL Version 2 section of the License file that&lt;br /&gt;
 * accompanied this code.  If applicable, add the following below the License&lt;br /&gt;
 * Header, with the fields enclosed by brackets [] replaced by your own&lt;br /&gt;
 * identifying information: &amp;quot;Portions Copyrighted [year]&lt;br /&gt;
 * [name of copyright owner]&amp;quot;&lt;br /&gt;
 * &lt;br /&gt;
 * Contributor(s):&lt;br /&gt;
 * &lt;br /&gt;
 * If you wish your version of this file to be governed by only the CDDL or&lt;br /&gt;
 * only the GPL Version 2, indicate your decision by adding &amp;quot;[Contributor]&lt;br /&gt;
 * elects to include this software in this distribution under the [CDDL or GPL&lt;br /&gt;
 * Version 2] license.&amp;quot;  If you don&amp;quot;t indicate a single choice of license, a&lt;br /&gt;
 * recipient has the option to distribute your version of this file under&lt;br /&gt;
 * either the CDDL, the GPL Version 2 or to extend the choice of license to&lt;br /&gt;
 * its licensees as provided above.  However, if you add GPL Version 2 code&lt;br /&gt;
 * and therefore, elected the GPL Version 2 license, then the option applies&lt;br /&gt;
 * only if the new code is made subject to such option by the copyright&lt;br /&gt;
 * holder.&lt;br /&gt;
 */&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved.&lt;br /&gt;
 * Use is subject to license terms.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
import java.lang.System;&lt;br /&gt;
import java.lang.String;&lt;br /&gt;
import java.lang.IndexOutOfBoundsException;&lt;br /&gt;
import java.lang.ArrayIndexOutOfBoundsException;&lt;br /&gt;
/**&lt;br /&gt;
 * Parse a Cookie: header into individual tokens according to RFC 2109.&lt;br /&gt;
 */&lt;br /&gt;
public class CookieTokenizer&lt;br /&gt;
{&lt;br /&gt;
    /**&lt;br /&gt;
     * Upper bound on the number of cookie tokens to accept.  The limit is&lt;br /&gt;
     * based on the 4 different attributes (4.3.4) and the 20 cookie minimum&lt;br /&gt;
     * (6.3) given in RFC 2109 multiplied by 2 to accomodate the 2 tokens in&lt;br /&gt;
     * each name=value pair (&amp;quot;JSESSIONID=1234&amp;quot; is 2 tokens).&lt;br /&gt;
     */&lt;br /&gt;
    private static final int MAX_COOKIE_TOKENS = 4 * 20 * 2;&lt;br /&gt;
    /**&lt;br /&gt;
     * Array of cookie tokens.  Even indices contain name tokens while odd&lt;br /&gt;
     * indices contain value tokens (or null).&lt;br /&gt;
     */&lt;br /&gt;
    private String tokens[] = new String[MAX_COOKIE_TOKENS];&lt;br /&gt;
    /**&lt;br /&gt;
     * Number of cookie tokens currently in the tokens[] array.&lt;br /&gt;
     */&lt;br /&gt;
    private int numTokens = 0;&lt;br /&gt;
    /**&lt;br /&gt;
     * Parse a name=value pair from the Cookie: header.&lt;br /&gt;
     *&lt;br /&gt;
     * @param cookies The Cookie: header to parse&lt;br /&gt;
     * @param beginIndex The index in cookies to begin parsing from, inclusive&lt;br /&gt;
     */&lt;br /&gt;
    private int parseNameValue(String cookies, int beginIndex) {&lt;br /&gt;
        int length = cookies.length();&lt;br /&gt;
        int index = beginIndex;&lt;br /&gt;
        while (index &amp;lt; length) {&lt;br /&gt;
            switch (cookies.charAt(index)) {&lt;br /&gt;
            case &amp;quot;;&amp;quot;:&lt;br /&gt;
            case &amp;quot;,&amp;quot;:&lt;br /&gt;
                // Found end of name token without value&lt;br /&gt;
                tokens[numTokens] = cookies.substring(beginIndex, index).trim();&lt;br /&gt;
                if (tokens[numTokens].length() &amp;gt; 0) {&lt;br /&gt;
                    numTokens++;&lt;br /&gt;
                    tokens[numTokens] = null;&lt;br /&gt;
                    numTokens++;&lt;br /&gt;
                }&lt;br /&gt;
                return index + 1;&lt;br /&gt;
            case &amp;quot;=&amp;quot;:&lt;br /&gt;
                // Found end of name token with value&lt;br /&gt;
                tokens[numTokens] = cookies.substring(beginIndex, index).trim();&lt;br /&gt;
                numTokens++;&lt;br /&gt;
                return parseValue(cookies, index + 1);&lt;br /&gt;
            case &amp;quot;&amp;quot;&amp;quot;:&lt;br /&gt;
                // Skip past quoted span&lt;br /&gt;
                do index++; while (cookies.charAt(index) != &amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            index++;&lt;br /&gt;
        }&lt;br /&gt;
        if (index &amp;gt; beginIndex) {&lt;br /&gt;
            // Found end of name token without value&lt;br /&gt;
            tokens[numTokens] = cookies.substring(beginIndex, index).trim();&lt;br /&gt;
            if (tokens[numTokens].length() &amp;gt; 0) {&lt;br /&gt;
                numTokens++;&lt;br /&gt;
                tokens[numTokens] = null;&lt;br /&gt;
                numTokens++;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        return index;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Parse the name=value tokens from a Cookie: header.&lt;br /&gt;
     *&lt;br /&gt;
     * @param cookies The Cookie: header to parse&lt;br /&gt;
     */&lt;br /&gt;
    public int tokenize(String cookies) {&lt;br /&gt;
        numTokens = 0;&lt;br /&gt;
        if (cookies != null) {&lt;br /&gt;
            try {&lt;br /&gt;
                // Advance through cookies, parsing name=value pairs&lt;br /&gt;
                int length = cookies.length();&lt;br /&gt;
                int index = 0;&lt;br /&gt;
                while (index &amp;lt; length)&lt;br /&gt;
                    index = parseNameValue(cookies, index);&lt;br /&gt;
            }&lt;br /&gt;
            catch (ArrayIndexOutOfBoundsException e) {&lt;br /&gt;
                // Filled up the tokens[] array&lt;br /&gt;
            }&lt;br /&gt;
            catch (IndexOutOfBoundsException e) {&lt;br /&gt;
                // Walked off the end of the cookies header&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        return numTokens;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Return the number of cookie tokens parsed from the Cookie: header.&lt;br /&gt;
     */&lt;br /&gt;
    public int getNumTokens() {&lt;br /&gt;
        return numTokens;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Returns a given cookie token from the Cookie: header.&lt;br /&gt;
     *&lt;br /&gt;
     * @param index The index of the cookie token to return&lt;br /&gt;
     */&lt;br /&gt;
    public String tokenAt(int index) {&lt;br /&gt;
        return tokens[index];&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Parse the value token from a name=value pair.&lt;br /&gt;
     *&lt;br /&gt;
     * @param cookies The Cookie: header to parse&lt;br /&gt;
     * @param beginIndex The index in cookies to begin parsing from, inclusive&lt;br /&gt;
     */&lt;br /&gt;
    private int parseValue(String cookies, int beginIndex) {&lt;br /&gt;
        int length = cookies.length();&lt;br /&gt;
        int index = beginIndex;&lt;br /&gt;
        while (index &amp;lt; length) {&lt;br /&gt;
            switch (cookies.charAt(index)) {&lt;br /&gt;
            case &amp;quot;;&amp;quot;:&lt;br /&gt;
            case &amp;quot;,&amp;quot;:&lt;br /&gt;
                // Found end of value token&lt;br /&gt;
                tokens[numTokens] = cookies.substring(beginIndex, index).trim();&lt;br /&gt;
                numTokens++;&lt;br /&gt;
                return index + 1;&lt;br /&gt;
            case &amp;quot;&amp;quot;&amp;quot;:&lt;br /&gt;
                // Skip past quoted span&lt;br /&gt;
                do index++; while (cookies.charAt(index) != &amp;quot;&amp;quot;&amp;quot;);&lt;br /&gt;
                break;&lt;br /&gt;
            }&lt;br /&gt;
            index++;&lt;br /&gt;
        }&lt;br /&gt;
        // Found end of value token&lt;br /&gt;
        tokens[numTokens] = cookies.substring(beginIndex, index).trim();&lt;br /&gt;
        numTokens++;&lt;br /&gt;
        return index;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Servlet Cookie Reader ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import javax.servlet.*;&lt;br /&gt;
import javax.servlet.http.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
public class MyServlet extends HttpServlet {&lt;br /&gt;
   &lt;br /&gt;
    public void doGet(HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;
    throws ServletException, java.io.IOException {&lt;br /&gt;
    &lt;br /&gt;
    Cookie cookie = null;&lt;br /&gt;
    //Get an array of Cookies associated with this domain&lt;br /&gt;
    Cookie[] cookies = request.getCookies();&lt;br /&gt;
    boolean hasCookies = false;&lt;br /&gt;
    &lt;br /&gt;
  if (cookies != null)&lt;br /&gt;
        hasCookies = true;&lt;br /&gt;
   &lt;br /&gt;
        // display the name/value of each cookie&lt;br /&gt;
        response.setContentType(&amp;quot;text/html&amp;quot;);&lt;br /&gt;
        java.io.PrintWriter out = response.getWriter();&lt;br /&gt;
    &lt;br /&gt;
        out.println(&amp;quot;&amp;lt;html&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;head&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;title&amp;gt;Cookie information&amp;lt;/title&amp;gt;&amp;quot;);  &lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/head&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;body&amp;gt;&amp;quot;);&lt;br /&gt;
        if (hasCookies){&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;h2&amp;gt; The name and value of each found cookie&amp;lt;/h2&amp;gt;&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
         for (int i = 0; i &amp;lt; cookies.length; i++){&lt;br /&gt;
            cookie = cookies[i];&lt;br /&gt;
            out.println(&amp;quot;Name of cookie #&amp;quot;+(i + 1)+&amp;quot;: &amp;quot;+cookie.getName()+&amp;quot;&amp;lt;br&amp;gt;&amp;quot;);&lt;br /&gt;
            out.println(&amp;quot;Value of cookie #&amp;quot;+(i + 1)+&amp;quot;: &amp;quot;+cookie.getValue()+&amp;quot;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;quot;);&lt;br /&gt;
      &lt;br /&gt;
       }&lt;br /&gt;
        &lt;br /&gt;
        } else {&lt;br /&gt;
            out.println(&amp;quot;&amp;lt;h2&amp;gt; This request did not include any cookies&amp;lt;/h2&amp;gt;&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
      &lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/body&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/html&amp;gt;&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        out.close();&lt;br /&gt;
    } &lt;br /&gt;
    &lt;br /&gt;
    public void doPost(HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;
    throws ServletException, java.io.IOException {&lt;br /&gt;
        &lt;br /&gt;
        doGet(request,response);&lt;br /&gt;
    } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Servlet Cookie Setter ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
import javax.servlet.*;&lt;br /&gt;
import javax.servlet.http.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
public class MyServlet extends HttpServlet {&lt;br /&gt;
   &lt;br /&gt;
    public void doGet(HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;
    throws ServletException, java.io.IOException {&lt;br /&gt;
    &lt;br /&gt;
    Cookie cookie = null;&lt;br /&gt;
    //Get an array of Cookies associated with this domain&lt;br /&gt;
    Cookie[] cookies = request.getCookies();&lt;br /&gt;
    boolean newCookie = false;&lt;br /&gt;
    &lt;br /&gt;
    //Get the &amp;quot;mycookie&amp;quot; Cookie if it exists&lt;br /&gt;
    if (cookies != null){&lt;br /&gt;
        for (int i = 0; i &amp;lt; cookies.length; i++){&lt;br /&gt;
            if (cookies[i].getName().equals(&amp;quot;mycookie&amp;quot;)){&lt;br /&gt;
              cookie = cookies[i];&lt;br /&gt;
             } &lt;br /&gt;
        }//end for&lt;br /&gt;
    }//end if&lt;br /&gt;
       &lt;br /&gt;
    if (cookie == null){&lt;br /&gt;
       newCookie=true;&lt;br /&gt;
      //Get the cookie&amp;quot;s Max-Age from a context-param element&lt;br /&gt;
      //If the &amp;quot;cookie-age&amp;quot; param is not set properly&lt;br /&gt;
      //then set the cookie to a default of -1, &amp;quot;never expires&amp;quot;&lt;br /&gt;
      int maxAge;&lt;br /&gt;
      try{&lt;br /&gt;
           maxAge = new Integer(getServletContext().getInitParameter(&amp;quot;cookie-age&amp;quot;)).intValue();&lt;br /&gt;
           } catch (Exception e) {&lt;br /&gt;
            maxAge = -1;&lt;br /&gt;
           }&lt;br /&gt;
      &lt;br /&gt;
       //Create the Cookie object&lt;br /&gt;
     &lt;br /&gt;
        cookie = new Cookie(&amp;quot;mycookie&amp;quot;,&amp;quot;&amp;quot;+getNextCookieValue());&lt;br /&gt;
        cookie.setPath(request.getContextPath());&lt;br /&gt;
        cookie.setMaxAge(maxAge);&lt;br /&gt;
        response.addCookie(cookie);&lt;br /&gt;
        &lt;br /&gt;
        }//end if&lt;br /&gt;
        // get some info about the cookie&lt;br /&gt;
        response.setContentType(&amp;quot;text/html&amp;quot;);&lt;br /&gt;
        java.io.PrintWriter out = response.getWriter();&lt;br /&gt;
    &lt;br /&gt;
        out.println(&amp;quot;&amp;lt;html&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;head&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;title&amp;gt;Cookie info&amp;lt;/title&amp;gt;&amp;quot;);  &lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/head&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;body&amp;gt;&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
        out.println(&amp;quot;&amp;lt;h2&amp;gt; Information about the cookie named \&amp;quot;mycookie\&amp;quot;&amp;lt;/h2&amp;gt;&amp;quot;);&lt;br /&gt;
        &lt;br /&gt;
        out.println(&amp;quot;Cookie value: &amp;quot;+cookie.getValue()+&amp;quot;&amp;lt;br&amp;gt;&amp;quot;);&lt;br /&gt;
        if (newCookie){&lt;br /&gt;
        out.println(&amp;quot;Cookie Max-Age: &amp;quot;+cookie.getMaxAge()+&amp;quot;&amp;lt;br&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;Cookie Path: &amp;quot;+cookie.getPath()+&amp;quot;&amp;lt;br&amp;gt;&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/body&amp;gt;&amp;quot;);&lt;br /&gt;
        out.println(&amp;quot;&amp;lt;/html&amp;gt;&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
        out.close();&lt;br /&gt;
    } &lt;br /&gt;
    private long getNextCookieValue(){&lt;br /&gt;
    &lt;br /&gt;
    /*This produces a cookie value to show how to create Cookie objects. If &lt;br /&gt;
      this method was heavily used in a production environment it may&lt;br /&gt;
      produce too many objects; synchronization of a single Date object&lt;br /&gt;
      might be better, based on performance testing. At any rate a&lt;br /&gt;
      production environment would produce a unique cookie value in a&lt;br /&gt;
      different manner such as from a unique database ID. */&lt;br /&gt;
    //returns the number of milleseconds since Jan 1, 1970&lt;br /&gt;
    return new java.util.Date().getTime();&lt;br /&gt;
    &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void doPost(HttpServletRequest request, HttpServletResponse response)&lt;br /&gt;
    throws ServletException, java.io.IOException {&lt;br /&gt;
        &lt;br /&gt;
        doGet(request,response);&lt;br /&gt;
    } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>