ItemSet: Servlets and JSP
This test covers basic things that are defined in Servlet 2.4 (and JSP 2.0) specifications and used by Java Web applications
Skills
- Tag Libraries
Understand how to declare and use tag libraries.
- Servlet Lifecycle
Understand servlet creation, initialization and destruction
- Issues of Servlet Programming
Serving requests, receiving HTTP parameters, handling responses, logging, etc.
- Issues of JSP Programming
Directives, expressions, scriptlets; "jsp:useBean" and related constructs.
- Session State, Scopes
Various scopes, sessions and HTTP mechanisms related to HttpSession, synchronization issues.
- Servlet Deployment
Understand how servlet container interacts with servlets and JSPs depending on the descriptor web.xml
1. Tag Libraries
- ServletSpec_1001: Read the following code snippet.
<taglib>
<taglib-uri>
http://www.yourcompany.com/yourTagLibrary
</taglib-uri>
<taglib-location>
/WEB-INF/yourTagLibrary.tld
</taglib-location>
</taglib> What statement applies to this code snippet?
- The tablib element of the deployment descriptor is required if you use a taglib directive in a JSP page that references a TLD by name (indirectly) rather than by location (directly).
- This tag goes into the WEB-INF/myApplication/web.xml file.
- This definition is incorrect because the taglib-uri should have been uri.
- This definition is incorrect because the taglib should have been tag.
2. Servlet Lifecycle
- ServletSpec_2001: How would you retrieve servlet-initialization parameters? (By "paramName" we denote the name of a parameter; by "paramIndex" - its number in the order of declarations).
- String value = getServletConfig().getInitParameter("paramName");
- String value = getServletConfig().getInitParameter(paramIndex);
- String value = getServletConfig().getParameter("paramName");
- String value = getServletConfig().getParameter(paramIndex);
- ServletSpec_2002: How do you declare initialization parameters for the servlet in the deployment descriptor?
<web-app>
<servlet>
<servletname>servletName</servletname>
<servletclass>com.myCo.MyServlet</servletclass>
<initparam>
<paramname>color</paramname>
<paramvalue>blue</paramvalue>
</initparam>
</servlet>
</web-app>
<web-app>
<servlet>
<servlet-name>servletName</servlet-name>
<servlet-class>com.myCo.MyServlet</servlet-class>
<init-param>
<name>color</name>
<value>blue</value>
</init-param>
</servlet>
</web-app>
<web-app>
<servlet>
<name>servletName</name>
<class>com.myCo.MyServlet</class>
<init-param>
<param-name>color</param-name>
<param-value>blue</param-value>
</init-param>
</servlet>
</web-app>
<web-app>
<servlet>
<servlet-name>servletName</servlet-name>
<servlet-class>com.myCo.MyServlet</servlet-class>
<init-param>
<param-name>color</param-name>
<param-value>blue</param-value>
</init-param>
</servlet>
</web-app>
- ServletSpec_2003: What method is called by the servlet container just before this servlet is put into service?
- public void init() throws IOException { //code }
- public void start() throws IOException { //code }
- public void service() throws ServletException { //code }
- public void init() throws ServletException { //code }
- ServletSpec_2004: What method is called by the servlet container just after the servlet is removed from service?
- public void destroy() { // code… }
- public void destroy() throws ServletException { // code… }
- public void finalize() throws ServletException { // code… }
- public void finalize() { // code… }
3. Issues of Servlet Programming
- ServletSpec_3001: What method is called by the servlet container to process a GET request (according to Servlet 2.4 specification)?
public String doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
//code
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
//code
}
public String doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException {
//code
}
public void doGet(ServletRequest request,
ServletResponse response) throws ServletException {
//code
}
- ServletSpec_3002: How would you add an entry to the log file from a servlet assuming that e is an initialized Exception object?
- getServletConfig().log("An exception occurred", e);
- getServletConfig().logger(e);
- getServletContext().log(e);
- getServletContext().log("An exception occurred", e);
- ServletSpec_3003: Assuming that the servlet method for handling HTTP GET requests is doGet(HttpServletRequest request, HttpServletResponse response), how do you get a request parameter in that servlet?
- String value = request.getInitParameter("name");
- String value = response.getParameter("name");
- String value = request.getParameter("name");
- String value = request.getInitParameter(10);
- ServletSpec_3004: Assuming that the servlet method for handling HTTP GET requests is doGet(HttpServletRequest request, HttpServletResponse response), which two statements are true regarding the following code snippet?
Enumeration enum = request.getParameterNames();
Select 2 correct answers!
- This is how you retrieve the names of all request parameters.
- This is how you would then get the name of the next request parameter:
name = (String)enum.nextElement();
- The code snippet in the question will not work. The return type is a String array, not an Enumeration object.
- This would have worked had the code been:
String[] names = request.getParameterNames();
- ServletSpec_3005: Which one of the following is the most likely trigger to cause a browser to use the HTTP HEAD method?
- Client sends updated information to server.
- Server sends HTTP HEAD to client.
- Server requests HTTP HEAD of client.
- Client checks resource modification date/time.
- ServletSpec_3006: What interface and method name should be used to acquire a binary stream for the response?
- ServletResponse.getOutputStream
- ServletResponse.getStream
- ServletResponse.getWriterStream
- ServletResponse.getWriter
4. Issues of JSP Programming
- ServletSpec_4001: How do you perform a server-side include whose content gets translated within the JSP page?
- include action
- include directive
- include scriptlet
- include expression
- ServletSpec_4002: How would you include dynamic content in a JSP page that doesn't get translated within the context of the current page?
- <%@ page import="footer.html" %>
- include directive
- <%@include file="footer.html" %>
- jsp:include
- ServletSpec_4003: Which two of the following are valid attributes of the JSP page directive?
Select 2 correct answers!
- extends="package.class"
- errorPage="true"
- buffer="false"
- import="package.class"
- ServletSpec_4004: What is an example of an expression?
- <%= count %>
- <% String count=4; %>
- <% out.print("hello"); %>
- <%! String count=4; %>
- ServletSpec_4005: Which one of the following is not an attribute of the jsp:useBean tag?
- scope
- name
- type
- class
- ServletSpec_4006: Which two of the following pairs are equivalents?
Select 2 correct answers!
- and
<jsp:scriptlet>yourCode</jsp:scriptlet>
ServletContext.useBean("beanName",id,scope, class); and <jsp:useBean id="customer"
scope="page" class="session.Customer" />
<%@ taglib uri="URIForLibrary" prefix="tagPrefix" %>
and <jsp:taglib uri="URIForLibrary" prefix="tagPrefix" />
- and
<jsp:directive.page att="val"/>
- ServletSpec_4007: How can you set several attributes of a Bean at the same time?
- You can't. They must be done individually.
- You do this with the deployment descriptor.
- You use the jsp:setProperty action with the property attribute set to the wildcard *.
- You use the jsp:useBean and list the attributes in this tag together.
- ServletSpec_4008: Read the following listing.
1. <html>
2. <body>
3.
4. <% x = x - 15; %>
5. New customers: <%= x %>
6. </body>
7. </html>
What should be inserted on line 3 to allow successful compilation and produce an output of New customers: 20?
- ServletSpec_4009: Read the following code snippet. What option will produce the same result?
- <jsp:getProperty name=myBean property=color />
- <jsp:getProperty "myBean.getColor" />
- <jsp:getProperty bean1.getColor/>
- <jsp:getProperty name="myBean" property="color" />
5. Session State, Scopes
- ServletSpec_5001: Which two of the following methods are used to access object attributes within the session scope?
Select 2 correct answers!
- getNames
- getSessionAttributes
- getAttribute
- getAttributeNames
- ServletSpec_5002: What is the interface used to access values and resources and to set object attributes within the session scope?
- HttpSession
- RequestSession
- HttpRequestSession
- Session
- ServletSpec_5003: What method accesses attributes within the request scope?
- getAttributes
- getNames
- getAttributeNames
- getNamedAttribute
- ServletSpec_5004: How could you reference a session in a servlet?
- Session mySession = Context.getSession();
- Session mySession = pageContext.getHttpSession();
- HttpSession mySession = pageContext.getHttpSession();
- HttpSession mySession = pageContext.getSession();
- ServletSpec_5005: Which two of the following methods are used to access resources within the context scope?
Select 2 correct answers!
- getSession
- getNamedDispatcher
- getDispatcher
- getRequestDispatcher
- ServletSpec_5006: Which two of the following methods are used to access values and resources and to set object attributes within the session scope?
Select 2 correct answers!
- setAttribute
- removeAttributeName
- deleteAttribute
- removeAttribute
6. Servlet Deployment
- ServletSpec_6001: Which two of the following statements are true regarding declaring a servlet instance in a deployment descriptor?
Select 2 correct answers!
- The servlet element specifies the fully qualified class name of the servlet.
- The tags are nested within <web-app> </web-app> tags.
- The tags do not define parameters.
- The tags are <servlet-instance></servlet-instance>.
- ServletSpec_6002: How many servlet mappings in a deployment descriptor may be defined for one Web app?
- Unlimited.
- Servlet mappings don't go into the deployment descriptor.
- Zero.
- One.
Unclassified Questions