/** * A tag handler for a custom tag. * This tag handler can generate content from a bean with the type * cs4208.beans.TestBean * The tag handle expects that a bean of this type is available on * the request scope. However, the key with which the bean is stored * on the request scope is not hard-coded. * In the JSP that uses this custom tag, the key for the bean should * be set on this tag handler. * Do note that this tag handler is in a package. The tag handler you * define should also be in a package. * Of course, to get this to work, place the compiled class file for this * Java file in classes/cs4208/tags * * @author Nauman Chaudhry */ package cs4208.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import cs4208.beans.TestBean; public class TestBeanTag extends SimpleTagSupport { public void doTag(){ try{ PageContext pContext = (PageContext) getJspContext(); TestBean tBean = (TestBean) pContext.getRequest().getAttribute(m_beanName); JspWriter out = pContext.getOut(); if(tBean != null) out.println(tBean.getTestContent()); else out.println("Bean " + m_beanName + " is null in TestBeanTag?"); }catch(IOException ioe) { try{ PageContext pContext = (PageContext) getJspContext(); pContext.getOut().println("Error with the bean"); } catch(Exception e){} } } // Bean name. The JSP page is expected to set this. public void setBeanName(String beanName) { m_beanName = beanName; } private String m_beanName; }