/** * Initialization of numero property. */privateintnumero;privateStringnumero2;/** Creates new instance of tag handler */publicDuplicar(){super();}/////////////////////////////////////////////////////////////////// ////// User methods. ////// ////// Modify these methods to customize your tag handler. ////// ////////////////////////////////////////////////////////////////////** * Method called from doStartTag(). * Fill in this method to perform other operations from doStartTag(). * */privatevoidotherDoStartTagOperations(){JspWriterout=pageContext.getOut();try{numero=Integer.parseInt(numero2);out.println(numero*2);}catch(IOExceptionex){ex.printStackTrace();}//// TODO: code that performs other operations in doStartTag// should be placed here.// It will be called after initializing variables,// finding the parent, setting IDREFs, etc, and// before calling theBodyShouldBeEvaluated().//// For example, to print something out to the JSP, use the following://// try {// JspWriter out = pageContext.getOut();// out.println("something");// } catch (IOException ex) {// // do something// }////}/** * Method called from doEndTag() * Fill in this method to perform other operations from doEndTag(). * */privatevoidotherDoEndTagOperations(){//// TODO: code that performs other operations in doEndTag// should be placed here.// It will be called after initializing variables,// finding the parent, setting IDREFs, etc, and// before calling shouldEvaluateRestOfPageAfterEndTag().//}/** * Fill in this method to process the body content of the tag. * You only need to do this if the tag's BodyContent property * is set to "JSP" or "tagdependent." * If the tag's bodyContent is set to "empty," then this method * will not be called. */privatevoidwriteTagBodyContent(JspWriterout,BodyContentbodyContent)throwsIOException{//// TODO: insert code to write html before writing the body content.// e.g.://// out.println("<strong>" + attribute_1 + "</strong>");// out.println(" <blockquote>");//// write the body content (after processing by the JSP engine) on the output Writer//bodyContent.writeOut(out);//// Or else get the body content as a string and process it, e.g.:// String bodyStr = bodyContent.getString();// String result = yourProcessingMethod(bodyStr);// out.println(result);//// TODO: insert code to write html after writing the body content.// e.g.://// out.println(" </blockquote>");// clear the body content for the next time through.bodyContent.clearBody();}/////////////////////////////////////////////////////////////////// ////// Tag Handler interface methods. ////// ////// Do not modify these methods; instead, modify the ////// methods that they call. ////// ////////////////////////////////////////////////////////////////////** * This method is called when the JSP engine encounters the start tag, * after the attributes are processed. * Scripting variables (if any) have their values set here. * @return EVAL_BODY_BUFFERED if the JSP engine should evaluate the tag body, otherwise return SKIP_BODY. * This method is automatically generated. Do not modify this method. * Instead, modify the methods that this method calls. */publicintdoStartTag()throwsJspException,JspException{otherDoStartTagOperations();if(theBodyShouldBeEvaluated()){returnEVAL_BODY_BUFFERED;}else{returnSKIP_BODY;}}/** * This method is called after the JSP engine finished processing the tag. * @return EVAL_PAGE if the JSP engine should continue evaluating the JSP page, otherwise return SKIP_PAGE. * This method is automatically generated. Do not modify this method. * Instead, modify the methods that this method calls. */publicintdoEndTag()throwsJspException,JspException{otherDoEndTagOperations();if(shouldEvaluateRestOfPageAfterEndTag()){returnEVAL_PAGE;}else{returnSKIP_PAGE;}}/** * This method is called after the JSP engine processes the body content of the tag. * @return EVAL_BODY_AGAIN if the JSP engine should evaluate the tag body again, otherwise return SKIP_BODY. * This method is automatically generated. Do not modify this method. * Instead, modify the methods that this method calls. */publicintdoAfterBody()throwsJspException{try{//// This code is generated for tags whose bodyContent is "JSP"//BodyContentbodyContent=getBodyContent();JspWriterout=bodyContent.getEnclosingWriter();writeTagBodyContent(out,bodyContent);}catch(Exceptionex){handleBodyContentException(ex);}if(theBodyShouldBeEvaluatedAgain()){returnEVAL_BODY_AGAIN;}else{returnSKIP_BODY;}}/** * Handles exception from processing the body content. */privatevoidhandleBodyContentException(Exceptionex)throwsJspException{// Since the doAfterBody method is guarded, place exception handing code here.thrownewJspException("error in NewTag: "+ex);}/** * Fill in this method to determine if the rest of the JSP page * should be generated after this tag is finished. * Called from doEndTag(). */privatebooleanshouldEvaluateRestOfPageAfterEndTag(){//// TODO: code that determines whether the rest of the page// should be evaluated after the tag is processed// should be placed here.// Called from the doEndTag() method.//returntrue;}/** * Fill in this method to determine if the tag body should be evaluated * again after evaluating the body. * Use this method to create an iterating tag. * Called from doAfterBody(). */privatebooleantheBodyShouldBeEvaluatedAgain(){//// TODO: code that determines whether the tag body should be// evaluated again after processing the tag// should be placed here.// You can use this method to create iterating tags.// Called from the doAfterBody() method.//returnfalse;}/** * Fill in this method to determine if the tag body should be evaluated. * Called from doStartTag(). */privatebooleantheBodyShouldBeEvaluated(){//// TODO: code that determines whether the body should be// evaluated should be placed here.// Called from the doStartTag() method.//returntrue;}/** * Setter for the numero attribute. */publicvoidsetNumero(intvalue){this.numero=value;}publicvoidsetNumero2(Stringnumero2){this.numero2=numero2;}
}
A
ArcanjoZer0PJ
index jsp :
<%@taglib uri="/WEB-INF/tlds/duplicar" prefix=“minhatag” %>
<%@page contentType=“text/html”%>
<%@page pageEncoding=“UTF-8”%>
<%–
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library… action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
–%>
<%–
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
–%>
<%–
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library… action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
–%>
<%–
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
–%>
<?xml version=“1.0” encoding=“UTF-8”?><taglibversion=“2.0”xmlns=“<ahref="http://java.sun.com/xml/ns/j2ee">http://java.sun.com/xml/ns/j2ee</a>”xmlns:xsi=“<ahref="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>”xsi:schemaLocation=“<ahref="http://java.sun.com/xml/ns/j2ee">http://java.sun.com/xml/ns/j2ee</a>web-jsptaglibrary_2_0.xsd”>
<tlib-version>1.0</tlib-version><short-name>duplicar</short-name><uri>/WEB-INF/tlds/duplicar</uri><!-- A validator verifies that the tags are used correctly at JSPtranslation time. Validator entries look like this:<validator><validator-class>com.mycompany.TagLibValidator</validator-class><init-param><param-name>parameter</param-name><param-value>value</param-value></init-param></validator>
–>
<!-- A tag library can register Servlet Context event listeners incase it needs to react to such events. Listener entries looklike this:<listener><listener-class>com.mycompany.TagLibListener</listener-class></listener>
–>
<tag><name>Duplicar</name><tag-class>br.estacio.Duplicar</tag-class><body-content>scriptless</body-content><attribute><name>numero2</name><required>true</required><rtexprvalue>true</rtexprvalue><type>java.lang.String</type></attribute><attribute><name>numero</name><rtexprvalue>true</rtexprvalue><type>int</type></attribute></tag></taglib>