setstatustag.java

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

JAVA
90
字号
/*
 * 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 java.io.*;
import java.lang.*;
import java.lang.reflect.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

/**
 * JSP Tag <b>setStatus</b>, used to set the status code for the HTTP Response.
 * <p>
 * JSP Tag Lib Descriptor
 * <p><pre>
 * &lt;name&gt;setStatus&lt;/name&gt;
 * &lt;tagclass&gt;org.apache.taglibs.response.SetStatusTag&lt;/tagclass&gt;
 * &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
 * &lt;info&gt;Set the status code for the HTTP Response.&lt;/info&gt;
 *   &lt;attribute&gt;
 *     &lt;name&gt;status&lt;/name&gt;
 *     &lt;required&gt;true&lt;/required&gt;
 *     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
 *   &lt;/attribute&gt;
 * </pre>
 *
 * @author Glenn Nielsen
 */

public class SetStatusTag extends TagSupport
{
    private String status;

    /**
     * Method called at end of tag which sets the HTTP Response status code.
     *
     * @return SKIP_BODY
     */
    public final int doEndTag() throws JspException
    {
        int status_code = 0;
        HttpServletResponse resp = null;
        Field ec = null;
 
        try {
            resp = (HttpServletResponse)pageContext.getResponse(); 
            Class rc = resp.getClass();
            ec = rc.getField(status);
            status_code = ec.getInt(resp);
        } catch(Exception e) {
            throw new JspException(
                "Response setStatus tag could not find status code: " + status);
        }

	((HttpServletResponse)pageContext.getResponse()).setStatus(status_code);
        return EVAL_PAGE;
    }

    /**
     * Required attribute status, set the status code to return.
     *
     * The status must be a text string for a status code as
     * defined in java class HttpServletResponse. For example,
     * <b>SC_OK</b> to return HTTP status code 200.
     *
     * @param String Status Code name, e.g. "SC_OK"
     */
    public final void setStatus(String stat)
    {
        status = stat;
    }
}

⌨️ 快捷键说明

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