Q.1 Which of the following methods will be invoked when a ServletContext is destroyed? (Select one)
a. contextDestroyed() of javax.servlet.ServletContextListener
b. contextDestroyed() of javax.servlet.HttpServletContextListener
c. contextDestroyed() of javax.servlet.http.ServletContextListener
d. contextDestroyed() of javax.servlet.http.HttpServletContextListener
a. contextDestroyed() of javax.servlet.ServletContextListener
b. contextDestroyed() of javax.servlet.HttpServletContextListener
c. contextDestroyed() of javax.servlet.http.ServletContextListener
d. contextDestroyed() of javax.servlet.http.HttpServletContextListener
Answer: a
Explanation
Remember that the concept of servlet context applies to all the servlets and not just HttpServlets. Therefore, interfaces related to servlet context belong to the javax.servlet package.
Explanation
Remember that the concept of servlet context applies to all the servlets and not just HttpServlets. Therefore, interfaces related to servlet context belong to the javax.servlet package.
Q.2 Which of the following methods will be invoked when a ServletContext is created? (Select one)
a. contextInstantiated() of javax.servlet.ServletContextListener
b. contextInitialized() of javax.servlet.ServletContextListener
c. contextInited() of javax.servlet.ServletContextListener
d. contextCreated() of javax.servlet.ServletContextListener
a. contextInstantiated() of javax.servlet.ServletContextListener
b. contextInitialized() of javax.servlet.ServletContextListener
c. contextInited() of javax.servlet.ServletContextListener
d. contextCreated() of javax.servlet.ServletContextListener
Answer: b
Explanation
On the exam, you will be asked questions that require you to know the method names for all the methods of the servlet API. As in this question, the options may be very confusing.
Explanation
On the exam, you will be asked questions that require you to know the method names for all the methods of the servlet API. As in this question, the options may be very confusing.
Q.3 Consider the following class:
import javax.servlet.*;
public class MyListener implements ServletContextAttributeListener {
public void attributeAdded(ServletContextAttributeEvent scab) {
System.out.println(“attribute added”);
}
public void attributeRemoved(ServletContextAttributeEvent scab) {
System.out.println(“attribute removed”);
}
}
import javax.servlet.*;
public class MyListener implements ServletContextAttributeListener {
public void attributeAdded(ServletContextAttributeEvent scab) {
System.out.println(“attribute added”);
}
public void attributeRemoved(ServletContextAttributeEvent scab) {
System.out.println(“attribute removed”);
}
}
Which of the following statements about the above class is correct? (Select one)
a. This class will compile as is.
b. This class will compile only if the attributeReplaced() method is added to it.
c. This class will compile only if the attributeUpdated() method is added to it.
d. This class will compile only if the attributeChanged() method is added to it.
a. This class will compile as is.
b. This class will compile only if the attributeReplaced() method is added to it.
c. This class will compile only if the attributeUpdated() method is added to it.
d. This class will compile only if the attributeChanged() method is added to it.
Answer: b
Explanation
ServletContextAttributeListener also declares the public void attributeReplaced(
ServletContextAttributeEvent scab) method, which is called when an existing attribute is replaced by another one.
Explanation
ServletContextAttributeListener also declares the public void attributeReplaced(
ServletContextAttributeEvent scab) method, which is called when an existing attribute is replaced by another one.
Q.4 Which method is used to retrieve an attribute from a ServletContext? (Select one)
a. String getAttribute(int index)
b. String getObject(int index)
c. Object getAttribute(int index)
d. Object getObject(int index)
e. Object getAttribute(String name)
f. String getAttribute(String name)
g. String getObject(String name)
a. String getAttribute(int index)
b. String getObject(int index)
c. Object getAttribute(int index)
d. Object getObject(int index)
e. Object getAttribute(String name)
f. String getAttribute(String name)
g. String getObject(String name)
Answer: e
Explanation
Since we can store any type of object in a servlet context, the getAttribute() method returns an object. You can then cast the returned object to whatever type you expect it to be.
Explanation
Since we can store any type of object in a servlet context, the getAttribute() method returns an object. You can then cast the returned object to whatever type you expect it to be.
Q.5 Which method is used to retrieve an initialization parameter from a Servlet-Context? (Select one)
a. Object getInitParameter(int index)
b. Object getParameter(int index)
c. Object getInitParameter(String name)
d. String getInitParameter(String name)
e. String getParameter(String name)
a. Object getInitParameter(int index)
b. Object getParameter(int index)
c. Object getInitParameter(String name)
d. String getInitParameter(String name)
e. String getParameter(String name)
Answer: d
Explanation
Initialization parameters are specified in the deployment descriptor. Since we can only specify strings in the deployment descriptor, the getInitParameter() method returns a String.
Explanation
Initialization parameters are specified in the deployment descriptor. Since we can only specify strings in the deployment descriptor, the getInitParameter() method returns a String.
Q.6 Which deployment descriptor element is used to specify a ServletContext-Listener? (Select one)
a. <context-listener>
b. <listener>
c. <servlet-context-listener>
d. <servletcontextlistener>
e. <servletcontext-listener>
a. <context-listener>
b. <listener>
c. <servlet-context-listener>
d. <servletcontextlistener>
e. <servletcontext-listener>
Answer: b
Explanation
All the listeners that are specified in the deployment descriptor are specified using the <listener> element:
<listener>
<listener-class>com.abc.MyServletContextListener</listener-class>
</listener>
Explanation
All the listeners that are specified in the deployment descriptor are specified using the <listener> element:
<listener>
<listener-class>com.abc.MyServletContextListener</listener-class>
</listener>
The servlet container automatically figures out the type of interface that the specified class implements.
Q.7 Which of the following web.xml snippets correctly specify an initialization parameter for a servlet context? (Select one)
a. <context-param>
<name>country</name>
<value>USA</value>
<context-param>
b. <context-param>
<param name=”country” value=”USA” />
<context-param>
c. <context>
<param name=”country” value=”USA” />
<context>
d. <context-param>
<param-name>country</param-name>
<param-value>USA</param-value>
<context-param>
a. <context-param>
<name>country</name>
<value>USA</value>
<context-param>
b. <context-param>
<param name=”country” value=”USA” />
<context-param>
c. <context>
<param name=”country” value=”USA” />
<context>
d. <context-param>
<param-name>country</param-name>
<param-value>USA</param-value>
<context-param>
Answer: d
Explanation
Initialization parameters for the servlet context are specified using the <contextparam> element, which contains exactly one <param-name> and exactly one
<param-value> element.
Explanation
Initialization parameters for the servlet context are specified using the <contextparam> element, which contains exactly one <param-name> and exactly one
<param-value> element.
Q.8 Which of the following is not a requirement of a distributable web application? (Select one)
a. It cannot depend on the notification events generated due to changes in the ServletContext attribute list.
b. It cannot depend on the notification events generated due to changes in the session attribute list.
c. It cannot depend on the notification events generated when a session is activated or passivated.
d. It cannot depend on the notification events generated when ServletContext is created or destroyed.
e. It cannot depend on the notification events generated when a session is created or destroyed.
a. It cannot depend on the notification events generated due to changes in the ServletContext attribute list.
b. It cannot depend on the notification events generated due to changes in the session attribute list.
c. It cannot depend on the notification events generated when a session is activated or passivated.
d. It cannot depend on the notification events generated when ServletContext is created or destroyed.
e. It cannot depend on the notification events generated when a session is created or destroyed.
Answer: c Explanation
A servlet container may not propagate ServletContextEvents (generated when a context is created or destroyed) and ServletContextAttribute-Events (generated when the attribute list of a context changes) to listeners residingin other JVMs. This means that your web application cannot depend on these notifications. The same is true for events generated when a session is created or destroyed and when the attribute list of a session changes.
A servlet container may not propagate ServletContextEvents (generated when a context is created or destroyed) and ServletContextAttribute-Events (generated when the attribute list of a context changes) to listeners residingin other JVMs. This means that your web application cannot depend on these notifications. The same is true for events generated when a session is created or destroyed and when the attribute list of a session changes.
A session resides in only one JVM at a time. So, all the session attributes that implement HttpSessionActivationListener receive notifications when the session is activated or passivated.
Q.9 Which of the following is a requirement of a distributable web application? (Select one)
a. It cannot depend on ServletContext for sharing information.
b. It cannot depend on the sendRedirect() method.
c. It cannot depend on the include() and forward() methods of the RequestDispatcher class.
d. It cannot depend on cookies for session management.
a. It cannot depend on ServletContext for sharing information.
b. It cannot depend on the sendRedirect() method.
c. It cannot depend on the include() and forward() methods of the RequestDispatcher class.
d. It cannot depend on cookies for session management.
Answer: a Explanation
Since each JVM has a separate instance of a servlet context for each web application (except the default one), the attribute set in a ServletContext on one JVM will not be visible in the ServletContext for the same application on another JVM.
Since each JVM has a separate instance of a servlet context for each web application (except the default one), the attribute set in a ServletContext on one JVM will not be visible in the ServletContext for the same application on another JVM.
No comments:
Post a Comment