001 /* 002 * Copyright 2004 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package examples; 017 018 import javax.servlet.jsp.*; 019 import javax.servlet.jsp.tagext.*; 020 import java.util.Hashtable; 021 import java.io.Writer; 022 import java.io.IOException; 023 024 /** 025 * Example1: the simplest tag 026 * Collect attributes and call into some actions 027 * 028 * <foo att1="..." att2="...." att3="...." /> 029 */ 030 031 public class FooTag 032 extends ExampleTagBase 033 { 034 private String atts[] = new String[3]; 035 int i = 0; 036 037 private final void setAtt(int index, String value) { 038 atts[index] = value; 039 } 040 041 public void setAtt1(String value) { 042 setAtt(0, value); 043 } 044 045 public void setAtt2(String value) { 046 setAtt(1, value); 047 } 048 049 public void setAtt3(String value) { 050 setAtt(2, value); 051 } 052 053 /** 054 * Process start tag 055 * 056 * @return EVAL_BODY_INCLUDE 057 */ 058 public int doStartTag() throws JspException { 059 i = 0; 060 return EVAL_BODY_TAG; 061 } 062 063 public void doInitBody() throws JspException { 064 pageContext.setAttribute("member", atts[i]); 065 i++; 066 } 067 068 public int doAfterBody() throws JspException { 069 try { 070 if (i == 3) { 071 bodyOut.writeOut(bodyOut.getEnclosingWriter()); 072 return SKIP_BODY; 073 } else 074 pageContext.setAttribute("member", atts[i]); 075 i++; 076 return EVAL_BODY_TAG; 077 } catch (IOException ex) { 078 throw new JspTagException(ex.toString()); 079 } 080 } 081 } 082