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.

Leave a Reply