Questions and Answers
Q1) How would servlet code from a service method (e.g., doPost() ) retrieve the
value of the “User-Agent” header from the request?
Ans: String userAgent = request.getHeader(“User-Agent”);
Q2) Which HTTP methods are used to show the client what the server is receiving?
Ans: TRACE ( This method is typically used for troubleshooting, not for production)
Q3) Which method of HttpServletResponse is used to redirect an HTTP
request to another URL?
Ans: sendRedirect()
Q4) Which HTTP method are NOT considered idempotent?
Ans: POST
Q5) Given req is a HttpServletRequest, which gets a binary input stream?
Ans: ServletInputStream inputStream = req.getInputStream()
Q6) How would you set a header named “CONTENT-LENGTH” in the
HttpServletResponse object?
Ans: response.setHeader(“CONTENT-LENGTH”,”1024”);
Q7) Write servlet code fragment that gets a binary stream for writing an image or other binary type to the HttpServletResponse.
Ans: ServletOutputStream so = response.getOutputStream();
Q8) Which methods are used by a servlet to handle form data from a client?
Ans: HttpServlet.doPost()
Q9) Which of the following methods are declared in HttpServletRequest as opposed to in ServletRequest?
Ans: getMethod(), getHeader(), getCookies()
Q10) How should servlet developers handle the HttpServlet’s service() method when extending HttpServlet?
Ans: They should override at least one doXXX() method (such as doPost()).
Q11) When using a RequestDispatcher, the use of which methods can often lead to an IllegalStateException?
Ans: flush, write
Q12) Which statements about ServletContext initialization parameters are true?
Ans: 1) They should be used for data that changes rarely.
2) They can be accessed using ServletContext.getInitParameter(String).
3) They should be used for data that is applicable to an entire web application.
Q13) Which types define the methods getAttribute() and setAttribute()?
Ans: HttpSession, ServletRequest, ServletContext
Q14) If a servlet is invoked using the forward or include method of RequestDispatcher, which methods of the servlet’s request object can access the request attributes set by the container?
Ans: getAttribute
Q15) Which calls provide information about initialization parameters that are applicable to an entire web application?
Ans: ServletContext.getInitParameterNames(), ServletContext.getInitParameter(String)
Q16) What is true about listeners?
Ans: 1) An HttpSessionListener can be used to perform an action when an HttpSession has timed out.
2) A ServletContextListener can be used to perform an action when the servlet context is about to be shut down.
3) A ServletRequestAttributeListener can be used to perform an action when an attribute has been removed from a ServletRequest.
Q17) Which is most logically stored as an attribute in session scope?
Ans: An object representing a user who has just logged into the system.
Q18) When declaring a listener in the DD, which sub-elements of the <listener> element are required?
Ans: <listener-class>
Q19) Which types of objects can store attributes?
Ans: HttpServletRequest
Q20) How container registers listeners ?
Ans: The container registers listeners based on declarations in the deployment descriptor.
Q21) Which statements about RequestDispatcher are true (where applicable, assume the RequestDispatcher was not obtained via a call to getNamedDispatcher())?
Ans: 1) A RequestDispatcher can be used to forward a request to another servlet.
2) The servlet to which a request is forwarded may access the original query string by calling getAttribute(“javax.servlet.forward.query_string”) on the
ServletRequest.
Q22) Which are true about the RequestDispatcher interface?
Ans: 1) Of its two methods, forward() is used most frequently.
2) Depending on the class whose method creates a RequestDispatcher, the path to the resource to be forwarded to will change.
Q23) If a client will NOT accept a cookie, which session management mechanism could the web container employ?
Ans: URL rewriting
Q24) Which statements about HttpSession objects are true?
Ans: A session whose timeout period has been set to -1 will never expire. A session will become invalid after a timeout period
defined by the servlet container.
Q25) Which statements about session tracking are true?
Ans: 1) URL rewriting may be used by a server as the basis for session tracking.
2) SSL has a built-in mechanism that a servlet container could use to obtain data used to define a session.
3) When using cookies for session tracking, the name of the session tracking cookie must be JSESSIONID.
Q26) Which statements about session attributes are true?
Ans: 1) The return type of HttpSession.getAttribute(String) is Object.
2) Attributes bound into a session are available to any other servlet that belongs to the same ServletContext and handles a request
identified as being part of the same session.
3) Calling setAttribute(“keyA”, “valueB”) on an HttpSession which already holds a value for the key keyA will cause the previous
value for this attribute to be replaced with the String valueB.
Q27) Which interfaces define a getSession() method?
Ans: HttpServletRequest
Q28) Given a session object s, and the code: s.setAttribute(“key”, value);
Which listeners could be notified?
Ans: HttpSessionBindingListener and HttpSessionAttributeListener
Q29) Given that req is an HttpServletRequest, which snippets create a session if one doesn’t exist?
Ans: req.getSession(); and req.getSession(true);
Q30) Given a session object s with two attributes named myAttr1 and myAttr2, which will remove both attributes from this session?
Ans: s.removeAttribute(“myAttr1”);
s.removeAttribute(“myAttr2”);
Q31) What is true about HttpSession objects in distributed environments ?
Ans: When a session is moved from one JVM to another, any session attribute implmenting the HttpSessionActivationListener
interface will be notified. When a session is moved from one JVM to another, attribute values that implement java.io.Serializable will be transferred to the new JVM.
Q32) What is true about session timeouts?
Ans: Session timeout declarations made in the DD can specify time in minutes. Session timeout declarations made programmatically can specify time only in seconds.
Q33) Which method(s) can be used to ask the container to notify your application whenever a session is about to timeout?
Ans: HttpSessionBindingListener.valueBound and HttpSessionListener.sessionDestroyed
Q34) Given this DD element:
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
What does the element accomplish?
Ans: By default, all files with the specified extension mapping should NOT have any Expression Language code evaluated by the JSP container.
Q35) Which directives specify an HTTP response that will be of type “image/svg”?
Ans: <%@ page contentType=”image/svg” %>
Q36) What is true about jspInit()?
Ans: It has access to a ServletConfig. It has access to a ServletContext. It is only called once. It can be overridden.
Q37) Which types of objects are available to the jspInit() method?
Ans: ServletConfig and ServletContext
Q38) Given: <%@ page isELIgnored=”true” %> What is the effect?
Ans: The JSP containing this directive should NOT have any Expression Language code evaluated by the JSP container.
Q39) Which statement concerning JSPs is true?
Ans: Both jspInit() and jspDestroy() can be overridden.
Q40) JSP Lifecycle Steps
Ans: Translate the JSP into a servlet > Compile servlet source code > Instantiate the servlet class > Call jspInit() > Call _jspService() > Call jspDestroy()
Q41) Write two valid JSP implicit variables
Ans: exception and application
Q42) Given a request with two parameters: one named “first” represents a user’s
first name and another named “last” represents his last name.
Write JSP scriptlet code outputs these parameter values
Ans: <% out.println(request.getParameter(“first”));
out.println(request.getParameter(“last”)); %>
Q43) Write JSP expression tag will print the context initialization parameter named “javax.sql.DataSource”?
Ans: <%= application.getInitParameter(“javax.sql.DataSource”) %>
Q44) Which statements is about disabling scripting elements are true?
Ans: You can disable scripting via the DD by using the <scripting-invalid> element.
Q45) Which is an example of the syntax used to import a class in a JSP ?
Ans: <%@ page import=”java.util.Date” %>
Q46) Given an HTML form that uses checkboxes to allow a user to select multiple values for a parameter called hobbies.
Which EL expressions evaluate to the first value of the hobbies parameter?
Ans: ${param.hobbies} and ${paramValues.hobbies[0]}
Q47) Given that a web application stores the webmaster email address in the
servlet context initialization parameter called master-email. Which retrieves that value?
Ans: <a href=’mailto:${initParam[‘master-email’]}’> email me</a>
Q48) What is true about the . (dot) and [] EL operators?
Ans: ${foo.bar} is equivalent to ${foo[“bar”]}, ${foo[“5”]} is valid syntax if foo is a Map, ${foo[5]} is valid syntax if foo is a List or an array
Q49) Given a JSP page with the line:
${101 % 10}
What will be dispyed ?
Ans: 1
Q50) Show valid usage of EL implicit variables
Ans:${initParam.foo},${requestScope.foo},${header[“User-Agent”]}, ${pageContext.request.requestURI}
Q51) Which are true about the <jsp:useBean> standard action?
Ans: Either the class or type attributes may be specified,
but at least one. It is valid to include both the class attribute and the type
attribute, even if their values are NOT the same.
Q51) Which are true about the <jsp:useBean> standard action?
Ans: Either the class or type attributes may be specified,
but at least one. It is valid to include both the class attribute and the type
attribute, even if their values are NOT the same.
Q52) How would you include dynamic content in a JSP, similar to a
server-side include (SSI)?
Ans: <jsp:include page=”/segments/footer.jspf” />
Q53) What is true about EL access operators ?
Ans: Anywhere the . dot operator is used the [] operator can be use instead
Q54) What is true about TLD files?
Ans: TLD files may be placed in any subdirectory of WEB-INF.
Q55) Assuming the standard JSTL prefix conventions are used,
which JSTL tags would you use to iterate over a collection of objects?
Q53) What is true about EL access operators ?
Ans: Anywhere the . dot operator is used the [] operator can be use instead
Q54) What is true about TLD files?
Ans: TLD files may be placed in any subdirectory of WEB-INF.
Q55) Assuming the standard JSTL prefix conventions are used,
which JSTL tags would you use to iterate over a collection of objects?
Ans: <c:forEach>
Q56) A JSP page contains a taglib directive whose uri attribute has the
value myTags. Which deployment descriptor element defines the
associated TLD?
Ans:
<taglib>
<taglib-uri>myTags</taglib-uri>
valid tag elements.
<taglib-location>/WEB-INF/myTags.tld</taglib-location>
</taglib>
Q57) How can a Classic tag handler instruct the container to ignore the remainder of
the JSP that invoked the tag?
Ans: The doEndTag() method should return Tag.SKIP_PAGE
Q58) Which directives and/or standard actions are applicable ONLY
within tag files?
Ans: tag, jsp:doBody, jsp:invoke
Q59) What Simple tag mechanism will tell a JSP page to stop processing?
Ans: Throw a SkipPageException from the doTag method.
Q60) What is true about the Classic tag model?
Ans:
The SKIP_PAGE constant is a valid return value of the doEndTag method.
There are three tag interfaces—Tag, IterationTag, and BodyTag—but only two built-in base classes:
TagSupport, and BodyTagSupport.
Q56) A JSP page contains a taglib directive whose uri attribute has the
value myTags. Which deployment descriptor element defines the
associated TLD?
Ans:
<taglib>
<taglib-uri>myTags</taglib-uri>
valid tag elements.
<taglib-location>/WEB-INF/myTags.tld</taglib-location>
</taglib>
Q57) How can a Classic tag handler instruct the container to ignore the remainder of
the JSP that invoked the tag?
Ans: The doEndTag() method should return Tag.SKIP_PAGE
Q58) Which directives and/or standard actions are applicable ONLY
within tag files?
Ans: tag, jsp:doBody, jsp:invoke
Q59) What Simple tag mechanism will tell a JSP page to stop processing?
Ans: Throw a SkipPageException from the doTag method.
Q60) What is true about the Classic tag model?
Ans:
The SKIP_PAGE constant is a valid return value of the doEndTag method.
There are three tag interfaces—Tag, IterationTag, and BodyTag—but only two built-in base classes:
TagSupport, and BodyTagSupport.
Q61) Which are true about the findAncestorWithClass method in
the TagSupport class?
Ans:
It is a static method in the TagSupport class.
It is NOT defined by any of the standard JSP tag interfaces.
It requires two parameters: A Tag and a Class.
Q62) Which must be true if you want to use dynamic attributes for
a Simple tag handler?
Ans:
Your Simple tag must use the <dynamic-attributes> element in the TLD.
Your Simple tag handler must implement the DynamicAttributes interface.
Q63) What is true about tag files?
Ans: A tag file must have the file extension of .tag or .tagx
Q64) What about doAfterBody() is true?
Ans: Assuming no exceptions occur, doAfterBody() is called after doStartTag() for any tag that implements IterationTag and returns EVAL_BODY_INCLUDE from doStartTag().
It is a static method in the TagSupport class.
It is NOT defined by any of the standard JSP tag interfaces.
It requires two parameters: A Tag and a Class.
Q62) Which must be true if you want to use dynamic attributes for
a Simple tag handler?
Ans:
Your Simple tag must use the <dynamic-attributes> element in the TLD.
Your Simple tag handler must implement the DynamicAttributes interface.
Q63) What is true about tag files?
Ans: A tag file must have the file extension of .tag or .tagx
Q64) What about doAfterBody() is true?
Ans: Assuming no exceptions occur, doAfterBody() is called after doStartTag() for any tag that implements IterationTag and returns EVAL_BODY_INCLUDE from doStartTag().