You are on page 1of 4

You want the copyright information contained in <webapp root>/html/copyright.

html to be added
to the html content that your servlet generates. Which of the following code fragments occuring
in doGet method would achieve this objective?

Select 1 correct option.


a this.getServletContext().getRequestDispatcher("../html/copyright.html").include(request,
response);
This is invalid because a relative path is being passed to getRequestDispatcher method
of ServletContext.
b this.getServletContext().getRequestDispatcher("/html/copyright.html").include(request,
response);
This is valid because an absolute path is being passed to getRequestDispatcher method
of ServletContext. (/html/copyright.html starts with a "/").
c request.getRequestDispatcher("../html/copyright.html").include(request, response);
This is valid because getRequestDispatcher() of ServletRequest interface supports
absolute as well as relative paths.
d request.include("../html/copyright.html", request, response);
There is no include method in request class.
e this.getServletContext().getRequestDispatcher("/html/copyright.html").forward(request,
response);
calling forward will forward the request to the given resource and so the data generated
by this servlet will be lost.
f request.getRequestDispatcher("/html/copyright.html").forward(request, response);
calling forward will forward the request to the given resource and so the data generated
by this servlet will be lost.

General Comments1. This is invalid because a relative path is being passed to


getRequestDispatcher method of ServletContext.
2. This is valid because an absolute path is being passed to getRequestDispatcher method of
ServletContext. (/html/copyright.html starts with a "/").
3. This is valid because getRequestDispatcher() of ServletRequest interface supports absolute as
well as relative paths.
4. There is no include method in request.
5 and 6. calling forward will forward the request to the given resource and so the data generated
by this servlet will be lost.

<html>
<head>
<title> Test Page </title>
<body>
Today's Date is
</body>
</html>

Options:
<%@import package='java.util.*' %> <%@page import='java.util.*' %>
<%=new Date()%> <%new Date()%>
Correct Answer(s)
<%@import package='java.util.*' %>
<html>
<head>
<title> Test Page </title>
<body>
Today's Date is <%=new Date()%>
</body>
</html>

Question 9 of 20 (Answered Wrong)


Select 1 correct option.
a ${request.contextPath}
'request' is not an implicit object so you can use anything starting with request.

b ${pageContext.contextPath}
getContextPath() is a method in HttpServletRequest and not in pageContext.

c ${requestScope.contextPath}
requestScope is an implicit object but it contains the attributes. It is not same as
ServletRequest object.

d ${pageContext.request.contextPath}
pageContext is an implicit object. It does contain 'request' object of class
HttpServletRequest. getContextPath() can be called on this object.

e ${pageContext.requestScope.contextPath}
pageContext.requestScope does not make sense.
f ${requestScope.request.contextPath}
requestScope does not contain request object.

g ${contextPath}
contextPath is not an implicit object.

Which of the following directives are applicable only to tag files?


Select 3 correct options
a attribute
b variable
Valid only for tag files.
c page
d include
e import
No such directive.
General CommentsOnly the following directives are valid for tag files:
taglib, include, tag, attribute, variable.

Select the correct sequence of actions that a servlet container performs before servicing
any request.
1. Instantiate listeners defined in the deployment descriptor.
2. Initialize filters defined in the deployment descriptor.
3. Initialize servlets that are set to load on startup.
4. Call the contextInitialized method on the listeners implementing
ServletContextListener interface.

Correct Answer(s)

1423

General CommentsAs Per SRV 9.12


When a web application is deployed into a container, the following steps must be
performed, in this order, before the web application begins processing client requests.
1. Instantiate an instance of each event listener identified by a <listener> element in the
deployment descriptor.
2. For instantiated listener instances that implement ServletContextListener, call the
contextInitialized() method.
3. Instantiate an instance of each filter identified by a <filter> element in the deployment
descriptor and call each filter instance’s init() method.
4. Instantiate an instance of each servlet identified by a <servlet> element that includes a
<load-on-startup> element in the order defined by the load-onstartup element values, and
call each servlet instance’s init() method.

Question 17 of 20 (Answered Right)

In file companyhome.jsp:
<html><body>
Welcome to ABC Corp!
<%@ page errorPage="simpleerrorhandler.jsp" %>
<%@ include file="companynews.jsp" %>
</body></html>
In file companynews.jsp:
<%@ page errorPage="advancederrorhandler.jsp" %>
<h3>Todays News</h3>
Which of the following statements are correct?
Select 1 correct option.
a When companyhome.jsp is requested, the output will contain "welcome..." as
well as "Todays News".
b companyhome.jsp will not compile.

c companynews.jsp will not compile.

d Both the files will compile but will throw an exception at runtime.

e None of these.

General CommentsFact 1: In case of an include directive ( <%@ include file=...%> or


<jsp:directive.include> ), the contents of the included file are placed as it is in to the
including file.
Fact 2: Any page directive attribute except import can occur at most once in a
translation unit.
Now, in this question, both the including and the included files have an errorPage
directive. Thus, the resulting including file (companyhome.jsp) will contain 2 errorPage
directives, which is wrong.

You might also like