Adv - Java Means Durga Sir: Plot No: 202, Iind Floor, Huda Maitrivanam, Ameerpet, Hyd-5000038
Adv - Java Means Durga Sir: Plot No: 202, Iind Floor, Huda Maitrivanam, Ameerpet, Hyd-5000038
Page 1
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Unit- 3: The Web Container Model
Objectives
4. Describe the Web container life cycle event model for requests,
sessions, and web applications;create and configure listener classes for
each scope life cycle; create and configure scope attribute listener
classes; and given a scenario, identify the proper attribute listener to
use.
Page 2
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
A. HttpSession B. ServletConfig
C. ServletContext D. HttpServletRequest
E. HttpServletResponse
Answer: D
Q2.our web site has many user-customizable features, for example font and color
preferences on web pages. Your IT department has already built a subsystem for
user preferencessing the Java SE platform's lang.util.prefs package APIs, and you
have been ordered to reuse this subsystem in your web application. You need to
create an event listener that constructs the preferences actory and stores it in the
application scope for later use. Furthermore, this factory requires that the URL to a
database must be declared in the deployment descriptor like this:
42. <context-param>
43. <param-name>prefsDbURL</param-name>
44. <param-value>
45. jdbc:pointbase:server://dbhost:4747/prefsDB
46. </param-value>
47. </context-param>
Page 3
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
}
// more code here
}
C. public class PrefsFactoryInitializer implements ServletContextListener {
public void contextInitialized(ServletContextEvent e) {
ServletContext ctx = e.getServletContext();
String prefsURL = ctx.getInitParameter("prefsDbURL");
PreferencesFactory myFactory = makeFactory(prefsURL);
ctx.setAttribute("myPrefsFactory", myFactory);
}
// more code here
}
D. public class PrefsFactoryInitializer implements ContextListener {
public void contextCreated(ServletContext ctx) {
String prefsURL = ctx.getParameter("prefsDbURL");
PreferencesFactory myFactory = makeFactory(prefsURL);
ctx.putAttribute("myPrefsFactory", myFactory);
}
// more code here
}
Answer: C
Page 4
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
D. configure a listener in the application deployment descriptor, using the <listener>
element
E. include a class implementing ServletContextListener as part of the web application
deployment
F. include a class implementing ContextDestroyedListener as part of the web application
deployment
G. include a class implementing HttpSessionAttributeListener as part of the web
application deployment
Answer: D, E
Q4.You want to create a filter for your web application and your filter will
implement javax.servlet.Filter.
Which two statements are true? (Choose two.)
A. Your filter class must implement an init method and a destroy method.
B. Your filter class must also implement javax.servlet.FilterChain.
C. When your filter chains to the next filter, it should pass the same arguments it
received in its doFilter method.
D. The method that your filter invokes on the object it received that implements
javax.servlet.FilterChain can invoke either another filter or a servlet.
E. Your filter class must implement a doFilter method that takes, among other things, an
HTTPServletRequest object and an HTTPServletResponse object.
Answer: A, D
Page 5
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
C. A subclass of HttpServletRequestWrapper CANNOT modify the behavior of the
getReader method.
D. An HttpServletRequestWrapper may be used only by a class implementing the
javax.servlet.Filter interface.
E. An HttpServletRequestWrapper CANNOT be used on the request passed to the
RequestDispatcher.include method.
F. An HttpServletRequestWrapper may modify the header of a request within an object
implementing the javax.servlet.Filter interface.
Answer: A, B, F
Page 6
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Q7.Click the Exhibit button.
Page 7
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Q8.Given the definition of MyServlet:
Answer: A
Q9.You need to store a Java long primitive attribute, called customerOID, into the
session scope. Which two code snippets allow you to insert this value into the
session? (Choose two.)
Answer: A, C
Page 8
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Q10.Your web application requires the adding and deleting of many session
attributes during a complex use case. A bug report has come in that indicates that
an important session attribute is being deleted too soon and a NullPointerException
is being thrown several interactions after the fact. You have decided to create a
session event listener that will log when attributes are being deleted so you can track
down when the attribute is erroneously being deleted.
Q11.One of the use cases in your web application uses many session-scoped
attributes. At the end of the use case, you want to clear out this set of attributes
from the session object.
Assume that this static variable holds this set of attribute names:
A. session.removeAll(USE_CASE_ATTRS);
B. for ( String attr : USE_CASE_ATTRS ) {
session.remove(attr);
}
C. for ( String attr : USE_CASE_ATTRS ) {
session.removeAttribute(attr);
}
D. for ( String attr : USE_CASE_ATTRS ) {
Page 9
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
session.deleteAttribute(attr);
}
E. session.deleteAllAttributes(USE_CASE_ATTRS);
Answer: C
Q12. You have a simple web application that has a single Front Controller servlet
hat dispatches to JSPs to generate a variety of views. Several of these views require
further database processing to retrieve the necessary order object using the orderID
request parameter. To do this additional processing, you pass the request first to a
servlet that is mapped to the URL pattern WEB-INF/retreiveorder.do in the
deployment descriptor. This servlet takes two request parameters, the orderID and
the jspURL. It handles the database calls to retrieve and build the complex order
objects and then it dispatches to the jspURL. Which code snippet in the Front
Controller servlet dispatches the request to the order retrieval servlet?
A. request.setAttribute("orderID", orderID);
request.setAttribute("jspURL", jspURL);
RequestDispatcher view
= context.getRequestDispatcher("/WEB-INF/retreiveOrder.do");
view.forward(request, response);
B. request.setParameter("orderID", orderID);
request.setParameter("jspURL", jspURL);
Dispatcher view
= request.getDispatcher("/WEB-INF/retreiveOrder.do");
view.forwardRequest(request, response);
C. String T="/WEB-INF/retreiveOrder.do?orderID=%d&jspURL=%s";
String url = String.format(T, orderID, jspURL);
RequestDispatcher view
= context.getRequestDispatcher(url);
view.forward(request, response);
Page 10
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
D. String T="/WEB-INF/retreiveOrder.do?orderID=%d&jspURL=%s";
String url = String.format(T, orderID, jspURL);
Dispatcher view = context.getDispatcher(url);
view.forwardRequest(request, response);
Answer: C
Q13.You want to create a filter for your web application and your filter will
implement javax.servlet.Filter.
Which two statements are true? (Choose two.)
A. Your filter class must implement an init method and a destroy method.
B. Your filter class must also implement javax.servlet.FilterChain.
C. When your filter chains to the next filter, it should pass the same arguments it
received in its doFilter method.
D. The method that your filter invokes on the object it received that implements
javax.servlet.FilterChain can invoke either another filter or a servlet.
E. Your filter class must implement a doFilter method that takes, among other things, an
HTTPServletRequest object and an HTTPServletResponse object.
Answer: A, D
A. <include/>
B. <dispatcher>INCLUDE</dispatcher>
C. <dispatcher>include</dispatcher>
D. <filter-condition>INCLUDE</filter-condition>
E. <filter-condition>include</filter-condition>
Answer: B
Q15. Your web application uses a simple architecture in which servlets handle
requests and then forward to a JSP using a request dispatcher. You need to pass
information calculated by the servlet to the JSP; furthermore, that JSP uses a
Page 11
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
custom tag and must also process this information. This information must NOT be
accessible to any other servlet, JSP or session in the webapp. How can you
accomplish this goal?
Q17. Your web application uses a simple architecture in which servlets handle
requests and then forward to a JSP using a request dispatcher. You need to pass
information calculated in the servlet to the JSP for view generation. This
information must NOT be accessible to any other servlet, JSP or session in the
webapp. Which two techniques can you use to accomplish this goal?(Choose two.)
A. Add attributes to the session object.
B. Add attributes on the request object.
C. Add parameters to the request object.
D. Use the pageContext object to add request attributes.
E. Add parameters to the JSP's URL when generating the request dispatcher.
Answer: B
Page 12
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Q18. Given:
String value = getServletContext().getInitParameter("foo");
in an HttpServlet and a web application deployment descriptor that contains:
<context-param>
<param-name>foo</param-name>
<param-value>frodo</param-value>
</context-param>
Answer: A, C
Q19. Click the Exhibit button. Given the web application deployment descriptor
elements:
11. <filter>
12. <filter-name>ParamAdder</filter-name>
13. <filter-class>com.example.ParamAdder</filter-class>
14. </filter>
31. <filter-mapping>
32. <filter-name>ParamAdder</filter-name>
33. <servlet-name>Destination</servlet-name>
34. </filter-mapping>
...
55. <servlet-mapping>
56. <servlet-name>Destination</servlet-name>
57. <url-pattern>/dest/Destination</url-pattern>
58. </servlet-mapping>
What is the result of a client request of the Source servlet with no query string?
Page 13
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
A. chain.forward(request, response);
Page 14
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
B. chain.doFilter(request, response);
C. request.forward(request, response);
D. request.doFilter(request, response);
Answer: B
A. REQUEST_URI
B. javax.servlet.forward.request_uri
C. javax.servlet.forward.REQUEST_URI
D. javax.servlet.request_dispatcher.request_uri
E. javax.servlet.request_dispatcher.REQUEST_URI
Answer: B
Q22.One of the use cases in your web application uses many session-scoped
attributes. At the end of the use case, you want to clear out this set of attributes
from the session object. Assume that this static variable holds this set of attribute
names:
Which code snippet deletes these attributes from the session object?
A. session.removeAll(USE_CASE_ATTRS);
B. for ( String attr : USE_CASE_ATTRS ) {
Page 15
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
session.remove(attr);
}
C. for ( String attr : USE_CASE_ATTRS ) {
session.removeAttribute(attr);
}
D. for ( String attr : USE_CASE_ATTRS ) {
session.deleteAttribute(attr); }
E. session.deleteAllAttributes(USE_CASE_ATTRS);
Answer: C
Q23. You need to store a floating point number, called Tsquare, in the session scope.
Which two code snippets allow you to retrieve this value? (Choose two.)
Answer: B, D
Q24. You need to store a floating point number, called Tsquare, in the session scope.
Which two code snippets allow you to retrieve this value? (Choose two.)
Answer: B, D
Page 16
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Q25.You need to store a Java long primitive attribute, called customerOID, into the
session scope. Which two code snippets allow you to insert this value into the
session? (Choose two.)
Answer: A, C
Page 17
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Q26. Your web application uses a simple architecture in which servlets handle
requests and then forward to a JSP using a request dispatcher. You need to pass
information calculated in the servlet to the JSP for view generation. This
information must NOT be accessible to any other servlet, JSP or session in the
webapp. Which two techniques can you use to accomplish this goal?
(Choose two.)
A. Add attributes to the session object. B. Add attributes on the request object.
C. Add parameters to the request object.
D. Use the pageContext object to add request attributes.
E. Add parameters to the JSP's URL when generating the request dispatcher.
Answer: B, E
Q 27. Which three are true about servlet filters? (Choose three.)
A. A filter must implement the destroy method.
B. A filter must implement the doFilter method.
C. A servlet may have multiple filters associated with it.
D. A servlet that is to have a filter applied to it must implement the javax.servlet.
FilterChain interface.
E. A filter that is part of a filter chain passes control to the next filter in the chain by
invoking the FilterChain.forward method.
F. For each <filter> element in the web application deployment descriptor, multiple
instances of a filter may be created by the web container.
Answer: A, B,
C
Page 18
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Answers:
<listener>
<listener-class>com.example.MyListener</listener-class>
</listener>
Q 29. Which is true about the web container request processing model?
A. The init method on a filter is called the first time a servlet mapped to that filter is
invoked.
B. A filter defined for a servlet must always forward control to the next resource in the
filter chain.
C. Filters associated with a named servlet are applied in the order they appear in the web
application deployment descriptor file.
D. If the init method on a filter throws an UnavailableException, then the container will
make no further attempt to execute it.
Answer: C
Page 19
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
The return value of this method indicates a symbolic name of the next view. From
this name, the Front Controller servlet looks up the JSP URL in a configuration
table. This URL might be an absolute path or a path relative to the current request.
Next, the Front Controller servlet must send the request to this JSP to generate the
view. Assume that the servlet variable request is assigned the current
HttpServletRequest object and the variable context is assigned the webapp's
ServletContext.
Which code snippet of the Front Controller servlet accomplishes this goal?
A. Dispatcher view
= context.getDispatcher(viewURL);
view.forwardRequest(request, response);
B. Dispatcher view
= request.getDispatcher(viewURL);
view.forwardRequest(request, response);
C. RequestDispatcher view
= context.getRequestDispatcher(viewURL);
view.forward(request, response);
Page 20
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Q31. Given that a web application consists of two HttpServlet classes, ServletA and
ServletB, and the ServletA.service method:
20. String key = "com.example.data";
21. session.setAttribute(key, "Hello");
22. Object value = session.getAttribute(key);
23.
Assume session is an HttpSession, and is not referenced anywhere else in ServletA.
Which two changes, taken together, ensure that value is equal to "Hello" on line 23?
(Choose two.)
A. ensure that the ServletB.service method is synchronized
B. ensure that the ServletA.service method is synchronized
C. ensure that ServletB synchronizes on the session object when setting session attributes
D. enclose lines 21-22 in a synchronized block:
synchronized(this) {
session.setAttribute(key, "Hello");
value = session.getAttribute(key);
}
E. enclose lines 21-22 in a synchronized block:
synchronized(session) {
session.setAttribute(key, "Hello");
value = session.getAttribute(key); }
Answer: C,
E
Page 21
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com
ADV.JAVA Means DURGA SIR
Page 22
Plot No : 202, IInd Floor ,HUDA Maitrivanam,Ameerpet, Hyd-5000038.
www.durgasoft.com