Archive for December, 2007

Sunday, December 30th, 2007

Line 11 shows a JSP expression containing a variable declared with a JSP declare. Line 12 shows some text concatenated with a Java method invocation. The HTML paragraph tags are outside the JSP expression tag markers. JSP may accept HTML tags coded within the tag markers. However, including HTML tags, which are formatting elements, with logic elements within JSP expressions is usually a poor coding practice. The generated HTML is identical to that shown in Listing 3-3; no need to be repetitious. Now that you ve seen a few simple JSP pages, you might wonder about competing technologies. How does JSP stack up against the competition? What features does JSP have that the competition doesn t? What are the advantages and disadvantages of using JSP? The above questions are addressed in the next section.
We provide special commissions and earns up to $125 us per referral for all website hosting directories. With such big commissions you should immediately sign up for our affiliate program for website hosting directory sites.

Saturday, December 29th, 2007

on these JSP predefined objects. Notice that lines 13 and 14 output an HTML

tag. Although you can code HTML tags with JSP output statements, you should not do so. It’s good design practice to separate the programming logic, provided by coding JSP tags, from page formatting tags. You can read a bit more on the separation of logic from presentation later in this chapter. Listing 3-3 shows the generated HTML resulting from the JSP page in Listing 3-2. Listing 3-3: The HTML generated by the JSP page 1 2 3 4 ShowForwardAndReverse.jsp 5 6 7

You Entered Lou Marco 8

Your Name Backwards is ocraM uoL 9 10 Listing 3-4 shows another variation of the JSP page that can generate the HTML shown in Listing 3-3. Listing 3-4: A variation of the forward and backward JSP page 1 <%@ page language="java" %> 2 <%--JSP Page With a Declaration and Expressions --%> 3 4 5 ShowForwardAndReverse.jsp 6 7 8 <%-- Here is a JSP declaration --%> 9 <%! String yourName = request.getParameter("yourname") ; %> 10 <%-- Here are some JSP expressions %> 11

You Entered <%= yourName %>

<%= "Your Name Backwards is " + 12 new StringBuffer(yourName).reverse() %> 13 14 Line 2 of Listing 3-4 is a JSP comment and line 1 is the same JSP directive as coded in Listing 3-2. Again, lines 3 through 7 and 13 and 14 are straight HTML. Line 9 is an example of a JSP declaration. JSP declarations are bracketed with <%! and %>. You may code complete Java declare statements and complete Java method code in a JSP declaration. Line 9 shows the declaration and initialization of the string variable containing the string entered in the HTML form. Lines 11 and 12 are examples of JSP expressions. A JSP expression is compiled to an output statement that writes a string. The string is formed by evaluating the expression sandwiched between <@= and %>. JSP expressions use the toString() method to convert non-string objects to strings for output, as in the following expression: <%= yourName %> produces the same output as the scriptlet below: <% out.println( yourName ) ; %>
Our window web hosting plans are bursting with features and FREE tools, at the very small rate. Sign up today window web hosting.

Saturday, December 29th, 2007

1 <%@ page language="java" %> 2 <%--JSP Page With Scriptlet --%> 3 4 5 6 ShowForwardAndReverse.jsp 7 8 9 <%-- Here is the JSP scriptlet --%> 10 <% String yourName = request.getParameter("yourname") ; 11 StringBuffer yourNameReverse = 12 new StringBuffer(yourName).reverse( ) ; 13 out.println("

You Entered ” + yourName ) ; 14 out.println(”

Your Name Backwards is ” + yourNameReverse ) ; 15 %> 16 17 The first thing that pops out is that the JSP page contains elements that begin with the characters <% and end with the characters %>. JSP elements are bracketed with <% and %>. The JSP engine identifies types of JSP elements with additional characters appended to <%. For example, lines 2 and 9, bracketed with <%--and --%>, are JSP comments. The JSP engine does not include JSP comments during the class file translation and, of course, the JSP engine does not use the JSP comments when compiling the page into a servlet. As an aside, line 3, the HTML comment, is passed to the JSP engine for translation and compilation. Lines 3 through 8 and 16 and 17 are static HTML, which are passed to the JSP engine for translation and compilation. The resultant servlet writes these lines to the output stream with out.write statements. Line 1 is an example of a JSP directive. A JSP directive sets various page parameters that affect the structure and properties of the JSP page. The directive coded on line 2 states that the scripting language used in this JSP page is Java. In the JSP 1.1 and 1.2 specifications, the only defined and required scripting language for the language attribute is java. However, other JSP implementations support other scripting languages beside Java. Allaire’s “Jrun” and Caucho Technology’s “Resin” are JSP implementations that support JavaScript and Java as scripting languages. A few points about coding JSP directives are in order here. Notice that the JSP directive (line 1) is bracketed by <%@ and %>. You may code whitespace between the @ sign and the directive (page in this case). You cannot code any whitespace between the attribute and its value (language=”java” in this case). Chapter 4, “The Elements of a JSP Page,” contains more information on JSP directives. Lines 10 through 15 contain a JSP scriptlet. Scriptlets are pieces of Java code that are inserted into the service method generated by the JSP translator. Scriptlets are sandwiched between <% and %>. Line 10 shows a Java String declare with a call to method getParameter from the predefined request object instantiated from class HttpServletRequest. As you can read in Chapter 4, JSP pages have access to a set of predefined objects, of which the request object is one. The call to getParameter requires a parameter name represented as a string object as an argument. The argument used is the name of the text input box coded on line 2 from Listing 3-1 (yourname). Line 11 shows another string declare, yourNameReverse, initialized with a call to reverse(), a method from the StringBuffer class in the java.lang package. You do not need to do anything to have JSPs recognize elements from java.lang because JSPs are translated into a Java class and then compiled into a servlet. The compiler that creates the servlet does not need any import statements or special setup to recognize elements from the java.langpackage. If your JSP needed methods from, say, java.util.Vector, you would have to use a JSP directive (the page directive, actually) to generate an import statement that imports the required methods. Lines 13 and 14 compile to output statements that write the entered name forward and backward. These two lines reference println from another JSP predefined object, out. As previously mentioned, Chapter 4 has the full scoop
Don’t want to have just any web hosting, but web hosting provider who will share the same beliefs? You have found them. Our Church Web Hosting company will treat in you in appropriate way, the one you are accustomed to.

Friday, December 28th, 2007

EJB & JSP: Java On The Edge, Unlimited Edition by Lou Marco ISBN: 0764548026 Your Guide to Cutting-Edge J2EE Programming Techniques. Examining Some JSP Pages In this section, you ll see a few simple JSP pages and read a brief explanation of the JSP elements and how these elements produce dynamic content. A Simple JSP Page: Your Name Here and ereH emaN ruoY This JSP page code in Listing 3-1 requests that the user enter his or her name, after which it invokes a JSP that displays the user s name and the name spelled backward. Listing 3-1 shows an HTML page that contains a reference to a JSP followed by a JSP that generates the HTML containing the entered name and the name in reverse. Listing 31 shows the HTML page, forwardreverse.html, that requests user input. Listing 3-1: HTML page requesting a JSP 1 2 3 JSP Page to Display Name, Forward and Reverse 4 5 6

Enter Your Name Below

7
8 9 10
11 12 The HTML page above does not contain any JSP. Rather, the
tag on line 7 invokes a JSP named ShowForwardAndReverse.jsp when the user clicks the Submit button. If the user enters the name Lou Marco, the SUBMIT process encodes the data and passes it as a name-value pair, yourname=Lou+Marco, and then passes the following URL to the server: http://localhost:8080/forwardreverse.html?yourname=Lou+Marco. Note Use the Tomcat Web server, which has a JSP engine. See Appendix C for instructions on installing and configuring the Tomcat server. Listing 3-2 is the first of two examples that show slightly different ways to code ShowForwardAndReverse.jsp. Listing 3-2: A JSP page that prints name forward and reverse
Are you tired of finding web hosting providers listed as inexpensive?Just check our Inexpensive Web Hosting services.

Thursday, December 27th, 2007

Communicating with Other Beans The standard Java event delegation mechanism enables you to code JavaBeans to communicate with other objects or beans. Beans that are the source of communication are designated event sources, whereas interested beans, ones that care about the activities of source beans, are the event listeners. For example, you may want bean A to know when properties in bean B change. If so, bean B is the event source and bean A is the event listener. The events bean A listens for are property change events.
CGI is used because it is far better and more physical than other ways , such as constructing miniatures for effects shots or hiring a cheap deal of extras for crowd scenes, and because it allows the creation of images that would not be feasible using any other technology.Check fore more details under our cgi web hosting section.

Wednesday, December 26th, 2007

37 38 39 40 41 42 43 44 45 public double getResult() { return performTheOperation() ; //6} /** Perform the operation…..generate the result*/ private double performTheOperation( ) { double aResult = 0.0 ; 46 47 48 if ( operation.equals(”+”) ) aResult = operand1 + operand2 ; else 49 50 51 if ( operation.equals(”-”) ) aResult = operand1 - operand2 ; else 52 53 54 if ( operation.equals(”*”) ) aResult = operand1 * operand2 ; else 55 56 57 if ( operation.equals(”/”) ) aResult = operand1 / operand2 ; 58 59 60 } } return aResult ; A cursory examination of Listing 6-1 reveals its purpose, which is to serve as a simple calculator. CalcBean has four properties: two operands, an operation, and the result of the operation applied to the operands. To keep things simple, this bean does not include any code that checks for errors (such as zerodivide) or performs any edit checks on property values. Line 1 shows the bean stored as a package. It is a good Java programming practice to use packages to group related classes together. Here, we have only one class so the use of packages may seem a bit pedantic. Later, you ll see where you should put the CalcBean class within the Tomcat server s directory structure. Line //2 shows the declarations for the bean properties. Notice that the properties are declared with the privatevisibility modifier. The bean requirement is that bean properties are not declared public; you can have declared bean properties as protected or use default package visibility. If our bean has subclasses that need access to the bean s properties, you can make a case for using the protected visibility modifier. Of course, you can still declare the properties private and use get/set methods within the subclasses to gain access to the bean properties. Note that because bean properties are permitted to have initialized values, when this bean is created, its properties are given these initial values. Line //3 shows the no-arg constructor. You do not need to explicitly code the empty constructor because, as you recall, Java uses the empty constructor by default. The important point is not to code a constructor that requires arguments. Line //4 shows a typical set method and line //5 shows a typical get method. The coding of the get and set methods follows the convention described in the preceding section. Remember, if you do not code your accessor methods according to the described convention, bean tools and the JSP container recognize the methods as accessor methods. Line //6 shows a get method that invokes a method to generate the result of the calculation. You could have included the body of the method performTheOperation within the get method or coded performTheOperationin another bean or class. JavaBeans, as software components, must have the ability to communicate with other components. In the following section, you can read how to code your beans so they can communicate with other beans.
We feature a web hosting shopping cart and live support solution. Just try our web hosting shopping cartwhich provides a secure way of obtaining payments through your website.

Tuesday, December 25th, 2007

For example, the method invocation coded below reads the fourth element (or index value 3): aVarOfSupportedType = getAnArrayOfThisType( 3 ) ; The set method that writes an indexed property requires two arguments: the array position and the data to be written to the property. Here s the method header for a set method for the indexed property declared in the previous code listing: public void setAnArrayOfThisType( int arrIndex, SupportedType aVar ) For example, the method invocation in the following writes some data into the fourth array element: setAnArrayOfThisType( 3, aVarOfSupportedType ) ; Now that you ve seen the basic elements that comprise a bean, let s take a look at a simple bean. Creating a Simple Bean The code in Listing 6-1 shows a simple bean. Later in the chapter, this bean will be used in a JSP. Listing 6-1: The CalcBean class 1 package cbean ; //1 2 public class CalcBean { 3 /** 4 Calculator bean for Chapter 6 5 */ 6 private int operand1 = 0 ; //2 7 private int operand2 = 0 ; 8 private double result = 0 ; 9 private String operation = “” ; 10 11 //No-arg constructor for bean…. 12 public CalcBean() { } //3 13 14 //Get/Set methods follow 15 public void setOperand1( int op1 ) { //4 16 operand1 = op1 ; 17 } 18 public void setOperand2( int op2 ) { 19 operand2 = op2 ; 20 } 21 public void setOperation( String oper ) { 22 operation = oper ; 23 } 24 public void setResult( double aResult) { 25 result = aResult ; 26 } 27 public int getOperand1() { //5 28 return operand1 ; 29 } 30 public int getOperand2() { 31 return operand2 ; 32 } 33 public String getOperation() { 34 return operation; 35 } 36
Adult web hosting has rich experience in providing unique solutions for the customer’s needs, just check frontpage web hosting services.

Monday, December 24th, 2007

that do all sorts of useful work before returning the element. You are free to use any elements at your disposal to derive a value for the method to return. For example: public SomeClassOrPrimitiveType getAnInstanceVariable() { Class1 objectClass1 = new Class1() ; SomeClassOrPrimitiveType aVar = objectClass1AnotherMethod ( objectClass1, anotherElement ) ; return aVar ; } In the preceding example a new object is created and another method queried before the final value, aVar is returned to the caller. You may also have get methods that don’t query an specific instance variable. For example, if you have a bean with the boolean property networkUp, the method isNetworkUp would query the network and return a result based on the status of the network. In practice, many get methods just return the current value of the instance variable. Much of the manipulation of setting instance variable values is done in “set” methods. Set, or mutator, methods enable a bean user to change the value of a bean property. Here s an example modeled after the get method above: public void setAnInstanceVariable(SomeClassOrPrimitiveType aVar) { anInstanceVariable = aVar ; } Notice the following about the set method: . The set method is declared public. . The set method returns void. . The set method takes one argument of the same class or primitive type as the instance variable. . The name of the get method is the word “set” followed by the name of the instance variable with the first letter of the variable name capitalized. Notice that setAnInstanceVariable, anInstanceVariable is set to the value of aVar which was passed into the method. While it is not a requirement of the set method that it contain one or more assignment statements, this is usually the case. You don t need to write both get and set methods for a bean property. If your JSP or Java code doesn t need to change a bean property, then you shouldn t create a set method for that property. The following section takes a quick look at coding accessor methods for indexed bean properties. Coding Accessor Methods for Indexed Bean Properties The basic idea and rationale for coding get and set methods for indexed properties is the same as those for non- array properties. Needing some mechanism for accessing an array element, consider the following indexed property declaration: private SupportedType[] anArrayOfThisType ; Then the method header would appear as follows: public SupportedType getAnArrayOfThisType( int arrIndex )
File Transfer Protocol comes together with anonymous ftp access with every FTP Web Hosting account we offer today. Enjoy burs table ftp transfers together with fast FTP connection.

Sunday, December 23rd, 2007

The naming convention details depend on whether or not the instance variable representing a bean property is an array or not. Bean properties represented as arrays are known as indexed properties. First, let s consider the case in which bean properties are not represented as arrays. Coding Accessor Methods for Non-Array Bean Properties The following is a description of the naming convention for accessor methods of non-array bean properties: Given a variable named anInstanceVariable, declared as follows: private SomeClassOrPrimitiveType anInstanceVariable ; the get method that reads the variable s name can be coded as follows: public SomeClassOrPrimitiveType getAnInstanceVariable() { return anInstanceVariable ; } Notice the following about the variable declaration and the get method: . The variable anInstanceVariable is declared private. The private declaration insures that users of your bean cannot access the bean s instance variables at will. The bean user has to access the instance variables through an approved interface. . The get method is declared public. The get method is part of the approved interface the bean exposes to the outside world. . The get method takes no arguments. . The name of the get method is the word “get” followed by the name of the instance variable with the first letter of the variable name capitalized. However, when the instance variable is of type boolean, you may name the method starting with the word is instead of the word get. . The get method returns some element of the same class or primitive data type as the instance variable. The preceding list of restrictions is required for bean use. Suppose you coded the getAnInstanceVariablemethod without following the rules? For example: public SomeClassOrPrimitiveTypegetAnInstanceVariable( Class1 objClass1 ) { return anInstanceVariable ; } You could invoke the method to get the instance variable. However, JavaBean tools would not know the preceding coded method is a get method. The difference in signatures between the no-argument get method and the method coded above would “fool” the bean tool. Stated differently, the preceding coded method does not follow the standard for coding JavaBeans. To the bean tool, the above method is not related to a bean property. One common mistake is to forget that the first letter of the instance variable name included in the set method name must be capitalized. Hence, the method header coded as follows fails the naming standard: public SomeClassOrPrimitiveType getanInstanceVariable() When you access bean properties in your JSPs, you must code get methods according to the convention described previously, or else the JSP engine, as with bean tools, will fail to recognize the method as a get method. In the preceding example, the get method merely returns the instance variable. Of course, you may code methods
For the first time, E-commerce websites allow small and large companies to actually compete on a level playing field.We highly recommend you to visit ecommerce website hosting.

Saturday, December 22nd, 2007

Note You may encounter the term bean as you read about JavaBeans, here and throughout Java literature. A bean is an instance of a class created as a JavaBean. In some circles, a bean may mean an Enterprise JavaBean, too. In this book, bean refers to an instance of a JavaBean class and enterprise bean refers to an instance of an Enterprise JavaBean. Professional Java development environments use the Java Programming Language’s inherent introspection feature to peek inside JavaBeans, enabling the Java developer to access and change the properties of JavaBeans by using a GUI. The environments use structures akin to property sheets to get and set bean property values. You must follow a convention, which you may already know, when naming your accessor methods. This convention is explained later in this chapter in the section Coding JavaBean Property Accessor Methods. It s important to note that a Java developer can use a JavaBean without knowing anything about the bean s internals; the entire state of the bean is described through its properties, which must be accessible through the bean s accessor methods. After all, that s what writing component software is all about. Another requirement of all JavaBeans is that they implement either the Serializable interface or the Externalizable interface. This requirement allows a bean to be persistent, an attribute inherent to all objects that implement either of these interfaces. In this book we will only consider the Serializable interface. You may be muttering to yourself, Great! I have to become a JavaBean maven in order to use JavaBeans in JavaServer Pages. Don t worry! Everything you need to know about using JavaBeans in your JSPs is in the preceding brief definition. If you create a public class with a zero-argument constructor and no public instance variables, you can use this class in your JSP pages. If you want to save persistent data, you can write this data to a database or implement Serializable. Rather than coding blocks of scriptlets or methods in your JSPs, you can code JavaBeans containing the scriptlet or method code and invoke the methods from the bean in your JSPs. Now, that s not so bad, is it? Before you read about how to code your JSPs to use JavaBeans, a few words about coding JavaBeans are in order. Coding JavaBeans Basically, coding JavaBeans resembles coding any Java class; remember to code your bean public, write (or enable Java to automatically create) a zero-argument constructor, define no public instance variables, and use getand set methods to provide access to your nonpublic instance variables. You may already know how to code a public Java class and use a zero-argument class constructor. Whether this is the case or not, here s an example of a public class declaration with a zero-argument constructor: public class SomeClassName { //Here s the no arg constructor public SomeClassName() { } //Rest of the code for this class follows } You can give your instance variables any visibility except public, although most of the time you may opt for private visibility for your instance variables. Writing a method in a JavaBean is exactly the same as writing a method in any Java class. Code your methods as you would for any Java class, using the familiar Java language constructs you ve come to know and love. Coding JavaBean Property Accessor Methods As previously mentioned, you must follow a naming convention when coding accessor methods to read values from and write values to instance variables. JavaBean tools follow the naming convention when looking inside the bean. These tools then extract the bean properties, enabling the bean user to change the state of the bean by changing the bean properties through a property sheet.
Find the right website host for you, offering a directory of website hosts, free domain name registration tool, hosting reviews, web hosting ratings and articles for beginners, and tons of other resources, check web hosting ratings for more information.