Sun River
Topics about Java SE, Servlet/JSP, JDBC, MultiThread, UML, Design Pattern, CSS, JavaScript, Maven, JBoss, Tomcat, ...
posts - 78,comments - 0,trackbacks - 0

Question 1:

You need to create a database connection in your application after reading the username, password, and database server URL from the deployment descriptor. Which will be the best place to do this?

Choices:

  • A. Servlet constructor
  • B. init() method
  • C. service() method
  • D. doGet() method
  • E. doPost() method

Correct choice:

  • B

Explanation:

The init() method is invoked once and only once by the container, so the creation of the database connection will be done only once, which is appropriate. The service() , doGet() , and doPost() methods might be called many times by the container.

The username, password, and URL are to be read from the deployment descriptor. These initialization parameters are contained in the ServletConfig object, which is passed to the init() method. That is why we need to use the init() method instead of the constructor for this purpose, even though the constructor is also called only once.

Question 2:

A user can select multiple locations from a list box on an HTML form. Which of the following methods can be used to retrieve all the selected locations?

Choices:

  • A. getParameter()
  • B. getParameters()
  • C. getParameterValues()
  • D. getParamValues()
  • E. None of the above

Correct choice:

  • C

Explanation:

The getParameterValues(String paraName) method of the ServletRequest interface returns all the values associated with a parameter. It returns an array of Strings containing the values. The getParameter() method returns just one of the values associated with the given parameter, so choice A is incorrect. There are no methods named getParameters() or getParamValues() , so choices B and D are incorrect.

posted on 2006-10-08 05:01 Sun River 阅读(271) 评论(0)  编辑  收藏 所属分类: Servlet & Jsp