senderrortag.java
来自「jakarta-taglibs」· Java 代码 · 共 158 行
JAVA
158 行
/*
* 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>sendError</b>, used to send error as the HTTP Response.
* <p>
* The <b>sendError</b> tag can use an optional error message that it
* gets from the body of the tag.
* <p>
* The required attribute <b>error</b> must be set to an HTTP error
* code such as <b>SC_UNAUTHORIZED</b>.
* <p>
* If the optional attribute <b>reset</b> is set to <b>true</b> the
* buffer, headers, and status code will be reset.
* <p>
* JSP Tag Lib Descriptor
* <p><pre>
* <name>sendError</name>
* <tagclass>org.apache.taglibs.response.SendErrorTag</tagclass>
* <bodycontent>JSP</bodycontent>
* <info>Return an HTTP error as the HTTP Response.</info>
* <attribute>
* <name>error</name>
* <required>true</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* <attribute>
* <name>reset</name>
* <required>false</required>
* <rtexprvalue>false</rtexprvalue>
* </attribute>
* </pre>
*
* @author Glenn Nielsen
*/
public class SendErrorTag extends BodyTagSupport
{
private String error;
private boolean reset = false;
/**
* Method called at start of tag, just returns EVAL_BODY_TAG
*
* @return EVAL_BODY_TAG
*/
public final int doStartTag() throws JspException
{
return EVAL_BODY_TAG;
}
/**
* Read the body of the sendError tag to obtain the error message.
*
* @return SKIP_BODY
*/
public final int doAfterBody() throws JspException
{
// Use the body of the tag as header value
int error_code = 0;
BodyContent body = getBodyContent();
String s = body.getString().trim();
// Clear the body since we only used it as input for the header
// value
body.clearBody();
if( reset )
pageContext.getResponse().reset();
try {
HttpServletResponse resp = null;
Field ec = null;
resp = (HttpServletResponse)pageContext.getResponse();
Class rc = resp.getClass();
ec = rc.getField(error);
error_code = ec.getInt(resp);
} catch(Exception e) {
throw new JspException(
"Response sendError tag could not find error code: " + error);
}
try {
if( s.length() > 0 )
((HttpServletResponse)pageContext.getResponse()).sendError(error_code,s);
else
((HttpServletResponse)pageContext.getResponse()).sendError(error_code);
} catch(IOException e) {
throw new JspException(
"Response sendError tag failed: " + e.getMessage());
} catch(IllegalStateException e) {
throw new JspException(
"Response sendError tag failed " + e.getMessage());
}
return SKIP_BODY;
}
/**
* Method called at end of Tag used to send an error code,
* always returns SKIP_PAGE.
*
* @return SKIP_PAGE
*/
public final int doEndTag() throws JspException
{
return SKIP_PAGE;
}
/**
* Required attribute that sets the error code to return.
*
* The error must be a text string for an error code as
* defined in java class HttpServletResponse. For example,
* <b>SC_UNATHORIZED</b> to return HTTP error code 401.
*
* @param String error code, i.e. <b>SC_UNATHORIZED</b>
*/
public final void setError(String err)
{
error = err;
}
/**
* Optional attribute that flags whether the buffer, headers,
* and status code should be reset.
*
* @param boolean <b>true</b> or <b>false</b>
*/
public final void setReset(boolean reset)
{
this.reset = reset;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?