JavaServer Page

February 1, 2007

Java reflection Instead of the conventional approach, our

Filed under: JSP Tag Libraries — webmaster @ 10:43 am

CHAPTER 8 Using JavaBeans with tags object, our tag would still be usable (without changes or even recompilation) since it would always look for a method with the name that was passed as the property attribute. This type of flexibility can only be achieved by using the reflection API. We now see how this flexibility can be applied to JavaBeans. 8.2 JavaBeans and reflection The topic of JavaBeans has books devoted to the subject, so we needn t delve into its finer points. We will only mention issues that directly affect the tags we develop in this chapter; namely, JavaBean properties, introspection, and what all this has to do with reflection. What then are JavaBeans? In a nutshell, a JavaBean is merely a Java class. Java- Beans conventions are the de facto development conventions (as introduced by Java- Soft) for Java components. These conventions define how a JavaBean is to expose its properties and events. JavaBeans publish properties and any events they provide through a strict method signature pattern, making these method names predictable so that Java development and debugging tools may easily use the reflection API to learn about the bean and offer a visual interaction with it. When a tool uses reflection to analyze a bean in this way, we call it introspection. The benefit of building Java components that adhere to JavaBeans conventions is that they are guaranteed to work well with any of the multitude of JavaBean supporting software tools available. 8.2.1 Tags and JavaBeans Most interactions between our tags and beans will revolve around fetching data from the bean and presenting it. Typically, bean interaction will involve a JSP getting the value of some property of a bean and displaying that value to the user. In light of this, we forgo discussing the second role of the JavaBean standard we mentioned, which is defining how events are specified. Primarily, our tags are concerned with two bean-related issues: Introspecting the beans to find the properties and get the methods that these tags should call for property retrieval. Calling these methods with the correct set of parameters. The next two sections deal with the properties of JavaBeans and introspecting them. 8.2.2 JavaBeans properties What makes a JavaBean unique is that it conforms to specific criteria regarding how it exposes its properties and events. What exactly are properties? Properties are attributes of the JavaBean, something in its state or functionality that the bean

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

Java reflection Instead of the conventional approach, our

Filed under: JSP Tag Libraries — webmaster @ 10:43 am

Java reflection Instead of the conventional approach, our tag extracts Method objects from the HttpServletRequest class and stores them in a hashtable. The key to the stored methods is the name of the property the method retrieves. NOTE Note that we use Class.getMethod() to obtain the Method objects. Class.getMethod() expects two parameters: (1) The name of the method and (2) an array of class objects in which each entry in the array specifies the type of argument. In our case, using this approach was easy since all the needed methods have an empty argument list (array of size zero). This parameter is required since Java supports method overloading; meaning, a class may contain more than one method with the same name as long as the arguments to those methods are different. C Fetches the method object from the method cache; the key to the method cache is the property name D Invokes the method using the current request variable; the method parameter list is an empty array of objects (no parameters) When a request arrives, serving it is a breeze. All we need to do is fetch the method object that is stored in the cache, using the property name as the key. We use the Method class s invoke() with the current request object as the first parameter, and an empty argument list as the second. We use an empty argument list since none of the methods we are calling takes any arguments. E Handles the InvocationTargetException If the invoked method throws an exception, we will need to handle it here. The implementation of QueryRequestTag as seen in listing 8.1 is very different from what might have been expected had we worked without reflection. A conventional implementation of this tag would have taken the property name and performed a few if-else statements based on its value until it knew the method to use, and then it would call that method. Using reflection completely changes this algorithm. What did we gain? We could implement this with a simple if-else statement. We gained extensibility! Suppose that we want to add new property for the tag to handle simply add the code to fetch and store the Method in the method cache, and we re finished. In the case of this tag, the work to support a new method with reflection isn t much more (if any) than the work it takes to add another condition to an if-else statement. To further appreciate reflection, imagine if our tag did not store methods in a hashtable and, instead, simply looked for the methods by name at runtime. This approach would allow our tag to call any get method on the request. If the Servlet API were updated to add new properties to the request

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

Java reflection Array class The Array class offers

Filed under: JSP Tag Libraries — webmaster @ 3:10 am

CHAPTER 8 Using JavaBeans with tags methods.put(”userPrincipal”, b reqc.getMethod(”getUserPrincipal”, p)); methods.put(”remoteUser”, reqc.getMethod(”getRemoteUser”, p)); } catch(Throwable t) { } } protected String property = null; public void setProperty(String property) { this.property = property; } public int doStartTag() throws JspException { try { Method m = (Method)methods.get(property); c if(null != m) { writeHtml(pageContext.getOut(), m.invoke(pageContext.getRequest(), d params).toString()); return SKIP_BODY; } else { // Log and throw a JspTagException } } catch(java.io.IOException ioe) { // User probably disconnected … // Log and throw a JspTagException } catch(InvocationTargetException ite) { e // Exception in the called method // Log and throw a JspTagException } catch(IllegalAccessException iae) { // We are not allowed to access this method // Log and throw a JspTagException } } protected void clearProperties() { property = null; super.clearProperties(); } } B Obtains method objects from the HttpServletRequest class and stores them in a method cache for later use. First we create an empty array of classes (see note) and procure an instance of HttpServletRequest from which to retrieve the methods.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

Java reflection Array class The Array class offers

Filed under: JSP Tag Libraries — webmaster @ 3:10 am

Java reflection Array class The Array class offers functionality for manipulating arrays of an unknown type. We ll forgo a deeper discussion of this class since the tags in this chapter won t need to use it. Constructor class Constructor class represents the constructor of a JavaBean, including any and all parameters to it (much like Method class). This class will not be used in our tags either so, once again, we ll forgo discussing it here. Using reflection: QueryRequestTag To better understand reflection, let s develop a tag that uses the reflection API. The tag will call some methods (to fetch a request property) of the request (HttpServletRequest) object using reflection. The source for the QueryRequestTag is in listing 8.1. Listing 8.1 Source code for the QueryRequestTag handler package book.reflection; import java.util.Hashtable; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import book.util.LocalStrings; import book.util.ExTagSupport; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspException; public class QueryRequestTag extends ExTagSupport { static Object []params = new Object[0]; static Hashtable methods = new Hashtable(); static LocalStrings ls = LocalStrings.getLocalStrings(QueryRequestTag.class); static { try { Class []p = new Class[0]; Class reqc = HttpServletRequest.class; methods.put(”method”, reqc.getMethod(”getMethod”, p)); methods.put(”queryString”, reqc.getMethod(”getQueryString”, p)); methods.put(”requestURI”, reqc.getMethod(”getRequestURI”, p)); b

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services

« Previous Page

Powered by Java Web Hosting