JavaSercer Pages redirect com.ora.jsp.tags.generic.RedirectTag JSP Encodes the url (Blog hosting)

January 20, 2007 on 6:11 pm | In Java | No Comments

JavaSercer Pages redirect com.ora.jsp.tags.generic.RedirectTag JSP Encodes the url attribute and possible param tags in the body and sets redirect headers. page true true … At the top of the TLD file, you find a standard XML declaration and a DOCTYPE declaration, specifying the Document Type Definition (DTD) for this file. A DTD defines the rules for how elements in an XML file must be used, such as the order of the elements, which elements are mandatory and which are optional, if an element can be included multiple times, etc. If you’re not familiar with XML, don’t worry about this. Just accept the fact that you need to copy the first two elements of Example 16.8 faithfully into your own TLD files. Regarding the order of the elements, just follow the same order as in Example 16.8. Whether an element is mandatory or optional is spelled out in the following descriptions of each element. After the two declarations, the first element in the TLD file must be the element. This is the main element for the TLD, enclosing all more specific elements that describe the library. Within the body of the element, you can specify elements that describe the library as such, as well as each individual tag handler. Let’s start with the five elements that describe the library itself. The element is mandatory and is used to specify the tag library version. The version should be specified as a series of numbers separated by dots. In other words, the normal conventions for software version numbers, such as 1.1 or 2.0.3, should be used. The element, specifying the version of the JSP specification that the library depends on, is optional. The default value is 1.1. The element is intended to be used by page authoring tools. It’s a mandatory element that should contain the default prefix for the action elements. In Example 16.8 the value is ora, meaning that an authoring tool by default generates custom action elements using the ora prefix, for instance . This element value can also be used by a tool as the value of the prefix attribute if it generates the taglib directive in the JSP page. The element value must not include whitespace or other special characters, or start with a digit or underscore. The element is also intended to benefit authoring tools. The value can be used as the default value for the uri attribute in a taglib directive. It’s an optional element, following the same character rules as the element. The last element that describes the library as such is the optional element. It can be used to provide a short description of the library, perhaps something a tool might display to help users decide if the library is what they are looking for. Besides the general elements, the TLD must include at least one element. The element contains other elements that describe different aspects of the custom action: , , , , , and . The element is mandatory and contains the unique name for the corresponding custom action element. The element, also mandatory, contains the fully qualified class name for the tag handler class. If the action introduces variables or needs to do additional syntax validation as described in the next section, the optional element is used to specify the fully qualified class name for the TagExtraInfo subclass. page 230
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check professional tomcat hosting services

JavaSercer Pages // Set the first loop value,

January 20, 2007 on 1:20 am | In Java | No Comments

JavaSercer Pages // Set the first loop value, if any if (enum != null && enum.hasMoreElements( )) { Object currValue = enum.nextElement( ); pageContext.setAttribute(loopId, currValue); return EVAL_BODY_TAG; } else { return SKIP_BODY; } } After verifying that there really is an object with the specified name, a test is done to see if a property name is specified. If it is, the getProperty( ) method is called to retrieve the property value from the specified object so it can be used for the iteration. If a property name is not specified, the object itself is used. All the supported data structure types can be turned into an Enumeration. That’s done by calling the getEnumeration( ) method. The getProperty( ) method and the getEnumeration( ) method are not shown here, because this code is just plain Java code that has nothing to do with implementing iteration in a tag handler. You can look at the source code to see how they work. When the Enumeration has been created, the doStartTag( ) method initializes the loopId variable and places it in the JSP page scope. As you learned in the previous section, the code generated for the page uses the information gained from the LoopTagExtraInfo class to declare a Java variable and assign it the value it finds in one of the JSP scopes, right after the doStartTag( ) call. When the body has been evaluated, the doAfterBody( ) method is called: public int doAfterBody( ) throws JspException { if (enum.hasMoreElements( )) { Object currValue = enum.nextElement( ); pageContext.setAttribute(loopId, currValue); return EVAL_BODY_TAG; } else { return SKIP_BODY; } } The Enumeration is tested to see if it contains any more values. If it does, the loopId page scope variable is reassigned to the new value, and EVAL_BODY_TAG is returned to evaluate the body again. When the end of the Enumeration is reached, SKIP_BODY is returned to break the iteration. When the doAfterBody( ) method returns SKIP_BODY, the doEndTag( ) method is called: public int doEndTag( ) throws JspException { // Test if bodyContent is set, since it will be null if the // body was never evaluated (doStartTag returned SKIP_BODY) if (getBodyContent( ) != null) { try { getPreviousOut().print(getBodyContent( ).getString( )); } catch (IOException e) {} } return EVAL_PAGE; } For every iteration, the content of the evaluated body is buffered in the BodyContent instance assigned to the tag handler. In the doEndTag( ), the content is simply moved to the parent’s BodyContent instance or the main JspWriter instance for the page. An alternative to accumulating the content until the doEndTag( ) method is called is to write it to the parent’s output stream already in the doAfterBody( ) method, using the same code as shown here. page 228
Hint: If you are looking for high quality webhost to host and run your jsp application check Vision web hosting jsp services

Php5 web hosting - JavaSercer Pages Current value: Here, the tag iterates

January 19, 2007 on 3:36 pm | In Java | No Comments

JavaSercer Pages

    Current value: <%= x %>    

Here, the tag iterates over the elements of a String array, adding the current value to the response using a JSP expression in the action’s body. The com.ora.jsp.tags.generic.LoopTag class is the tag handler class for the action. It extends BodyTag support and has four properties: public class LoopTag extends BodyTagSupport { private String name; private String loopId; private String className; private String property; … A standard property setter method is provided for each property. This is no different than in previous examples, so it’s not shown here. The name, loopId, and className properties are mandatory. The name is the name of a JSP scope variable of one of the types listed earlier. The current value of the data structure is made available in the element body through a variable with the name specified by loopId, of the type specified by className. Optionally, property can be specified. If it is, it’s used to get the data structure from the specified property of the bean named by name, instead of using the name object itself as the data structure. To make the loopId variable available in the element’s body, a TagExtraInfo subclass is needed, as described in the previous section. The LoopTagExtraInfo class looks like this: public class LoopTagExtraInfo extends TagExtraInfo { public VariableInfo[] getVariableInfo(TagData data) { return new VariableInfo[] { new VariableInfo(data.getAttributeString(”loopId”), data.getAttributeString(”className”), true, VariableInfo.NESTED) }; } } It introduces a variable named by the loopId attribute, with the type specified by the className attribute. The scope is specified as NESTED, meaning the variable is available only within the action element’s body. In addition to the property variables, the tag handler class has an Enumeration instance variable: private Enumeration enum; This variable is initiated by the doStartTag( ) method: public int doStartTag( ) throws JspException { Object obj = pageContext.findAttribute(name); if (obj == null) { throw new JspException(”Variable ” + name + ” not found”); } Object mvObj = obj; try { // Get the multi-value object using the specified property // getter method, if any if (property != null) { mvObj = getProperty(obj, property); } enum = getEnumeration(mvObj); } catch (JspException e) { throw new JspException(”Error getting loop data from ” + name + “: ” + e.getMessage( )); } page 227

Hint: If you are looking for high quality and reliable webspace provider to host and run your jsp hosting application check Sandzak jsp web hosting provider

Oscommerce web hosting - JavaSercer Pages The UsePropertyTagExtraInfo class sets the scope

January 19, 2007 on 10:12 am | In Java | No Comments

JavaSercer Pages The UsePropertyTagExtraInfo class sets the scope to AT_END . As you can see in Example 16.5, this results in the variable declaration and assignment code being added after the doEndTag( ) call. To satisfy the second requirement, the tag handler must therefore give the variable a value and save it in one of the JSP scopes at the very latest in the doEndTag( ) method. Example 16.7 shows the doEndTag( ) method for the UsePropertyTag class. Example 16.7. Saving the New Object in a JSP Scope public int doEndTag( ) throws JspException { Object obj = pageContext.findAttribute(name); if (obj == null) { throw new JspException(”Variable ” + name + ” not found”); } Object propObj = getProperty(obj, property, className); pageContext.setAttribute(id, propObj); return SKIP_BODY; } The value is added to the page scope by calling the setAttribute( ) method on the current PageContext object, using the name specified by the id attribute. If the scope is specified as AT_BEGIN instead, the declaration is added before the doStartTag( ) call and the assignment code is added right after the call. In this case, the tag handler must save the variable in a JSP scope in the doStartTag( ) method. If the tag handler implements BodyTag, assignment code is also added so that it is executed for every evaluation of the body, and after the call to doAfterBody( ) . This allows the tag handler to modify the variable value in the doAfterBody( ) method, so each evaluation of the body has a new value. When we look at an iteration action later, you’ll see why this is important. Finally, if the scope is set to NESTED, both the declaration and the value assignment code are inserted in the code block representing the action body. The tag handler must therefore make the variable available in either the doStartTag( ) method or the doInitBody( ) method, and can also modify the value in the doAfterBody( ) method. The UsePropertyTagExtraInfo class sets the varName and className properties of the VariableInfo bean to the values of the id and className attributes specified by the page author in the JSP page. This is done using another simple class named TagData , passed as the argument to the getVariableInfo( ) method, as shown in Example 16.6. The TagData instance is created by the JSP container and contains information about all action attributes that the page author specified in the JSP page. It has two methods of interest. First, the getAttributeString( ) method, used in Example 16.6, simply returns the specified attribute as a String. But some attribute values may be specified by a JSP expression instead of a string literal, so-called request- time attributes. Since these values are not known during the translation phase, the TagData class also provides the getAttribute( ) method to indicate if an attribute value is a literal string, a request-time attribute, or not set at all. The getAttribute( ) method returns an Object. If the attribute is specified as a request-time value, the special REQUEST_TIME_VALUE object is returned. Otherwise, a String is returned, or null if the attribute is not set. 16.6 Developing an Iterating Action As I alluded to earlier, a tag handler can iterate over the element’s body until some condition is true. The evaluation of the body may be different for each iteration, since the tag handler can introduce a variable (used in the body) that changes its value. An example of an iterating action is the used in this book. It can be used to iterate over the element body once for each value in an array, a java.util.Vector, a java.util.Dictionary, or a java.util.Enumeration. Here’s an example of how the action can be used: <%@ page language="java" contentType="text/html" %> <%@ taglib uri="/orataglib" prefix="ora" %> <% String[] test = new String[4]; test[0] = "first"; test[1] = "second"; test[2] = "third"; test[3] = "fourth"; pageContext.setAttribute("test", test); %> page 226
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services

JavaSercer Pages The action results in code for (Private jvm hosting)

January 19, 2007 on 3:34 am | In Java | No Comments

JavaSercer Pages The action results in code for locating or creating the CatalogBean, and declaring and assigning a Java variable named catalog. But since we’re talking about custom actions here, let’s focus on the code generated for the action. First, a tag handler instance is created and initialized with the standard properties (pageContext and parent) plus all properties corresponding to the action attributes. Next, the doStartTag( ) and doEndTag( ) methods are called. Then comes the code that makes the object created by the action available as a scripting variable. Note how a variable with the name specified by the id attribute (prod) is declared, using the type specified by the className attribute. Also note that the variable is declared at the top level of the method. This means that it’s available to scripting elements anywhere on the page after the action element. The variable is then assigned the value of the object with same name located in one of the standard JSP scopes, using the findAttribute( ) method. This method searches through the scopes, in the order page, request, session, and application, until it finds the specified object. With the object available in the JSP page scope, the code generated for the action can find it. Since it’s also assigned to a Java variable, the JSP expression works correctly as well. At least two things are required for a tag handler to create a new object and make it accessible for other actions and JSP scripting code: 1. The JSP container must know the name and the Java type for the object, so it can generate the code for the variable declaration. 2. The object must be placed in one of the JSP scopes, so it can be found by findAttribute( ) and assigned to the variable. The first requirement is fulfilled by a class called TagExtraInfo . When you develop a tag handler for an action that introduces an object, you must also create a subclass of the TagExtraInfo class. The JSP container consults an instance of this class when it generates the code. Example 16.6 shows the class associated with the action. Example 16.6. UsePropertyTagExtraInfo Class package com.ora.jsp.tags.generic; import javax.servlet.jsp.tagext.*; public class UsePropertyTagExtraInfo extends TagExtraInfo { public VariableInfo[] getVariableInfo(TagData data) { return new VariableInfo[] { new VariableInfo(data.getAttributeString(”id”), data.getAttributeString(”className”), true, VariableInfo.AT_END) }; } } The method used by the JSP container during code generation is called getVariableInfo( ). It returns an array of VariableInfo objects, one per variable introduced by the tag handler. The VariableInfo class is a simple bean with four properties, all of them initialized to the values passed as arguments to the constructor: varName, className, declare, and scope. The meaning of the first two is not hard to guess: the name of the variable and the name of its class. The declare property is a boolean, in which true means that a new variable is created by the action. In other words, a declaration of the variable must be added to the generated servlet. A value of false means that the variable has already been created by another action or by another occurrence of the same action, so the generated code already contains the declaration. This is all the information the JSP container needs to generate the code for the variable declaration; the first requirement is satisfied. The scope property has nothing to do with the JSP scopes we have seen so far (page, request, session, and application). Instead, it defines where the new variable is available to JSP scripting elements. A value of AT_BEGIN means that it is available from the action’s start tag and stays available after the action’s end tag. AT_END means it is not available until after the action’s end tag. A variable with scope NESTED is available only in the action’s body, between the start and the end tags. The scope therefore controls where the variable declaration and value assignment code is generated, and the tag handler class must make sure the variable is available in one of the JSP scopes at the appropriate time. page 225
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

JavaSercer Pages The JSP 1.1 specification defines that (Servlets hosting)

January 18, 2007 on 8:35 pm | In Java | No Comments

JavaSercer Pages The JSP 1.1 specification defines that an attribute named id typically is used to name a variable created by an action.7 The value of the id attribute must be unique within the page. Because it’s used as a scripting variable name, it must also follow the variable name rules for the scripting language. For Java, this means it must start with a letter followed by a combination of letters and digits, and must not contain special characters, such as a dot or a plus sign. The attribute used in another action to refer to the variable can be named anything, but the convention established by the standard actions is to call it name. When a custom action creates a variable, it must cooperate with the JSP container to make it happen. To understand how this works, recall that the JSP page is turned into a servlet by the JSP container. The JSP container needs to generate code that declares the scripting variable in the generated servlet and assigns the variable a value. Before getting into how the tag handler and the container cooperate, let’s look at the kind of code that is generated for the custom action introduced in Chapter 8. Here’s a JSP page fragment: <% prod.getName( ); %> The action creates an instance of the CatalogBean (or locates an existing instance) and saves it in the page scope with the name catalog. It also declares a scripting variable named catalog and sets it to the same CatalogBean instance. The custom action retrieves the product property from the CatalogBean and introduces it as a page scope object named prod and a scripting variable with the same name, in the same manner as the action. Finally, the value of prod is added to the response twice: first using the action, and then again using a JSP expression. This JSP page fragment results in code similar to Example 16.5 in the generated servlet. Example 16.5. Code Generated for JSP Actions // Code for com.ora.jsp.beans.shopping.CatalogBean catalog = null; catalog= (com.ora.jsp.beans.shopping.CatalogBean) pageContext.getAttribute(”catalog”,PageContext.PAGE_SCOPE); if ( catalog == null ) { try { catalog = (com.ora.jsp.beans.shopping.CatalogBean) Beans.instantiate(getClassLoader( ), “com.ora.jsp.beans.shopping.CatalogBean”); } catch (Exception exc) { throw new ServletException (”Cannot create bean of class “+ “com.ora.jsp.beans.shopping.CatalogBean”); } pageContext.setAttribute(”catalog”, catalog, PageContext.PAGE_SCOPE); } … // Code for com.ora.jsp.tags.generic.UsePropertyTag _jspx_th_ora_useProperty_1 = new com.ora.jsp.tags.generic.UsePropertyTag( ); _jspx_th_ora_useProperty_1.setPageContext(pageContext); _jspx_th_ora_useProperty_1.setParent(null); _jspx_th_ora_useProperty_1.setId(”prod”); _jspx_th_ora_useProperty_1.setName(”catalog”); _jspx_th_ora_useProperty_1.setProperty(”product”); _jspx_th_ora_useProperty_1.setArg(”1″); _jspx_th_ora_useProperty_1.setClassName( “com.ora.jsp.beans.shopping.ProductBean”); try { _jspx_th_ora_useProperty_1.doStartTag( ); if (_jspx_th_ora_useProperty_1.doEndTag( ) == Tag.SKIP_PAGE) return; } finally { _jspx_th_ora_useProperty_1.release( ); } com.ora.jsp.beans.shopping.ProductBean prod = null; prod = (com.ora.jsp.beans.shopping.ProductBean) pageContext.findAttribute(”prod”); … // Code for out.print(pageContext.findAttribute(”prod”), “name”))); … // Code for <%= prod.getName( ) %> out.print( prod.getName( ) ); 7 If an action creates more than one variable, the id attribute is typically used to name one of them. page 224
Note: If you are looking for good and quality webspace to host and run your java application check professional java hosting services

JavaSercer Pages The class has two instance variables, (Mambo hosting)

January 18, 2007 on 2:33 pm | In Java | No Comments

JavaSercer Pages The class has two instance variables, name and value, and the corresponding setter methods. The most interesting method is the doEndTag( ) method. This method first uses the findAncestorWithClass( ) method to try to locate the enclosing EncodeURLTag instance. Note that this is not the class name used as the argument value. Instead, the ParamParent interface is used. The reason is that the action is supported in the body of other actions besides , such as the action. The ParamParent interface is implemented by all tag handlers for actions that can contain nested actions: package com.ora.jsp.tags.generic; public interface ParamParent { void setParam(String name, String value); } The interface defines one method: the setParam( ) method. This is the method the nested ParamTag tag handler uses to communicate with its parent. For each nested action, the setParam( ) method gets called when the parent’s action body is processed. The name and value for each action are accumulated in the parent tag handler, ready to be used when the parent’s doEndTag( ) method is called. Example 16.4 shows the setParam( ) and doEndTag( ) methods implemented by the EncodeURLTag class. Example 16.4. EncodeURLTag … private Vector params; … public void setParam(String name, String value) { if (params == null) { params = new Vector( ); } Param param = new Param(name, value); params.addElement(param); } public int doEndTag( ) throws JspException { StringBuffer encodedURL = new StringBuffer(url); if (params != null && params.size( ) > 0) { encodedURL.append(’?'); boolean isFirst = true; Enumeration e = params.elements( ); while (e.hasMoreElements( )) { Param p = (Param) e.nextElement( ); if (!isFirst) { encodedURL.append(’&'); } encodedURL.append(p.getName( )).append(’='). append(p.getValue( )); isFirst = false; } } try { HttpServletResponse res = (HttpServletResponse) pageContext.getResponse( ); JspWriter out = pageContext.getOut( ); out.print(res.encodeURL(encodedURL.toString( ))); } catch (IOException e) {} return SKIP_BODY; } In setParam( ), the parameter name and value are saved as instances of a simple value holder class named Param, held in a Vector. In the doEndTag( ) method, each parameter’s name/value pair is added to the URL before the complete URL is encoded to support session tracking through URL rewriting. If you don’t remember what all of this means, you can refresh your memory by looking at Chapter 8 again. 16.5 Creating New Variables Through Actions Actions can also cooperate through objects available in the standard JSP scopes (page, request, session, and application). One example of this type of cooperation is illustrated by the three standard JSP actions: , , and . The action creates a new object and makes it available in one of the JSP scopes. The other two actions can then access the properties of the object by searching for it in the scopes. Besides making the object available in one of the scopes, the action also makes it available as a scripting variable, so it can be accessed by scripting elements in the page. page 223
Note: If you are looking for cheapest and affordable webspace to host and run your servlet application check Astra j2ee hosting services

JavaSercer Pages This is part of (Servlet hosting) the puzzle.

January 18, 2007 on 8:25 am | In Java | No Comments

JavaSercer Pages This is part of the puzzle. However, a tag handler is usually interested only in finding a parent it’s been designed to work with. It would be nice to have a method that works its way up the hierarchy until it finds the parent of interest. That’s exactly what the findAncestorWithClass( ) method implemented by the TagSupport class does: public static final Tag findAncestorWithClass(Tag from, Class klass) { boolean isInterface = false; if (from == null || klass == null || (!Tag.class.isAssignableFrom(klass) && !(isInterface = klass.isInterface( )))) { return null; } for (;;) { Tag tag = from.getParent( ); if (tag == null) { return null; } if ((isInterface && klass.isInstance(tag)) || klass.isAssignableFrom(tag.getClass( ))) return tag; else from = tag; } } First of all, note that this is a static method. Consequently, it can be used even by tag handlers that implement the Tag interface directly, instead of extending the TagSupport class. The method takes two arguments: the tag handler instance to start searching from, and the class or interface of the parent. After making sure that all parameters are valid, it starts working its way up the nested tag handlers. It stops when it finds a tag handler of the specified class or interface and returns it. If the specified parent type is not found, the method returns null. This is all that’s needed to let a nested action communicate with its parent: the parent accessor methods, and the method that walks the action hierarchy to find the parent of interest. Example 16.3 shows how the ParamTag class uses this mechanism to find the enclosing EncodeURLTag instance. Example 16.3. The ParamTag Class package com.ora.jsp.tags.generic; import java.net.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class ParamTag extends TagSupport { private String name; private String value; public void setName(String name) { this.name = name; } public void setValue(String value) { this.value = value; } public int doEndTag( ) throws JspException { Tag parent = findAncestorWithClass(this, ParamParent.class); if (parent == null) { throw new JspException(”The param action is not ” + “enclosed by a supported action type”); } ParamParent paramParent = (ParamParent) parent; paramParent.setParam(name, URLEncoder.encode(value)); return EVAL_PAGE; } } page 222
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check professional tomcat hosting services

Comcast webspace - JavaSercer Pages You may be wondering why the

January 18, 2007 on 3:42 am | In Java | No Comments

JavaSercer Pages You may be wondering why the method is called getPreviousOut( ) as opposed to getOut( ). The name is intended to emphasize the fact that you want to use the object assigned as the output to the enclosing element in a hierarchy of nested action elements. Say you have the following action elements in a page: Some template text The JSP container first creates a JspWriter and assigns it to the out variable for the page. When it encounters the action, it creates a BodyContent object and temporarily assigns it to the out variable. It creates another BodyContent for the action and, again, assigns it to out. The container keeps track of this hierarchy of output objects. Template text and output produced by the standard JSP elements end up in the current output object. Each element can get access to its own BodyContent object by calling the getBodyContent( ) method and reading the content. For the element, the content is the template text. After processing the content, it can write it to the body by getting the BodyContent for this element through the getPreviousOut( ) method. Finally, the element can process the content provided by the element and add it to the top-level output object: the JspWriter object it gets by calling the getPreviousOut( ) method. The tag handler in Example 16.2 converts all the special characters it finds in its BodyContent object using the toHTMLString( ) method in the com.ora.jsp.utils.StringFormat class, introduced in Chapter 6. It gets the content of the BodyContent by using the getString( ) method, and uses it as the argument to the toHTMLString( ) method. The result is written to the JspWriter obtained by calling getPreviousOut( ). The doAfterBody( ) method then returns SKIP_BODY, since no iteration is needed. 16.4 Letting Actions Cooperate Now that you’ve seen how to develop basic tag handlers, let’s discuss some more advanced features. In this section, we look at tag handlers that let a page author use custom actions that cooperate with each other. You have seen examples of this throughout this java blog. For instance, in Chapter 9, various types of value actions are nested within the body of an action to set the values of place holders in the SQL statement. Another example is the action with nested actions, which are used in Chapter 8: servlet hosting services

« Previous PageNext Page »