Question 2) Sun Certified for J2EE Platform 1.4
Objective: The Web Container Model
SubObjective : For the ServletContext initialization parameters: write servlet code to access initialization parameters; and create the deployment descriptor elements for declaring initialization parameters.
Multiple Answer Multiple Choice
Which two method calls using the ServletContext interface will facilitate access to a file named kaplandata.bin stored in the /doc-root/kaplan/docs directory? (Choose two.)
- getURL(“/doc-root/kaplan/docs/kaplandata.bin”)
- getResource(“/doc-root/kaplan/docs/kaplandata.bin”)
- getAttribute(“/doc-root/kaplan/docs/kaplandata.bin”)
- getServletContextName(“/doc-root/kaplan/docs/kaplandata.bin”)
- getResourceAsStream(“/doc-root/kaplan/docs/kaplandata.bin”)
Answer:
B. getResource(“/doc-root/kaplan/docs/kaplandata.bin”)
E. getResourceAsStream(“/doc-root/kaplan/docs/kaplandata.bin”)
Tutorial:
The getResource(“/doc-root/kaplan/docs/kaplandata.bin”) method call will return a URL to a file named kaplandata.bin stored in the /doc-root/kaplan/docs directory. The getResource(java.lang.String path) method of the ServletContext interface returns a java.net.URL object. The getResourceAsStream(“/doc-root/kaplan/docs/kaplandata.bin”) method call will facilitate access to the /doc-root/kaplan/docs/kaplandata.bin file. The getResourceAsStream(“/doc-root/kaplan/docs/kaplandata.bin”) method call will return the /doc-root/kaplan/docs/kaplandata.bin file as a java.io.InputStream object.
The getURL(“/doc-root/kaplan/docs/kaplandata.bin”) method call is incorrect because the ServletContext interface does not have a getURL method.
The getAttribute(“/doc-root/kaplan/docs/kaplandata.bin”) method call will not facilitate access to a file named kaplandata.bin stored in the /doc-root/kaplan/docs directory. The getAttribute(java.lang.String name) method returns the attribute associated with the specified name.
The getServletContextName(“/doc-root/kaplan/docs/kaplandata.bin”) method call will not facilitate access to a file named kaplandata.bin stored in the /doc-root/kaplan/docs directory. The getServletContextName method returns the name of the Web application associated with the ServletContext object as specified in the deployment descriptor.
Reference:
1. Java Servlet Specification Version 2.4, javax.servlet, SRV.14.2 The javax.servlet package, SRV.14.2.8 ServletContext
2. Java 2 Platform Enterprise Edition, v 1.4 API Specification, Interface ServletContext, http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html
These questions are derived from the Self-Test Software Practice Test for 310-081 – Sun Certified Web Component Developer for J2EE Platform 1.4 .