This practice exam focuses on Servlets. For those using Head First Servlets and JSP, 2nd Edition it’s a perfect companion for chapters 4-6.
- Which of the following are interfaces? (3 correct answers)
- Servlet
- HttpServlet
- ServletRequest
- HttpServletRequest
- Which of the following are abstract classes? (2 correct answers)
- Servlet
- HttpServlet
- GenericServlet
- HttpServletRequest
- Which of the following statements is true? (1 correct answer)
- HttpServlet extends GenericServlet that implements Servlet.
- HttpServlet extends GenericServlet that extends Servlet.
- HttpServlet implements GenericServlet that extends Servlet.
- Which of the following statements are true? (2 correct answers)
- HttpServlet IS-A GenericServlet.
- HttpServlet IS-A Servlet.
- HttpServlet IS-A ServletRequest.
- Here are some actions taken by the Container when a client request arrives. Place them in the correct order starting from what happens first.
- Calls the void service(HttpServletRequest, HttpServletResponse) method of the servlet.
- Creates a pair of request and response objects.
- Finds the correct servlet based on the URL.
- Creates a new thread or allocates an existing thread to the client’s request.
- How can a servlet access it’s associated ServletConfig object? (1 correct answer)
- getServletConfig();
- request.getServletConfig();
- response.getServletConfig();
- getServletContext().getServletConfig();
- request.getSession().getServletConfig();
- How can a servlet access the application’s ServletContext object? (3 correct answers)
- getServletContext();
- request.getServletContext();
- response.getServletContext();
- getServletConfig().getServletContext();
- request.getSession().getServletContext();
- How is a request dispatched to hello.jsp from a doGet() method? (1 correct answer)
- request.getRequestDispatcher().forward(“hello.jsp”);
- request.getRequestDispatcher().dispatch(“hello.jsp”);
- request.getRequestDispatcher(“hello.jsp”).forward(request, response);
- request.getRequestDispatcher(“hello.jsp”).dispatch(request, response);
- How is a request redirected to hello.jsp from a doGet() method? (1 correct answer)
- request.redirect(“hello.jsp”);
- response.redirect(“hello.jsp”);
- request.sendRedirect(“hello.jsp”);
- response.sendRedirect(“hello.jsp”);
- Dispatching a request occurs on the server-side and redirection on the client-side. (1 correct answer)
- true
- false
- Both context init parameters and servlet init parameters are declared in the web.xml. (1 correct answer)
- true
- false
- The value of a servlet init parameter can be changed programmatically, but the value of a context init parameter cannot. (1 correct answer)
- true
- false
- A context init parameter cannot have the same name with a servlet init parameter. (1 correct answer)
- true
- false
- A servlet init parameter cannot have the same name with the servlet it refers to. (1 correct answer)
- true
- false
- Where is a servlet init parameter stored after the servlet is initialized and available for use? (1 correct answer)
- In the ServletConfig object of the servlet.
- In the ServletContext object of the web application.
- Where is a context init parameter stored after the servlet is initialized and available for use? (1 correct answer)
- In the ServletConfig object of the servlet.
- In the ServletContext object of the web application.
- Assume the servlet HelloServlet that belongs to package com. The file HelloServlet.class is placed in the directory WEB-INF/classes/com. Is this a correct declaration of an init parameter for this servlet? (1 correct answer)
<servlet> <servlet-name>Hello Servlet</servlet-name> <servlet-class>classes.com.HelloServlet</servlet-class> <init-param> <param-name>this</param-name> <param-value>Hello!</param-value> </init-param> </servlet>
- Yes.
- No, because servlet-name contains a space.
- No, because servlet-class has a wrong value.
- No, because param-name is a reserved Java keyword.
- No, because param-value contains an explanation mark (!).
- No, because init-param should be inside a servlet-mapping element.
- What happens when we compile and deploy this servlet? (1 correct answer)
public class Test extends HttpServlet { }- Compilation fails because there is no init() method defined.
- An exception is thrown at runtime because service() has no method to call!
- Deployment succeeds but we get a message “GET is not supported by this URL” if we access it.
- What happens when we compile and deploy this servlet? (1 correct answer)
class Test extends HttpServlet { }- Compilation fails because there is no init() method defined.
- An exception is thrown at runtime because the servlet has no modifier.
- An exception is thrown at runtime because service() has no method to call!
- Deployment succeeds but we get a message “GET is not supported by this URL” if we try to access its URL.
- What happens when this servlet is compiled, deployed and called? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO } }- Compilation fails because doGet() is empty.
- An exception is thrown at runtime because doGet() is empty.
- Deployment succeeds but nothing is displayed to the user’s browser.
- What happens when this servlet is compiled, deployed and called? (1 correct answer)
public class Test extends HttpServlet { public String doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO return null; } }- Compilation fails because doGet() must be void.
- Deployment succeeds but nothing is displayed to the user’s browser.
- A NullPointerException is thrown at runtime because null is returned.
- A ServletException is thrown at runtime because service() cannot find the proper doGet() method.
- What happens when this servlet is compiled and deployed? (1 correct answer)
public class Test extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<p>Hello!!</p>"); out.println("</body></html>"); out.close(); } }- Compilation fails because doGet() is protected.
- Compilation fails because doGet() does not declare a ServletException.
- Deployment succeeds and clients are served just fine.
- What happens when this servlet is deployed and called? (1 correct answer)
public class Test extends HttpServlet { public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html; charset=UTF-8"); final PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<p>Hello!!</p>"); out.println("</body></html>"); out.close(); } }- An exception is thrown at runtime because the Container cannot modify the request and response objects.
- An exception is thrown at runtime when the out object is closed.
- Deployment succeeds and clients are served just fine.
- What happens when this servlet is deployed and called? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); //out.println("<html><body>"); out.println("<p>Hello!!</p>"); //out.println("</body></html>"); out.close(); } }- A ServletException is thrown at runtime because the <html> and <body> tags are missing.
- Deployment succeeds and Hello!! is presented on the browser.
- The server responds with a HTTP status code 404: “Not Found”.
- What happens when this servlet is compiled, deployed and called? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<html><body>Hello!!</body></html>"); out.close(); } }- Deployment succeeds and Hello!! is presented on the browser.
- Compilation fails because the content type should be specified before any output is written.
- An exception is thrown at runtime because the response has not an explicitly set content type.
- What happens when this servlet is compiled, deployed and called? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<html><body>Hello!!</body></html>"); //out.close(); } }- An exception is thrown at runtime because out is not closed.
- Deployment succeeds and Hello!! is presented on the browser.
- Deployment succeeds but no output is presented on the browser.
- Does this servlet compile successfully? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception { // line #1 response.setContentType("text/html; charset=UTF-8"); response.getWriter().println("Hello!!").close(); // line #2 } }- Compilation succeeds.
- There is a compilation error at line #1.
- There is a compilation error at line #2.
- Both lines #1 and #2 contain a compilation error.
- Does this servlet compile successfully? (1 correct answer)
public class Test extends HttpServlet { public void init(ServletConfig config) { super.init(config); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<html><body>Hello!</body></html>"); out.close(); } }- Compilation succeeds.
- Compilation fails because there is no init(ServletConfig) in GenericServlet.
- Compilation fails because init(ServletConfig) of GenericServlet throws ServletException.
- Compilation fails because init(ServletConfig) of GenericServlet throws IOException and ServletException.
- Does this servlet compile successfully? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse response) throws IOException { req.getRequestDispatcher("index.html").forward(req, response); } }- Compilation succeeds.
- Compilation fails because index.html is not a jsp.
- Compilation fails because doGet should declare ServletException as well.
- Compilation fails because the request object’s reference must be request and not req.
- Does this servlet compile successfully? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse response) throws IOException { response.sendRedirect("index.html"); } }- Compilation succeeds.
- Compilation fails because index.html is not a jsp.
- Compilation fails because doGet should declare ServletException as well.
- Compilation fails because the request object’s reference must be request and not req.
- Which of the following statements shoud be inserted for a successful compilation? (1 correct answer)
public class Test extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) { // insert statement } }- request.getRequestDispatcher(“hello.jsp”).forward(request, response);
- request.getRequestDispatcher(“hello.jsp”).forward(response, request);
- response.getRequestDispatcher(“hello.jsp”).forward(request, response);
- response.getRequestDispatcher(“hello.jsp”).forward(response, request);
- None of the above.
- What happens when the servlet with the following method is deployed and called? (1 correct answer)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().print('a'); request.getRequestDispatcher("hello.jsp").forward(request, response); // line #1 response.getWriter().print('a'); // line #2 response.getWriter().close(); // line #3 }- An IllegalStateException is thrown at runtime because response.getWriter() is called more than once.
- An IllegalStateException is thrown at runtime at line #1 because the request is dispatched after writing data.
- An IllegalStateException is thrown at runtime at line #2 because data is written after the request has been dispatched.
- An IllegalStateException is thrown at runtime at line #3 because the writer is closed after the request has been dispatched.
- The browser displays the content of hello.jsp without any exception at runtime.
- The browser displays aa without any exception at runtime.
- The browser displays a without any exception at runtime.
- What happens when the servlet with the following method is deployed and called? (1 correct answer)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getServletContext().getRequestDispatcher("hello.jsp").forward(request, response); }- An exception is thrown at runtime because the provided URL does not start with “/”.
- Compilation fails because the provided URL does not start with “/”.
- The browser displays the content of hello.jsp.
- The browser displays an empty page.
- What happens when the servlet with the following method is deployed and called? (1 correct answer)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getServletContext().getRequestDispatcher("/hello.jsp"); }- An exception is thrown at runtime because the provided URL is absolute.
- Compilation fails because the provided URL is absolute.
- The browser displays the content of hello.jsp.
- The browser displays an empty page.
- A servlet init parameter with name “ice” and value “tea” is properly declared. What is the result of this code? (1 correct answer)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(getServletConfig().getInitParameter("tea")); }- A NullPointerException occurs at runtime.
- The browser displays an empty page.
- The browser displays “null”.
- The browser displays “ice”.
- The browser displays “tea”.
- Compilation fails.
- A servlet init parameter with name “ice” and value “tea” is properly declared. What is the result of this code? (1 correct answer)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(getInitParameter("ice")); }- A NullPointerException occurs at runtime.
- The browser displays an empty page.
- The browser displays “null”.
- The browser displays “ice”.
- The browser displays “tea”.
- Compilation fails.
- A context init parameter with name “ice” and value “tea” is properly declared. What is the result of this code? (1 correct answer)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(getServletContext().getInitParameter("tea")); }- A NullPointerException occurs at runtime.
- The browser displays an empty page.
- The browser displays “null”.
- The browser displays “ice”.
- The browser displays “tea”.
- Compilation fails.
- A context init parameter with name “ice” and value “tea” is properly declared. What is the result of this code? (1 correct answer)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(getContextParameter("ice")); }- A NullPointerException occurs at runtime.
- The browser displays an empty page.
- The browser displays “null”.
- The browser displays “ice”.
- The browser displays “tea”.
- Compilation fails.
- The Container creates a single instance for every servlet. The client requests are served with various threads. (1 correct answer)
- true
- false
- Assume that the container is running on port 9999 of localhost, our application is called test1 and that the following servlet is declared with url pattern /processor. What is the output? (1 correct answer)
public class Processor extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(request.getContextPath()); } }- http://localhost:9999/test1/processor
- /test1/processor
- /processor
- /test1
- Assume that the container is running on port 9999 of localhost, our application is called test1 and that the following servlet is declared with url pattern /processor. What is the output? (1 correct answer)
public class Processor extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(request.getServletPath()); } }- http://localhost:9999/test1/processor
- /test1/processor
- /processor
- /test1
- Assume that the container is running on port 9999 of localhost, our application is called test1 and that the following servlet is declared with url pattern /processor. What is the output? (1 correct answer)
public class Processor extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(request.getRequestURL()); } }- http://localhost:9999/test1/processor
- /test1/processor
- /processor
- /test1
- Assume that the container is running on port 9999 of localhost, our application is called test1 and that the following servlet is declared with url pattern /processor. What is the output? (1 correct answer)
public class Processor extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(request.getRequestURI()); } }- http://localhost:9999/test1/processor
- /test1/processor
- /processor
- /test1
- Consider this form.
<form method="post" action="processor"> <input type="submit" value="OK"/> </form>
And this servlet.
public class Processor extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(request.getMethod()); } }What is the output when the form is submitted? (1 correct answer)
- GET
- POST
- A HTTP 405 message informs that GET is not supported.
- A HTTP 405 message informs that POST is not supported.
- Consider this form.
<form method="post" action="processor"> <input type="submit" value="OK"/> </form>
And this servlet.
public class Processor extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(request.getMethod()); } public void doPost(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException { doGet(request, response); } }What is the output when the form is submitted? (1 correct answer)
- GET
- POST
- A HTTP 405 message informs that GET is not supported.
- A HTTP 405 message informs that POST is not supported.
- Consider this form.
<form name="form" method="post" action="processor"> <input name="submit" type="submit" value="OK"/> </form>
And this servlet.
public class Processor extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().format("%s %s", request.getParameter("form"), request.getParameter("name")); } }What is the output when the form is submitted? (1 correct answer)
- form OK
- null OK
- form null
- null null
- Consider this form.
<form name="form" method="post" action="processor"> <input name="submit" type="submit" value="OK"/> </form>
And this servlet.
public class Processor extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().format("%s %s", request.getParameter("form"), request.getParameter("submit")); } }What is the output when the form is submitted? (1 correct answer)
- form OK
- null OK
- form null
- null null
- Consider this form.
<form name="form" method="post" action="processor"> <input type="text" name="name" value="OK"/> <input name="submit" type="submit" value="OK"/> </form>
And this servlet.
public class Processor extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().format("%s %s", request.getParameter("name"), request.getParameter("OK")); } }What is the output when the form is submitted? (1 correct answer)
- OK OK
- null OK
- OK null
- null null
- Consider doGet() of a valid servlet with url pattern /source,
request.getRequestDispatcher("target").forward(request, response);and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getRequestURL());
What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- http://localhost:9999/exam/source
- http://localhost:9999/exam/target
- null
- Consider doGet() of a valid servlet with url pattern /source,
request.getRequestDispatcher("target").forward(request, response);and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getQueryString());
What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- user=nikos&pass=12345
- null
- Consider doGet() of a valid servlet with url pattern /source,
request.getRequestDispatcher("target").forward(request, response);and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getAttribute("javax.servlet.forward.query_string"));What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- user=nikos&pass=12345
- null
- Consider doGet() of a valid servlet with url pattern /source,
request.getRequestDispatcher("target").forward(request, response);and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getAttribute("javax.servlet.forward.servlet_path"));What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- /source
- /target
- null
- Consider doGet() of a valid servlet with url pattern /source,
request.getRequestDispatcher("target").forward(request, response);and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getServletPath());
What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- /source
- /target
- null
- Consider doGet() of a valid servlet with url pattern /source,
request.getRequestDispatcher("target").forward(request, response);and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getParameter("pass"));What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- 12345
- null
- Consider doGet() of a valid servlet with url pattern /source,
response.sendRedirect("target");and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getParameter("pass"));What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- 12345
- null
- Consider doGet() of a valid servlet with url pattern /source,
response.sendRedirect("target");and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getServletPath());
What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- /source
- /target
- null
- Consider doGet() of a valid servlet with url pattern /source,
response.sendRedirect("target");and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getAttribute("javax.servlet.forward.query_string"));What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- user=nikos&pass=12345
- null
- Consider doGet() of a valid servlet with url pattern /source,
response.sendRedirect("target");and doGet() of another valid servlet with url pattern /target.
response.getWriter().println(request.getQueryString());
What is the output when invoking http://localhost:9999/exam/source?user=nikos&pass=12345? (1 correct answer)
- user=nikos&pass=12345
- null
- There is a top-level folder help in the war file with index.html inside. What is the output of this code? (1 correct answer)
request.getRequestDispatcher("help/index.html").forward(request, response);- The content of index.html
- HTTP Status 404
- There is a top-level folder help in the war file with index.html inside. What is the output of this code? (1 correct answer)
request.getRequestDispatcher("/help/index.html").forward(request, response);- The content of index.html
- HTTP Status 404
- What is the output of this code? (1 correct answer)
request.getRequestDispatcher("http://fcom.gr").forward(request, response);- The content of fcom.gr
- HTTP Status 404
- There is a top-level folder help in the war file with index.html inside. What is the output of this code? (1 correct answer)
response.sendRedirect("help/index.html");- The content of index.html
- HTTP Status 404
- There is a top-level folder help in the war file with index.html inside. What is the output of this code? (1 correct answer)
response.sendRedirect("/help/index.html");- The content of index.html
- HTTP Status 404
- What is the output of this code? (1 correct answer)
response.sendRedirect("http://fcom.gr");- The content of fcom.gr
- HTTP Status 404
- What happens when this servlet is deployed and a user hits repeatedly the refresh button of his browser? (1 correct answer)
public class Test extends HttpServlet { private Integer number = 0; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { number = number + 1; response.getWriter().println(number); } }- The same value is always displayed.
- The displayed value increases with every refresh.
- What happens when this servlet is deployed and a user hits repeatedly the refresh button of his browser? (1 correct answer)
public class Test extends HttpServlet { private Integer number = new Random().nextInt(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println(number); } }- The same value is always displayed.
- The displayed value increases with every refresh.
- The following types are ALL interfaces and are used as listeners in a web application. (1 correct answer)
⇒ ServletContextListener
⇒ ServletContextAttributeListener
⇒ ServletRequestListener
⇒ ServletRequestAttributeListener
⇒ HttpSessionListener
⇒ HttpSessionBindingListener
⇒ HttpSessionAttributeListener
⇒ HttpSessionActivationListener- true
- false
- Consider the interface ServletContextListener. What is the signature of the method that is invoked when the servlet context is about to be shut down? (1 correct answer)
- void contextDeleted(ServletContextEvent)
- void contextDestroyed(ServletContextEvent)
- void servletContextDeleted(ServletContextEvent)
- void servletContextDestroyed(ServletContextEvent)
- There is a properly declared HttpSessionAttributeListener. How many times its attributeRemoved method is invoked when the following servlet is accessed once? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().setAttribute("planet", "earth"); request.getSession().removeAttribute("planet"); request.getSession().removeAttribute("planet"); } }- 0
- 1
- 2
- There is a properly declared HttpSessionAttributeListener. How many times its attributeRemoved method is invoked when the following servlet is accessed once? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().setAttribute("planet", "earth"); request.getSession().invalidate(); request.getSession().removeAttribute("planet"); request.getSession().setAttribute("planet", "earth"); } }- 0
- 1
- 2
- There is a properly declared HttpSessionAttributeListener. How many times its attributeReplaced method is invoked when the following servlet is accessed once? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().setAttribute("planet", "earth"); request.getSession().removeAttribute("earth"); request.getSession().setAttribute("planet", "earth"); } }- 0
- 1
- 2
- There is a properly declared HttpSessionAttributeListener. How many times its attributeReplaced method is invoked when the following servlet is accessed once? (1 correct answer)
public class Test extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().setAttribute("planet", "earth"); request.getSession().removeAttribute("planet"); request.getSession().setAttribute("planet", "venus"); request.setAttribute("planet", "earth"); request.setAttribute("planet", "venus"); } }- 0
- 1
- 2
© 2008-2009 Nikos Pougounias. A free contribution to the Java community. Please distribute it for free. http://nikojava.wordpress.com
Answers
- a, c, d
- b, c
- a
- a, b
- b, c, d, a
- a
- a, d, e
- c
- d
- a
- a
- b
- b
- b
- a
- b
- c
- c
- b
- c
- a
- c
- c
- b
- a
- b
- d
- c
- c
- a
- e
- e
- a
- d
- c
- e
- c
- f
- a
- d
- c
- a
- b
- d
- d
- d
- b
- c
- b
- a
- a
- a
- b
- a
- b
- b
- b
- b
- a
- a
- b
- a
- b
- a
- b
- a
- a
- b
- b
- b
- b
- a
Tags: certification, Java, SCWCD, Web
13 March 2009 at 7:58 am
[...] Hi, I’ve compiled a free mock specific for Servlets >here. [...]
15 March 2009 at 8:20 pm
thanks for the mock.
My comment is, the answer no have literal caracter correspondency
16 March 2009 at 8:23 am
Hi, again, i want to ask you about the question 45, i think that the right answer is B, can you explain why is d? Thanks for the mock again.
17 March 2009 at 9:43 pm
Hi Milton! The answer would be GET if the servlet was directly accessed. But here, it is accessed through a JSP…
17 March 2009 at 9:51 pm
what do you mean directly accesed?
I test this question with a html file, and the POST method is called normally.
Tonight I will test accesing through a JSP, but i think that the answer will be the same!
17 March 2009 at 11:26 pm
Have you used the exact code of the question?
18 March 2009 at 1:06 am
I get it,
the method parameters are inverted (HttpSevletResponse, HttpSevletRequest).
I will be most focus on that stuff
Thanks again.
19 March 2009 at 6:00 pm
Hi Niko,
Thanks for the mock.
A question, why does #16 have B as answer?
I would say A, because you can access ServletContext from the ServletConfig, so in fact ServletConfig has a ServletContext object.
22 March 2009 at 12:39 pm
Hi Francis,
Of course you can access the ServletContext from any ServletConfig (see question 7). However it’s the ServletContext that holds context init parameters!
27 March 2009 at 9:37 pm
Can someone explain me why in Q 71, the answer will be (B). I dont understand now since the session is invalidated, how can i remove any attributes from it. The session doesnt exists ?? So how can an attribute be still there ?
30 August 2009 at 12:50 pm
Amit,
you mean qn 70?
Yes. When session is invalidated then the attribute removed is called.
Here attribute removed is not called when the statement: request.getSession().removeAttribute(“planet”);
is executed but when the session is invalidated.
10 September 2009 at 7:11 pm
Hello…….
I get confused in qu.29 and qu.30. In 29 code gets fails as it require that servlet should throw ServletException then is that not needed for qu.30..
please help me…
30 October 2009 at 8:18 am
I think the answer of question 50 is wrong.
In question 62, the question would be more exact if there was a URL representing the requested servlet which performs the redirect as the path in sendRedirect is relative.
Similarly in question 63, I would recommend that you wrote that the application is deployed on a context path (whatever it may be) as the answer would be wrong if the application was deployed on root or default context path…
30 October 2009 at 8:20 am
Answer of question 45 also seems wrong to me…
30 October 2009 at 9:15 am
priya, the RequestDispatcher.forward method throws ServletException but the HttpServletResponse.sendRedirect doesn’t throw ServletException…