OpenStax-CNX module: m48187 1 Java4340r: Review R.G. (Dick) Baldwin This work is produced by OpenStax-CNX and licensed under the Creative Commons Attribution License 4.0 Abstract This module contains review questions and answers keyed to the module titled Java4340: JSP Fundamentals. 1 Table of Contents Preface (p. 1) Background information (p. 2) Expression tag (p. 2) Comment tag (p. 2) Scriptlet tag (p. 2) Declaration tag (p. 3) Directive tag (p. 3) Questions (p. 3) 1 (p. 3), 2 (p. 3), 3 (p. 3), 4 (p. 3), 5 (p. 3), 6 (p. 4), 7 (p. 4), 8 (p. 4), 9 (p. 4), 10 (p. 4), 11 (p. 5), 12 (p. 5), 13 (p. 5), 14 (p. 5), 15 (p. 5), 16 (p. 6), 17 (p. 6), 18 (p. 6), 19 (p. 7), 20 (p. 7), 21 (p. 7), 22 (p. 8) Image index (p. 8) Answers (p. 10) Images (p. 16) Miscellaneous (p. 23) 2 Preface This module is one in a collection of modules on JavaServer Pages (JSP) designed for teaching ITSE2317 - Java Programming (Intermediate) at Austin Community College in Austin, TX. This module contains review questions and answers keyed to the module titled Java4340: JSP Fundamentals 1. Once you study that module, you should be able to answer the review questions in this module. The questions and the answers in this module are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back again. The Images (p. 16) section contains a number of images that you may nd helpful as reference material in answering the questions. Version 1.3: May 4, 2014 8:40 pm -0500 http://creativecommons.org/licenses/by/4.0/ 1 http://cnx.org/content/m48075/latest/?collection=col11441/latest
OpenStax-CNX module: m48187 2 3 Background information The module named Java4340: JSP Fundamentals 2 introduced your rst JSP program. This module supplements the information in that module from a slightly dierent perspective. The main purpose of this module, however, is to provide a series of review questions and answers that you can use to solidify the information in your mind. There are ve dierent kinds of JSP tags. They are shown in Image 1 (p. 17). Image 2 (p. 18) shows some examples of those tags being used in a jsp le. 3.1 Expression tag Line 11 of Image 2 (p. 18) shows an expression tag. An expression tag is used to evaluate a Java expression and to insert the results of that evaluation into outgoing HTML code. For example, the JSP expression tag on Line 11 instantiates a new object of the Date class using a constructor that causes the new Date object to encapsulate the current date and time. A String version of the current date and time is inserted into the HTML output and displayed as shown immediately below the JSP Tags header in Image 3 (p. 19). (Inserting the new Date object's reference into the expression tag causes the tostring method of the Date class to be called, returning the String shown in Image 3 (p. 19).) Note that the Java source code inside a JSP expression tag is not terminated by a semicolon. In other words, it is a Java expression and is not a Java statement. 3.2 Comment tag Lines 13 and 15 of Image 2 (p. 18) also show expression tags. However, the rst one is inside an HTML comment element and the second one is inside a JSP comment tag. Therefore, they are both ignored and have no eect on the output. 3.3 Scriptlet tag The scriptlet tag is used to insert Java source code into HTML code where it will later be compiled and executed. (Note that code inserted into a scriptlet tag uses standard Java syntax including semicolons where required.) It is not necessary for a scriptlet to contain a complete block of Java source code. By a block, I mean none, one, or more statements surrounded by matching curly brackets. A block of Java source code can begin in one scriptlet tag and end in another scriptlet tag with HTML code inserted between the two. That is the case in Image 2 (p. 18). A scriptlet tag begins on Line 17 and ends on Line 25 of Image 2 (p. 18). The end of the scriptlet tag is in the middle of a for loop block. That means that the HTML code following the end of the scriptlet tag will be evaluated during each iteration of the for loop. The HTML code that begins on Line 26 and ends on Line 30 includes two JSP expression tags. During each iteration of the for loop, this HTML code creates and populates the three elements of a single row in a three-column HTML table that begins on Line 16 and ends on Line 35. One element is populated with literal HTML text. The other two elements are populated by evaluating two dierent JSP expression tags. The output table is shown in Image 3 (p. 19). The for loop that begins on Line 24 must have a terminating curly bracket in order to compile. The terminating curly bracket is provided by the scriptlet tag that begins on Line 31 and ends on Line 34. The code in that scriptlet also assigns a new value to the variable named localvar. 2 http://cnx.org/content/m48075/latest/?collection=col11441/latest
OpenStax-CNX module: m48187 3 3.4 Declaration tag A declaration tag is used on Line 36 of Image 2 (p. 18) to declare a variable of type int named globalcount and to initialize its value to 0. Variables declared in this manner are shared by all sessions accessing the servlet. Consequently, the declaration of variables in this manner should be used very sparingly if at all. (One browser session can modify such a variable in use by another browser session, which can lead to unexpected results.) Expression tags are then used to display the values of globalcount and localvar. Finally, a scriptlet tag is used to increment the value of globalcount. 3.5 Directive tag Two uses of the directive tag are shown near the top of Image 2 (p. 18). The rst usage causes the code in the le named sample_included_le.jsp ( Image 5 (p. 20) ) to be inserted at the location of the directive tag in Image 2 (p. 18).. The code in the scriptlet tag in Image 5 (p. 20) and the code in the expression tag immediately following that scriptlet tag produces the second line of output text near the top of Image 3 (p. 19).. The code in the scriptlet tag also causes the text " Printed from Java... " (without the quotation marks) to appear on the server system console as shown in Image 6 (p. 21). The second usage of the directive tag near the top of Image 2 (p. 18) causes the java.util package and all of its classes to be imported. This is the JSP alternative to a standard Java import directive. Note that the package name being imported is surrounded by quotation marks and is not terminated with a semicolon. 4 Questions 4.1 Question 1. There are three locations where processing can occur in web development. Where are they? Go to answer 1 (p. 16) 4.2 Question 2 True or False? This course emphasizes C++ programs that run on the client machine. Go to answer 2 (p. 16) 4.3 Question 3 True or False? JSP are simply HTML pages with Java code inserted into the page. Go to answer 3 (p. 16) 4.4 Question 4 True or False? When the user accesses a JSP via a browser, the page is processed by compiling and running the Java code in the page. This often produces HTML formatted output. Go to answer 4 (p. 16) 4.5 Question 5 True or False? Each time a JSP is accessed, the code is parsed and then compiled into a servlet bytecode executable). Go to answer 5 (p. 16) (Java
OpenStax-CNX module: m48187 4 4.6 Question 6 True or False? JSP tags in an HTML page are delimited using angle brackets and percent characters as shown in Image 8 (p. 4). Image 8: Question 6. <% %> Figure 1: Image 8: Question 6. Go to answer 6 (p. 16) 4.7 Question 7 True or False? The six kinds of JSP tags are: 1. Scriptlet 2. Expression 3. Directive 4. Comment 5. Declaration 6. Code Go to answer 7 (p. 15) 4.8 Question 8 True or False? When Java code appears on JSP it is enclosed in tags. Go to answer 8 (p. 15) 4.9 Question 9 Which kind of JSP tag is normally used to enclose ordinary Java source code? Go to answer 9 (p. 15) 4.10 Question 10 NetBeans conveniently colors the scriptlet and expression sections with a special color for easy visual identication. What is that color? Go to answer 10 (p. 15)
OpenStax-CNX module: m48187 5 4.11 Question 11 True or False? A scriptlet tag must contain a complete block of Java source code. (A block of Java source code is normally surrounded by matching curly brackets.) Go to answer 11 (p. 13) 4.12 Question 12 True or False? The expression tag is used to compile and run Java code and convert the output into string expressions. Go to answer 12 (p. 13) 4.13 Question 13 True or False? An expression tag begins and ends with the characters shown in Image 11 (p. 5). Image 11: Question 13. <%@ %> Figure 2: Image 11: Question 13. Go to answer 13 (p. 13) 4.14 Question 14 True or False? Semi-colons are required in expression tags. Go to answer 14 (p. 12) 4.15 Question 15 True or False? A directive tag begins and ends with the characters shown in Image 14 (p. 6).
OpenStax-CNX module: m48187 6 Image 14: Question 15. <%@ %> Figure 3: Image 14: Question 15. Go to answer 15 (p. 12) 4.16 Question 16 True or False? The include version of the directive tag can be used to cause text from an external le to be inserted into the jsp le just as though you had typed it in. Go to answer 16 (p. 12) 4.17 Question 17 True or False? The JSP syntax shown in Image 15 (p. 6) can be used to import Java classes that apply to the entire page. Image 15: Question 17. <%@ import="java.util.date" %> Figure 4: Image 15: Question 17. Go to answer 17 (p. 11) 4.18 Question 18 True or False? The JSP comment tag begins and ends as shown in Image 17 (p. 7).
OpenStax-CNX module: m48187 7 Image 17: Question 18. <%!-- --%> Figure 5: Image 17: Question 18. Go to answer 18 (p. 11) 4.19 Question 19 True or False? The contents of the JSP comment tags are not compiled, executed, or sent back to the browser. On the other hand, regular HTML comments are compiled, executed, and sent back to the browser. However, that content is not displayed. Go to answer 19 (p. 11) 4.20 Question 20 True or False? A JSP declaration tag begins and ends as shown in Image 19 (p. 7). Image 19: Question 20. <%! %> Figure 6: Image 19: Question 20. Go to answer 20 (p. 11) 4.21 Question 21 True or False? A JSP declaration is used to declare variables and methods in a page's scripting language. When the scripting language is the Java programming language, variables and methods in JSP declarations become declarations in the JSP page's servlet class. Go to answer 21 (p. 11)
OpenStax-CNX module: m48187 8 4.22 Question 22 True or False? The code in Image 20 (p. 8) is a valid JSP declaration for a variable named globalcount. Image 20: Question 22. <%! int globalcount="0;" %> Figure 7: Image 20: Question 22. Go to answer 22 (p. 10) 5 Image index Image 1 (p. 17). JSP Tags Image 2 (p. 18). Code - jsp_tags.jsp Image 3 (p. 19). Output - jsp_tags.jsp Image 4 (p. 20). Code - sample_included_le.jsp (w/ errors) Image 5 (p. 20). Code - sample_included_le.jsp Image 6 (p. 21). Console Output - jsp_tags.jsp Image 7 (p. 22). View Source - jsp_tags.jsp Image 8 (p. 4). Question 6. Image 9 (p. 14). A JSP for loop. Image 10 (p. 15). JSP output. Image 11 (p. 5). Question 13. Image 12 (p. 13). Expression tag. Image 13 (p. 13). Scripting-language expression. Image 14 (p. 6). Question 15. Image 15 (p. 6). Question 17. Image 16 (p. 12). Importing a Java class. Image 17 (p. 7). Question 18. Image 18 (p. 11). A JSP comment tag. Image 19 (p. 7). Question 20. Image 20 (p. 8). Question 22. Image 21 (p. 10). A JSP declaration. What is the meaning of the following two images? These images were inserted here simply to insert some space between the questions and the answers to keep them from being visible on the screen at the same time.
OpenStax-CNX module: m48187 9 This image was also inserted for the purpose of inserting space between the questions and the answers.
OpenStax-CNX module: m48187 10 6 Answers 6.1 Answer 22 False. The correct syntax for the declaration of a variable named 10). globalcount is shown in Image 21 (p. Image 21: A JSP declaration. <%! int globalcount = 0; %> Figure 8: Image 21: A JSP declaration. This question was included in this review to emphasize that unlike the directive tag, the declaration tag does not use HTML attribute syntax. Instead, it uses standard Java syntax at least for the declaration
OpenStax-CNX module: m48187 11 of variables. Go back to Question 22 (p. 8) 6.2 Answer 21 True. Go back to Question 21 (p. 7) 6.3 Answer 20 True. Go back to Question 20 (p. 7) 6.4 Answer 19 True. Go back to Question 19 (p. 7) 6.5 Answer 18 False. The correct syntax for a JSP comment tag is shown in Image 18 (p. 11). Image 18: A JSP comment tag. <%-- --%> Figure 9: Image 18: A JSP comment tag. Note that unlike an HTML comment tag, the code in Image 18 (p. 11) doesn't include an exclamation mark character. Go back to Question 18 (p. 6) 6.6 Answer 17 False. The correct format of the directive tag for importing a class is shown in Image 16 (p. 12).
OpenStax-CNX module: m48187 12 Image 16: Importing a Java class. <%@ page import="java.util.date" %> Figure 10: Image 16: Importing a Java class. Note the inclusion of the word page. Note also the use of the equal character and the quotation marks. While similar, this is not the format of an import directive in a Java source code le. Instead, it is the format of an attribute in XML or HTML. Go back to Question 17 (p. 6) 6.7 Answer 16 True. Go back to Question 16 (p. 6) 6.8 Answer 15 True. Go back to Question 15 (p. 5) 6.9 Answer 14 False. According to The Java EE 5 Tutorial 3, A JSP expression is used to insert the value of a scripting language expression, converted into a string, into the data stream returned to the client. When the scripting language is the Java programming language, (as is the case here) an expression is transformed into a statement that converts the value of the expression into a String object and inserts it into the implicit out object. The syntax for an expression is shown in Image 13 (p. 13). 3 http://docs.oracle.com/javaee/5/tutorial/doc/bnaov.html
OpenStax-CNX module: m48187 13 Image 13: Scripting-language expression. <%= scripting-language-expression %> Figure 11: Image 13: Scripting-language expression. Note that a semicolon is not allowed within a JSP expression, even if the same expression has a semicolon when you use it within a scriptlet. Go back to Question 14 (p. 5) 6.10 Answer 13 False.. An expression tag begins and ends with the characters shown in Image 12 (p. 13). Image 12: Expression tag. <%= %> Figure 12: Image 12: Expression tag. Go back to Question 13 (p. 5) 6.11 Answer 12 True. Go back to Question 12 (p. 5) 6.12 Answer 11 False. A scriptlet tag can contain a fragment of Java code. For example, in Image 9 (p. 14), one scriptlet tag begins on Line 17 and ends on Line 24. This tag contains the beginning of a for loop, but does not contain a complete block of Java code. Another scriptlet tag appears on Line 27. (Note that the green background is missing from this tag. That is because the cursor was on Line 27 when the screen shot was
OpenStax-CNX module: m48187 14 taken.) Another scriptlet tag extends from Line 29 through Line 31. This tag contains the closing curly bracket for the for loop. This example shows a common method of interspersing scriptlet code with HTML code using fragments of Java code in scriptlet tags. Image 9: A JSP for loop. Figure 13: Image 9: A JSP for loop. This for loop shown in Image 9 (p. 14) along with the associated HTML tags produces the table shown in an excerpt from the HTML output page in Image 10 (p. 15).
OpenStax-CNX module: m48187 15 Image 10: JSP output. Figure 14: Image 10: JSP output. Go back to Question 11 (p. 5) 6.13 Answer 10 NetBeans conveniently colors the scriptlet and expression sections green for easy visual identication. Go back to Question 10 (p. 4) 6.14 Answer 9 The scriptlet tag is normally used to enclose ordinary Java source code on JSP but a declaration tag can be used as well. Go back to Question 9 (p. 4) 6.15 Answer 8 True. Go back to Question 8 (p. 4) 6.16 Answer 7 False. The following are valid JSP tags: 1. Scriptlet 2. Expression 3. Directive 4. Comment 5. Declaration Item 6 (p. 4) in the list, Code, is not a valid JSP tag. Go back to Question 7 (p. 4)
OpenStax-CNX module: m48187 16 6.17 Answer 6 True. Go back to Question 6 (p. 4) 6.18 Answer 5 False. The rst time a JSP is accessed, the code is parsed and then compiled into a servlet (Java bytecode executable). Subsequent visits to the page use the compiled executable to produce output. This is more ecient that some other approaches that require more overhead each time a server-side program is accessed. Go back to Question 5 (p. 3) 6.19 Answer 4 True. Go back to Question 4 (p. 3) 6.20 Answer 3 True. Go back to Question 3 (p. 3) 6.21 Answer 2 False. This course emphasizes Java programs that run on the web server. Go back to Question 2 (p. 3) 6.22 Answer 1 The three locations are 1. the client machine 2. the web server, and 3. other servers. Go back to Question 1 (p. 3) 7 Images This section contains images that may be referred to by more than one question or answer. These images may also be helpful as reference material for answering the questions. Many of them were copied directly from Java4340: JSP Fundamentals 4. Other images may be interspersed within the questions and answers. 4 http://cnx.org/content/m48075/latest/?collection=col11441/latest
OpenStax-CNX module: m48187 17 Image 1: JSP Tags Figure 15: Image 1: JSP Tags
OpenStax-CNX module: m48187 18 Image 2: Code - jsp_tags.jsp Figure 16: Image 2: Code - jsp_tags.jsp
OpenStax-CNX module: m48187 19 Image 3: Output - jsp_tags.jsp Figure 17: Image 3: Output - jsp_tags.jsp
OpenStax-CNX module: m48187 20 Image 4: Code - sample_included_le.jsp (w/ errors) Figure 18: Image 4: Code - sample_included_le.jsp (w/ errors) Image 5: Code - sample_included_le.jsp Figure 19: Image 5: Code - sample_included_le.jsp
OpenStax-CNX module: m48187 21 Image 6: Console Output - jsp_tags.jsp Figure 20: Image 6: Console Output - jsp_tags.jsp
OpenStax-CNX module: m48187 22 Image 7: View Source - jsp_tags.jsp Figure 21: Image 7: View Source - jsp_tags.jsp
OpenStax-CNX module: m48187 23 8 Miscellaneous This section contains a variety of miscellaneous information. note: Housekeeping material Module name: Java4340r: Review File: Java4340r.htm Published: 12/05/13 Revised: 05/04/14 note: Disclaimers: Financial : Although the Connexions site makes it possible for you to download a PDF le for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF le, you should be aware that some of the HTML elements in this module may not translate well into PDF. I also want you to know that, I receive no nancial compensation from the Connexions website even if you purchase the PDF version of the module. In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge. Aliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX. -end