Q.1 Which of the following JSP tags can be used to include the output of another JSP page into the output of the current page at request time? (Select one)
a. <jsp:insert>
b. <jsp:include>
c. <jsp:directive.include>
d. <jsp:directive:include>
e. <%@ include %>
b. <jsp:include>
c. <jsp:directive.include>
d. <jsp:directive:include>
e. <%@ include %>
Answer: b Explanation
The tags in answers a and d are not valid JSP tags. Answers c and e are valid tags in XML syntax and JSP syntax, respectively, but they are directives and include other JSP pages or HTML/XML files at translation time. Answer b is the right answer because it includes the output of another component, JSP page, or Servlet at request time.
The tags in answers a and d are not valid JSP tags. Answers c and e are valid tags in XML syntax and JSP syntax, respectively, but they are directives and include other JSP pages or HTML/XML files at translation time. Answer b is the right answer because it includes the output of another component, JSP page, or Servlet at request time.
Q.2 Consider the contents of the following two JSP files: File 1: test1.jsp
<html><body>
<% String message = "Hello"; %>
//1 Insert LOC here.
The message is <%= message %>
</body></html>
<% String message = "Hello"; %>
//1 Insert LOC here.
The message is <%= message %>
</body></html>
File 2: test2.jsp
<% message = message + " world!"; %>
<% message = message + " world!"; %>
Which of the following lines can be inserted at //1 in test1.jsp so that it prints "The message is Hello world!" when requested? (Select one)
a <%@ include page="test2.jsp" %>
b <%@ include file="test2.jsp" />
c <jsp:include page="test2.jsp" />
d <jsp:include file="test2.jsp" />
b <%@ include file="test2.jsp" />
c <jsp:include page="test2.jsp" />
d <jsp:include file="test2.jsp" />
Answer: b Explanation
Since the test2.jsp file does not declare or define the variable message, it cannot compile on its own. This rules out dynamic inclusion using the <jsp:include> action. The file test1.jsp could print "Hello world" if it statically included test2.jsp. This could be done using the include directive: <%@ include %>. For the include directive, the valid attribute is file and not page, so answer b is correct.
Since the test2.jsp file does not declare or define the variable message, it cannot compile on its own. This rules out dynamic inclusion using the <jsp:include> action. The file test1.jsp could print "Hello world" if it statically included test2.jsp. This could be done using the include directive: <%@ include %>. For the include directive, the valid attribute is file and not page, so answer b is correct.
Q.3 Which of the following is a correct way to pass a parameter equivalent to the query string user=mary at request time to an included component? (Select one)
a. <jsp:include page="other.jsp" >
<jsp:param paramName="user" paramValue="mary" />
</jsp:include>
<jsp:param paramName="user" paramValue="mary" />
</jsp:include>
b. <jsp:include page="other.jsp" >
<jsp:param name="mary" value="user" />
</jsp:include>
<jsp:param name="mary" value="user" />
</jsp:include>
c. <jsp:include page="other.jsp" >
<jsp:param value="mary" name="user" />
</jsp:include>
<jsp:param value="mary" name="user" />
</jsp:include>
d. <jsp:include page="other.jsp" >
<jsp:param param="user" value="mary"/>
</jsp:include>
<jsp:param param="user" value="mary"/>
</jsp:include>
e. <jsp:include page="other.jsp" >
<jsp:param user="mary" />
</jsp:include>
<jsp:param user="mary" />
</jsp:include>
Answer: c Explanation
The only valid attributes that a <jsp:param> tag can have are name and value. This rules out answers a, d, and e. Answer b, <jsp:param name="mary" value="user" />, is equivalent to the query string mary=user. In the included component, a call to request.getParameter("mary"); will return "user". Answer c, <jsp:param value="mary" name="user" />, is equivalent to the query string user=mary. In the included component, a call to request.getParameter("user"); will return "mary". Therefore, answer c is the correct answer.
The only valid attributes that a <jsp:param> tag can have are name and value. This rules out answers a, d, and e. Answer b, <jsp:param name="mary" value="user" />, is equivalent to the query string mary=user. In the included component, a call to request.getParameter("mary"); will return "user". Answer c, <jsp:param value="mary" name="user" />, is equivalent to the query string user=mary. In the included component, a call to request.getParameter("user"); will return "mary". Therefore, answer c is the correct answer.
Q.4 Identify the JSP equivalent of the following code written in a servlet. (Select one)
RequestDispatcher rd = request.getRequestDispatcher("world.jsp");
rd.forward(request, response);
rd.forward(request, response);
a. <jsp:forward page="world.jsp"/>
b. <jsp:action.forward page="world.jsp"/>
c. <jsp:directive.forward page="world.jsp"/>
d. <%@ forward file="world.jsp"%>
e. <%@ forward page="world.jsp"%>
b. <jsp:action.forward page="world.jsp"/>
c. <jsp:directive.forward page="world.jsp"/>
d. <%@ forward file="world.jsp"%>
e. <%@ forward page="world.jsp"%>
Answer: a Explanation
The action tags in answers b through e are all invalid JSP tags. Answer a, <jsp:forward page="relativeURL" />, is the only valid way to write a forward action.
The action tags in answers b through e are all invalid JSP tags. Answer a, <jsp:forward page="relativeURL" />, is the only valid way to write a forward action.
Q.5 Consider the contents of two JSP files:
File 1: test1.jsp
<html><body>
<% pageContext.setAttribute("ninetyNine", new Integer(99)); %>
//1
</body></html>
File 1: test1.jsp
<html><body>
<% pageContext.setAttribute("ninetyNine", new Integer(99)); %>
//1
</body></html>
File 2: test2.jsp
The number is <%= pageContext.getAttribute("ninetyNine") %>
The number is <%= pageContext.getAttribute("ninetyNine") %>
Which of the following, when placed at line //1 in the test1.jsp file, will allow the test2.jsp file to print the value of the attribute when test1.jsp is requested? (Select one)
a. <jsp:include page="test2.jsp" />
b. <jsp:forward page="test2.jsp" />
c. <%@ include file="test2.jsp" %>
d. None of the above because objects placed in pageContext have the page scope and cannot be shared with other components.
b. <jsp:forward page="test2.jsp" />
c. <%@ include file="test2.jsp" %>
d. None of the above because objects placed in pageContext have the page scope and cannot be shared with other components.
Answer: c Explanation
Objects placed in pageContext have the page scope and are accessible within a single translation unit. Since files included statically using the include directive become an integral part of the same translation unit as the including file, they can share objects in the page scope via the PageContext container. So the correct answer is c: <%@ include file="test2.jsp" %>.
Objects placed in pageContext have the page scope and are accessible within a single translation unit. Since files included statically using the include directive become an integral part of the same translation unit as the including file, they can share objects in the page scope via the PageContext container. So the correct answer is c: <%@ include file="test2.jsp" %>.
Q.6 Consider the contents of two JSP files:
File 1: this.jsp
<html><body><pre>
<jsp:include page="that.jsp" >
<jsp:param name="color" value="red" />
<jsp:param name="color" value="green" />
</jsp:include>
</pre></body></html>
File 1: this.jsp
<html><body><pre>
<jsp:include page="that.jsp" >
<jsp:param name="color" value="red" />
<jsp:param name="color" value="green" />
</jsp:include>
</pre></body></html>
File 2: that.jsp
<%
String colors[] = request.getParameterValues("color");
for (int i=0; i<colors.length; i++) {
out.print(colors[i] + " ");
}
%>
<%
String colors[] = request.getParameterValues("color");
for (int i=0; i<colors.length; i++) {
out.print(colors[i] + " ");
}
%>
What will be the output of accessing the this.jsp file via the following URL? (Select one)
http://localhost:8080/chapter12/this.jsp?color=blue
http://localhost:8080/chapter12/this.jsp?color=blue
a. blue
b. red green
c. red green blue
d. blue red green
e. blue green red
b. red green
c. red green blue
d. blue red green
e. blue green red
Answer: c Explanation
The parameters passed via the <jsp:param> tag to an included component tag take precedence over the parameters already present in the request object of the including component. Also, the order of values passed via the <jsp:param> tag is the same as the order in which the tags appear. Thus, the correct answer is c. The output will be red green blue.
The parameters passed via the <jsp:param> tag to an included component tag take precedence over the parameters already present in the request object of the including component. Also, the order of values passed via the <jsp:param> tag is the same as the order in which the tags appear. Thus, the correct answer is c. The output will be red green blue.
Q.7 Consider the contents of two JSP files:
File 1: this.jsp
<html><body>
<%= request.getParameter("color") %>
<jsp:include page="that.jsp" >
<jsp:param name="color" value="red" />
</jsp:include><%= request.getParameter("color") %>
</body></html>
File 1: this.jsp
<html><body>
<%= request.getParameter("color") %>
<jsp:include page="that.jsp" >
<jsp:param name="color" value="red" />
</jsp:include><%= request.getParameter("color") %>
</body></html>
File 2: that.jsp
<%= request.getParameter("color") %>
<%= request.getParameter("color") %>
What will be the output of accessing the this.jsp file via the following URL? (Select one)
http://localhost:8080/chapter12/this.jsp?color=blue
http://localhost:8080/chapter12/this.jsp?color=blue
a. blue red blue
b. blue red red
c. blue blue red
d. blue red null
b. blue red red
c. blue blue red
d. blue red null
Answer: a Explanation
The first call to request.getParameter("color") in the this.jsp file returns blue. This file then includes the that.jsp file and passes a value of red for the color attribute. Since the values passed via <jsp:param> take precedence over the original values, a call to request.getParameter("color") in that.jsp returns red. However, this new value exists and is available only within the included component—that is, that.jsp. So, after the that.jsp page finishes processing, a call to request.getParameter("color") in the this.jsp file again returns blue. Thus, the correct answer is a. The output will be blue red blue.
The first call to request.getParameter("color") in the this.jsp file returns blue. This file then includes the that.jsp file and passes a value of red for the color attribute. Since the values passed via <jsp:param> take precedence over the original values, a call to request.getParameter("color") in that.jsp returns red. However, this new value exists and is available only within the included component—that is, that.jsp. So, after the that.jsp page finishes processing, a call to request.getParameter("color") in the this.jsp file again returns blue. Thus, the correct answer is a. The output will be blue red blue.
Q.8 Consider the contents of three JSP files:
File 1: one.jsp
<html><body><pre>
<jsp:include page="two.jsp" >
<jsp:param name="color" value="red" />
</jsp:include>
</pre></body></html>
File 1: one.jsp
<html><body><pre>
<jsp:include page="two.jsp" >
<jsp:param name="color" value="red" />
</jsp:include>
</pre></body></html>
File 2: two.jsp
<jsp:include page="three.jsp" >
<jsp:param name="color" value="green" />
</jsp:include>
<jsp:include page="three.jsp" >
<jsp:param name="color" value="green" />
</jsp:include>
File 3: three.jsp
<%= request.getParameter("color") %>
<%= request.getParameter("color") %>
What will be the output of accessing the one.jsp file via the following URL? (Select one)
http://localhost:8080/chapter12/one.jsp?color=blue
http://localhost:8080/chapter12/one.jsp?color=blue
a. red
b. green
c. blue
d. The answer cannot be determined.
b. green
c. blue
d. The answer cannot be determined.
Answer: b Explanation
The output is generated by the three.jsp file. Since the two.jsp file calls three.jsp and provides a value of green, this value takes precedence over all the previous values passed to one.jsp and two.jsp. Thus, the correct answer is b. The output will be green.
The output is generated by the three.jsp file. Since the two.jsp file calls three.jsp and provides a value of green, this value takes precedence over all the previous values passed to one.jsp and two.jsp. Thus, the correct answer is b. The output will be green.
No comments:
Post a Comment