addcookietag.java

来自「jakarta-taglibs」· Java 代码 · 共 218 行

JAVA
218
字号
/*
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.taglibs.response;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * JSP Tag <b>addCookie</b>, used to add a cookie to the Http Response.
 * <p>
 * The script variable for the <b>name</b> of the cookie is required.
 * <p>
 * Cookie attributes such as the comment, value, etc. can be set
 * statically by using the corresponding addCookie tag attribute.
 * Or dynamically using the corresponding tag in the body of the
 * addCookie tag.
 * <p>
 * JSP Tag Lib Descriptor
 * <p><pre>
 * &lt;name&gt;addCookie&lt;/name&gt;
 * &lt;tagclass&gt;org.apache.taglibs.response.AddCookieTag&lt;/tagclass&gt;
 * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
 * &lt;info&gt;Add a cookie to an Http Response.&lt;/info&gt;
 *   &lt;attribute&gt;
 *     &lt;name&gt;name&lt;/name&gt;
 *     &lt;required&gt;true&lt;/required&gt;
 *     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
 *   &lt;/attribute&gt;
 *   &lt;attribute&gt;
 *     &lt;name&gt;value&lt;/name&gt;
 *     &lt;required&gt;false&lt;/required&gt;
 *     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
 *   &lt;/attribute&gt;
 *   &lt;attribute&gt;
 *     &lt;name&gt;comment&lt;/name&gt;    
 *     &lt;required&gt;false&lt;/required&gt;
 *     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
 *   &lt;/attribute&gt;
 *   &lt;attribute&gt;
 *     &lt;name&gt;domain&lt;/name&gt;    
 *     &lt;required&gt;false&lt;/required&gt;
 *     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
 *   &lt;/attribute&gt;
 *   &lt;attribute&gt;
 *     &lt;name&gt;maxAge&lt;/name&gt;    
 *     &lt;required&gt;false&lt;/required&gt;
 *     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
 *   &lt;/attribute&gt;
 *   &lt;attribute&gt;
 *     &lt;name&gt;path&lt;/name&gt;    
 *     &lt;required&gt;false&lt;/required&gt;
 *     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
 *   &lt;/attribute&gt;
 *   &lt;attribute&gt;
 *     &lt;name&gt;secure&lt;/name&gt;    
 *     &lt;required&gt;false&lt;/required&gt;
 *     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
 *   &lt;/attribute&gt;
 *   &lt;attribute&gt;
 *     &lt;name&gt;version&lt;/name&gt;    
 *     &lt;required&gt;false&lt;/required&gt;
 *     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
 *   &lt;/attribute&gt;
 * </pre>
 *
 * @author Glenn Nielsen
 */

public class AddCookieTag extends TagSupport
{
    private Cookie cookie = null;
    private String name = null;
    private String value = null;
    private String comment = null;
    private String domain = null;
    private int maxAge = -1;
    private String path = null;
    private boolean secure = false;
    private int version = 0;

    /**
     * Method called at start of addCookie tag to create cookie
     *
     * @return EVAL_BODY_INCLUDE
     */
    public final int doStartTag() throws JspException
    {
	cookie = new Cookie(name,"");
	return EVAL_BODY_INCLUDE;
    }

    /**
     * Method called at end of Tag used to set cookie attributes
     * and add the cookie to the Http Response.
     *
     * @return EVAL_PAGE
     */
    public final int doEndTag() throws JspException
    {
	// Set the cookie attribute values
	if( value != null )
	    cookie.setValue(value);
	if( comment != null )
	    cookie.setComment(comment);
	if( domain != null )
	    cookie.setDomain(domain);
	cookie.setMaxAge(maxAge);
	cookie.setSecure(secure);
	cookie.setVersion(version);
	// Add the cookie
	((HttpServletResponse)pageContext.getResponse()).addCookie(cookie);

	return EVAL_PAGE;
    }

    /**
     * Optional attribute, specifies a comment that describes a cookie's purpose.
     *
     * @param String comment
     */
    public final void setComment(String com)
    {
	comment = com;
    }

    /**
     * Optional attribute, specifies the domain within which this
     * cookie should be presented.
     *
     * @param String domain
     */
    public final void setDomain(String dom)
    {
	domain = dom;
    }

    /**
     * Optional attribute, sets the maximum age of the cookie in seconds.
     *
     * @param String number of seconds
     */
    public final void setMaxAge(int max)
    {
        maxAge = max;
    }

    /**
     * Required attribute, name of the cookie.
     *
     * @param String name
     */
    public final void setName(String nam)
    {
	name = nam;
    }

    /**
     * Optional attribute, specifies a path for the cookie to which
     * the client should return the cookie.
     *
     * @param String path
     */
    public final void setPath(String pth)
    {
	path = pth;
    }

    /**
     * Optional attribute, assigns a value to a cookie.
     *
     * @param String value
     */
    public final void setValue(String val)
    {
	value = val;
    }

    /**
     * Optional attribute, indicates to the browser whether the cookie
     * should only be sent using a secure protocol, such as HTTPS or SSL.
     * Set to true or false.  Default value is false.
     *
     * @param String true or false
     */
    public final void setSecure(boolean flag)
    {
        secure = flag;
    }

    /**
     * Optional attribute, sets the version of the cookie protocol
     * this cookie complies with.
     *
     * @param int version number
     */
    public final void setVersion(int version)
    {
        this.version = version;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?