TagSupport类的生命周期具体执行过程
(1)当jsp容器中在解释jsp页面时,如果遇到自定义标签的开始标记,将利用"标签处理类"建立一个"标签处理对象".在建立"标签处理对象"的过程中,jsp容器会回调setPageContext()方法,然后根据自定义标签的属性值来初始化"标签处理对象"的属性.
(2)接着jsp容器会运行doStartTag()方法内的程序代码,然后根据此方法的返回值决定后续动作,如果返回SKIP_BODY常量,表示要求jsp容器忽略此标签主体内容;如果返回EVAL_BODY_INCLUDE常量,表示要求jsp容器执行标签主体的内容,并将结果包括在相应中,然后再运行doAfterBody()方法.
(3)如果doAfterBody()方法传回EVAL_BODY_INCLUDE常量,表示要求jsp容器再次执行标签主体的内容;如果返回SKIP_BODY常量,jsp容器建会运行doEndTag()方法.
(4)最后,jsp容器会运行doEndTag()方法内的程序代码,并根据此方法的返回值决定后续动作----如果返回SKIP_BODY常量,jsp容器会忽略自定义标签以后的jsp内容;如果返回EVAL_BODY_INCLUDE常量,jsp容器会运行自定义标签以后的jsp内容.
************************************************************************************************************************************************************
pageContext
page内置对象介绍
page对象有点类似于Java编程中的this指针,就是指当前JSP页面本身。page是java.lang.Object类的对象。page对象在实际开发过程中并不经常使用。
page对象常用方法
getClass():返回当时Object的类。
hashCode():返回此时Object的哈希代码。
toString():将此时的Object类转换成字符串。
equals(Object ob):比较此对象是否与指定的对象相等。
copy(Object ob):将此对象复制到指定的对象中。
clone():对此对象进行克隆。
二、
out内置对象介绍
out对象用来在页面输出数据,是在JSP开发过程中使用得最频繁的对象,然而使用起来也是为简便的。
out对象常用方法
print():在页面中打印出字符串信息,不换行。
print():在页面中打印出字符串信息,并且换行。
clear():清除缓冲区中尚存的内容。
clearBuffer():清除当前缓冲区中尚存的内容。
flush():清除数据流。
getBufferSize():返回缓冲区的内存大小,单位大小为字节流。如果不进行缓冲区的设置,大小为0。
getRemaining():返回缓冲区还剩下多少字节数可以使用。
isAutoFlush():检查当前缓冲区是设置为自动清空,还是满了就抛也异常。
close():关闭输出流。
三、
exception内置对象介绍
exception内置对象用来处理页面出现的异常错误,它是java.lang.Throwable类的一个对象。在JSP开发过程中,通常是在某个页面(比如A.jsp)中加入page指令的errorPage属性来将其指向一个专门处理异常错误的页面(doError.jsp)。如果这个错误处理页面doError.jsp已经封装了从A.jsp页面收到的错误信息,并且错误处理页面doError.jsp含有的isErrorpage属性设置为true,则这个错误处理页面可以调用exception内置对象来处理这个些错误信息。
exception对象常用方法
getMessage()和getLocalizedMessage():这两种方法分别返回exception对象的异常消息字符串和本地化语言的异常错误。
printStackTrace():显示异常的栈跟踪轨迹。
toString():返回关于异常错误的简单消息描述。
fillInStackTrace():重写异常错误的栈执行轨迹。
四、
config内置对象介绍
config内置对象是ServletConfig类的一个实例,用于JSP引擎在Servlet初始化时,通过config向它(Servlet)传递信息。这种信息可以是属性名/值匹配的参数,也可以是通过ServletContext对象传递的服务器的有关信息。一般在JSP开发中行少用到config内置对象,只有在编写Servlet时若需要重载Servlet的init()方法时才会用到。
config对象常用方法
getServletContext():返回一个含有服务器相关信息的ServletContext对象。
getIntParameter(String name):返回初始化参数的值。
getIntParameterNames():返回包含了Servlet初始化所需要的所有参数,返回类型是枚举型。
五、
pageContext内置对象介绍
pageContext内置对象是一个比较特殊的对象,它相当于页面中所有其他对象功能的最大集成者,即使用它可以访问到本页面中所有其他的对象,例如前面已经描述的request、response、out和page对象等。由于在JSP中request和response等对象本来就可以通过直接调用方法使用,所以pageContext对象在实际JSP开发中很少使用到。
pageContext对象常用方法
getRequest():返回当前页面中的request对象。
getResponse():返回当前页面中的response对象。
getSession():返回当前页面中的session对象。
getServletContext():返回当前页面中的application对象。
getPage():返回当前页面中的page对象。
getOut():返回当前页面中的out对象。
getException():返回当前页面中的exception对象。
getServletConfig():返回当前页面中的config对象。
www.2cto.com
setAttribute(String name):给指定的属性名设置属性值。
getAttribute(String naem):根据属性名找到相应的属性值。
setAttribute(String name,Object obj, int scope):在给定的范围内设置相应的属性值。
getAttribute(String name, intscope):在给的范围内获取相应的属性值。
findAttribute(String name):寻找一个属性并返回,如果查找不到则返回null。
removeAttribute(String name):通过属性名删除掉某个属性。
removeAttribute(String name,int scope):在指定的某个范围里删除某个属性。
getAttributeScope(String namescope):返回某属性的作用域。
getAttributeNamesInScope(intscope):返回指定范围内的所有属性名的枚举。
release():释放pageContext占据的所有资料。
forward(StringrelativeURLpath):使用当前页面重导到另一个页面。
include(StringrelativeURLpath):使用当前位置包含的另一个页面。
============================== As e Example =======================================
/*
* 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.input;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
*
* This class implements the <input:form> tag, which presents an <form
* ... /> element.
*
* @version 0.90
* @author Shawn Bayern
* @author Lance Lavandowska
* @author Karl von Randow
*/
public class Form extends TagSupport {
private String name; // name of the form
private String action; // form action
private Map attributes; // attributes of the <input> element
private String attributesText; // attributes of the <input> element as text
private String beanId; // bean id to get default values from
private String method; // form method
private String encType; // form encType
public void release() {
super.release();
name = null;
action = null;
attributes = null;
attributesText = null;
beanId = null;
method = null;
encType = null;
}
public int doStartTag() throws JspException {
try {
// get what we need from the page
JspWriter out = pageContext.getOut();
// start building up the tag
out.print("<form method=\"");
out.print(method != null ? method : "get");
out.print("\" action=\"");
if (action != null) {
if (action.length() > 0
&& pageContext.getResponse() instanceof HttpServletResponse) {
out.print(((HttpServletResponse) pageContext.getResponse())
.encodeURL(action));
} else {
out.print(action);
}
}
out.print("\" ");
if (name != null) {
out.print("name=\"" + Util.quote(name) + "\" ");
}
if (encType != null) {
out.print("enctype=\"" + encType + "\" ");
}
// include any attributes we've got here
Util.printAttributes(out, attributes);
if (attributesText != null) {
out.print(attributesText + " ");
}
// end the tag
out.print(">");
} catch (Exception ex) {
throw new JspTagException(ex.getMessage());
}
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
out.print("</form>");
} catch (Exception ex) {
throw new JspTagException(ex.getMessage());
}
return EVAL_PAGE;
}
public void setName(String x) {
name = x;
}
public void setAttributes(Map x) {
attributes = x;
}
public void setAttributesText(String x) {
attributesText = x;
}
public void setBean(String x) {
beanId = x;
}
public String getBean() {
return beanId;
}
public void setAction(String x) {
action = x;
}
public void setMethod(String x) {
method = x;
}
public void setEncType(String x) {
encType = x;
}
/**
* Getter for property name.
*
* @return Value of property name.
*/
public String getName() {
return name;
}
/**
* Getter for property method.
*
* @return Value of property method.
*/
public String getMethod() {
return method;
}
/**
* Getter for property encType.
*
* @return Value of property encType.
*/
public String getEncType() {
return encType;
}
/**
* Getter for property attributesText.
*
* @return Value of property attributesText.
*/
public String getAttributesText() {
return attributesText;
}
/**
* Getter for property attributes.
*
* @return Value of property attributes.
*/
public Map getAttributes() {
return attributes;
}
/**
* Getter for property action.
*
* @return Value of property action.
*/
public String getAction() {
return action;
}
}