Appendix A. Answers to the Quiz Questions

Quiz Answers for Day 1

A1:

From HTML pages, you saw the history of JSP traced through Java, JavaScript, Java applets, Java servlets, and then to JSP. All of those elements have left their mark on JSP programming.

A2:

You saw an overview of JSP scripting elements, comments, directives, and actions. Together, these make up the four different types of elements in JSP.

A3:

There are three different types of JSP scripting elements: scriptlets (for general code), declarations (which declare variables or methods), and expressions (which are evaluated and the result inserted into the Web page sent back to the browser).

A4:

Scriptlets are created with <% and %> tags, declarations with <%! and %> tags, and expressions with <%= and %> tags.

A5:

The three directive types are page, include, and taglib. You'll see them all at work in this book.

Quiz Answers for Day 2

A1:

application, config, Exception, out, page, pageContext, request, response, and session.

A2:

You print out the appropriate HTML, such as "<BR>".

A3:

There are only two: float and double.

A4:

Boolean variables can only hold two values, true and false.

A5:

The String methods toLowerCase, toUpperCase, and replace don't return the same String; they return a new String object.

Quiz Answers for Day 3

A1:

An if-else ladder. You can only use switch statements with types byte, char, short, or int in Java.

A2:

You add a default statement to the switch statement.

A3:

The initialization_expression, the end_condition, and the iteration_expression. The initialization_expression is executed before the loop is executed the first time; the end_condition is checked every time through the loop, and when it becomes false, the loop ends; and iteration_expression is executed after each time through the loop.

A4:

The break statement lets you end a loop early.

A5:

When you pass an item by value to a method, you pass a copy of that value. When you pass an item by reference, you actually pass the address of the item, and the code in the method has direct access to the passed item.

Quiz Answers for Day 4

A1:

You use the ACTION attribute, setting it to an URL.

A2:

If you use the GET method, the data in the form is treated as text and appended to the URL that the browser navigates to. The POST method encodes the data as part of the actual HTTP request sent to the server

A3:

Submit buttons, and text fields if there are no other controls in the form—by convention, when the user presses Enter in that case, the form's data is sent to the server. (And as you'll see tomorrow, image controls can also send data back to the server when clicked.)

A4:

The request object is an object of the Java javax.servlet.http.HttpServletRequest class. And that class is based on the javax.servlet.ServletRequest class.

A5:

You can use the request.getHeader("User-Agent") method.

Quiz Answers for Day 5

A1:

You use the request object's getParameters method, not the getParameter method. This method returns a java.util.Enumeration object. You loop over with the enumeration with the hasMoreElements method, and get the names with the nextElement method.

A2:

You give all the radio buttons the same name (using the NAME attribute), but different values (using the VALUE attribute) so you know which one was selected.

A3:

You get a drop-down list—and note that drop-down lists don't support multiple selections.

A4:

They're named image1.x and image1.y.

A5:

You can use <jsp:param> elements inside the <jsp:forward> element.

Quiz Answers for Day 6

A1:

The two access specifiers we've seen today are public and private.

A2:

You invoke the Java compiler at the command line. Its name is javac, so you enter that name followed by the name of the Java source code file you want to compile, such as javac ch06_01.java.

A3:

The directory is named classes. It's a subdirectory of the WEB-INF directory, which itself is a subdirectory of the directory that holds the JSP page.

A4:

The <jsp:getProperty> and <jspSetProperty> elements inside a <jsp:useBean> element are not executed if the object the <jsp:useBean> element refers to already exists before that element is executed.

A5:

It was deprecated because it did not lend itself to internationalization.

Quiz Answers for Day 7

A1:

The Cookie object method getName gets the name of the cookie, and its getValue method gets its value.

A2:

The default timeout between user accesses for a session in the Tomcat server is 30 minutes.

A3:

Over repeated page accesses, the session ID will be different each time, and the isNew method always returns true.

A4:

You can set the <jsp:useBean> element's scope attribute to page, request,session, or application.

A5:

The default setting for the page directive's session attribute is true, which means that you use sessions by default in JSP.

Quiz Answers for Day 8

A1:

A syntax error is one that prevents your code from being compiled, and an exception is a runtime error that happens when your compiled code runs.

A2:

The base class of the Exception class is the java.lang.Throwable class. The Exception class includes the methods of that class.

A3:

You use a finally block to handle exceptions not explicitly caught in any catch block. The code in this block will be executed whether or not an exception occurred.

A4:

The toString method is automatically called to convert the exception to a string. This usually displays an error message.

A5:

You use the getAttribute(java.lang.String name) method to get a request attribute's value, and the setAttribute(java.lang.String name, java.lang.Object o) method to set an attribute.

Quiz Answers for Day 9

A1:

The taglib directive. Set the uri attribute to the tag library's unique identifier, and the prefix attribute to the prefix you want to use for the tags.

A2:

The web.xml file. That file goes in the directory's WEB-INF subdirectory.

A3:

You use the <taglib> element's <taglib-uri> element to specify the library's URI, and the <taglib-location> element to specify the Java class to use.

A4:

You use the <tag> element's <name> element to give the name of the tag, and the <tag-class> element to give the name of the Java class for the tag.

A5:

As you saw in the code today, you can base your tag's Java class on the TagSupport class.

Quiz Answers for Day 10

A1:

The subelements of the <taglib> element are <tlib-version>, <jsp-version>, <short-name>, <uri>, <display-name>, <small-icon>, <large-icon>, <description>, <listener>, and <tag>.

A2:

The subelements of the <tag> element are <name>, <tag-class>, <tei-class>, <body-content>, <display-name>, <small-icon>, <large-icon>, <description>, <variable>, and <attribute>.

A3:

The doStartTag and doEndTag methods.

A4:

Technically, all you have to use is the <name> attribute. The type of the attribute is java.lang.String by default.

A5:

You use the pageContext.getAttribute method.

Quiz Answers for Day 11

A1:

Java will call the superclass's default constructor automatically.

A2:

With the super keyword. Java selects the constructor based on the argument list you pass to super: super(int1, float1). Don't forget that if you use super, it must be the first line in a subclass's constructor.

A3:

You use the protected access specifier.

A4:

Overloading a method means defining more than one version of the method, where each version has a different argument list. Overriding a method means redefining it (with the same argument list) in a subclass.

A5:

Only method a.

Quiz Answers for Day 12

A1:

You use the code attribute to specify the name of the .class file for the applet, and, if the .class file is in another directory, you can use the codebase attribute to specify that directory.

A2:

You use the goGet method to handle data sent with GET, and doPost to handle data sent with POST. You can also use the service method to handle both GET and POST requests. All these methods are passed HttpServletRequest and HttpServletResponse objects.

A3:

You can use the getWriter method of the HttpServletRequest object passed to you in goGet or doPost to get a PrintWriter object. Then you can use the PrintWriter object's print or println methods to send text to the Web page.

A4:

That's an easy one—the method is called init.

A5:

A servlet configuration parameter holds data for just one servlet, whereas a servlet context parameter can be accessed by all servlets in the same Web application.

Quiz Answers for Day 13

A1:

You would use a servlet context to share data across multiple servlets.

A2:

You use the RequestDispatcher class.

A3:

The model implements the internal logic of the application, the view handles the presentation of the data to the user, and the controller oversees the whole application, calling the model and the view as needed. The user interacts with the controller.

A4:

It's called tomcat-users.xml.

A5:

You use the <security-constraint> and <login-config> elements.

Quiz Answers for Day 14

A1:

To implement the Filter interface, you must override three methods—init, destroy, and doFilter.

A2:

You use the <filter> and <filter-mapping> elements.

A3:

The <filter-name> and <url-pattern> subelements.

A4:

The order is A→B→C→D→C→B→A.

A5:

You store the FilterConfig object passed to the filter's init method. Then, in the doFilter method, you can use this object's getInitParameter method to get the value of an initialization parameter.

Quiz Answers for Day 15

A1:

You can use the File class's delete method.

A2:

You can use the File class's exists method.

A3:

You can use the application object's getRealPath method.

A4:

You pass a value of true as the second parameter to the FileWriter class's constructor:

FileWriter filewriter = new FileWriter(file, true); 

A5:

You can use the FileInputStream and FileOutputStream stream classes to work with binary files.

Quiz Answers for Day 16

A1:

You use the Class.forName or the System.setProperty methods.

A2:

You use the DriverManager.getConnection method.

A3:

You can use a Connection object's createStatement method to create a new Statement object.

A4:

You can use the execute, executeBatch, executeQuery, or executeUpdate methods of a Statement object.

A5:

You can use the ResultSet object's getDouble method like this: resultSet.getDouble(4) (remember that field indexes are 1-based) or resultSet.getDouble("comment").

Quiz Answers for Day 17

A1:

A forward-only cursor, which only enables you to move forward through the result set.

A2:

You can use the ResultSet class's getFloat method.

A3:

You can set the cursor type in the createStatement method of the Connection object.

A4:

You can use the absolute method.

A5:

You can use the result set's getMetaData method to get a ResultMetaData object, and use this object's getColumnCount method to get the number of columns in the result set.

Quiz Answers for Day 18

A1:

You use the contentType attribute of the JSP page directive, and you can set it to application/xml to send XML to the browser.

A2:

You use the DocumentBuilderFactory class to create a DocumentBuilder object, then use the DocumentBuilder object's parse method.

A3:

You can use the getNodeName and getNodeValue methods.

A4:

You override the startElement and endElement methods.

A5:

You can use a tag like <xsl:template match="PLANET">, where you've given XSLT elements the prefix xsl.

Quiz Answers for Day 19

A1:

You use the <html:text> tag.

A2:

You use the <bean:message> tag.

A3:

It's the ActionServlet class. Fully qualified, the name is org.apache.struts.action.ActionServlet.

A4:

You use the <action-mappings> element.

A5:

You use an object of the ActionError class to register an error with the Struts framework.

Quiz Answers for Day 20

A1:

It's the java.awt.image.BufferedImage class.

A2:

You can use the JPEGCodec.createJPEGEncoder method, passing it an OutStream object.

A3:

You use the Graphics2D class's fillRect method.

A4:

You pass either the name of the Web host you're trying to connect to or its IP address, and the port you want to use.

A5:

You use the ServerSocket class.

Quiz Answers for Day 21

A1:

When you're writing to the document's body (as with the JavaScript document.write method), you have to wait until the body is loaded first. You can do that by placing the <SCRIPT> element in the <BODY> element. (Another way is to use the document object's ONLOAD event attribute; this event occurs after the document is loaded.)

A2:

You use the ONSUBMIT event attribute.

A3:

You use the ONDBLCLICK event attribute.

A4:

You can use the form's submit method, such as this: document.form1.submit().

A5:

You deploy WAR files to the webapps directory, and JAR files to lib directories.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.235.79