Servlets Generate HTML - Quiz Explanation

The correct answers are indicated below, along with text that explains the correct answers.
 
1. The HTTP request object passed to your server represents:
Please select the best answer.
  A. The information your servlet will send back to the browser
  B. The information the browser sent to the server
  C. The user’s email address
  D. An object to which you can write HTML
  The correct answer is B.
The HTTP request represents the request that the browser makes to the server with a GET or POST request.
A is incorrect because the information sent back to the browser is represented by the HTTP response object. C is incorrect because browsers do not send email addresses with their requests.
D is incorrect because you do not write HTML to a request object; you can write HTML to a response object.


2. The doGet() method of a servlet:
Please select the best answer.
  A. Sometimes uses the HTTP request object
  B. Always uses the HTTP request object
  C. Changes the values in the HTTP request object
  D. Cannot access the HTTP request object
  The correct answer is A.
Some servlets have doGet() methods that use the information in the HTTP request. Other servlets have doGet() methods that do not. A servlet cannot change the values in the HTTP request object.

3. Before writing any HTML to a response object, which tasks must be performed?
Please select all the correct answers.
  A. Clear the response
  B. Set the content type of the response to plain text HTML and create an output writer object associated with the response
  C. Create a response object
  D. Create a request object associated with the response
  The correct answers is B .
You must set the content type of the response, and create a writer to which you can write HTML. A is incorrect because there is no need to clear the response before generating HTML. C is incorrect because a response object has already been created and passed to you.
E is incorrect because a request object has already been created and passed to you.

4. To write HTML to a writer object associated with an HTTP response, what method do you use?
Please select the best answer.
  A. printHtml()
  B. write()
  C. writeHtml()
  D. println()
  The correct answer is D. The println() method of the writer is the method that writes HTML to the response.
A is incorrect because there is no printHtml() method – HTML is just text. B and C are also incorrect method names.


5. What is the extension for an HTML file that will have part of its content filled in with a server side include?
Please select the best answer.
  A. .html
  B. .ihtml
  C. .shtml
  D. .inc
  The correct answer is C.
The extension .shtml identifies documents that rely on server-side includes for some of the content. The other answers are incorrect.

6. The <SERVLET> tag is used when including servlet output in a page of ordinary HTML. What is the only required attribute for the <SERVLET> tag?
Please select the best answer.
  A. ALT
  B. CODE
  C. BORDER
  D. CODEBASE
  The correct answer is B.
The CODE attribute identifies the servlet that will generate the output. The other attributes are not required for SERVLET tags.

7. To get a session object, you call a method of which class?
Please select the best answer.
  A. HttpServlet
  B. HttpServletRequest
  C. HttpServletResponse
  D. ServletException
  The correct answer is B.
A session object reference is returned by the getSession() method of HttpServletRequest. An HttpServletRequest object is the first parameter to the doGet()method of any servlet.

8. If two users are requesting pages from the same server at the same time, which of the following statements are true?
Please select the best answer.
  A. The server will create a single session object and the servlets will share it
  B. The server will create two session objects, one for each user
  C. Servlets may create sessions, one for each user
  D. The second user will have to wait until the first session is over
  The correct answer is C.
Session objects are created by servlets if they are needed. One session object is created for each user, and servlets being run by different users cannot access the same session object. A is incorrect because sessions are not shared. B is incorrect because the server doesn’t create the session objects. D is incorrect because many users can have sessions at one time.

9. The parameter passed to the getSession() method of the request object can have the values true or false. What does a value of true mean?
Please select the best answer.
  A. Your code will be changing session variables
  B. Your code will be using the values of session variables
  C. A new session should be created if none exists
  D. An existing session should be reused
  The correct answer is C.
Passing true to getSession() means that a new session should be created if none exists. A and B are incorrect because your code can both use and change values in the session object no matter what parameter you pass to getSession(). D is incorrect because an existing session will be reused no matter what you pass: the parameter controls what happens when no session exists.


10. Which statement describes the way most programmers work with servlets and forms?
Please select the best answer.
  A. The form is kept in a separate HTML and only doPost() can process it
  B. The form is kept in a separate HTML file, and both doGet() and doPost() can process it
  C. doGet() generates the form HTML, and both doGet() and doPost() can process it
  D. doGet() generates the form HTML, and doPost() processes it
  The correct answer is D.
The same doGet() method that is used to generate HTML with any servlet is used to generate form HTML that has METHOD=POST, and doPost() is used to process it. The other answers represent approaches that are technically possible, but not as commonly used. Systems that keep the HTML in a separate file as answers A and B suggest are harder to maintain.
Servlets that use doGet() to generate the form and to process it need to discover whether a GET request is requesting the blank form or submitting the form with METHOD=GET, which is tricky code to write and maintain. The approach is answer D is easy to maintain, and the most popular approach.

11. The getParameterValues() of the HTTP request object gives you:
Please select the best answer.
  A. A string holding all the form field values
  B. An array of strings, each holding one form field value
  C. An array of strings, each holding a value for the field you specify
  D. An array of Object references, a mixture of strings and numbers
  The correct answer is C.
You pass a string name to getParameterValues(), and get an array of strings for that field, not all the fields on the form. A is incorrect because all the form fields are not in one string. B is incorrect because not all the form fields are in the array. D is incorrect because all the values come to the servlet as strings.