Learn. Practice. Feel prepared.
Servlet Objective Questions and Answers - Set 2
Strengthen your understanding of Java Servlets with topic-wise multiple-choice questions covering Servlet lifecycle, request & response handling, sessions, cookies, filters, listeners, security, and deployment descriptors. This is part 2 of the series.
Practice Servlet MCQs with Detailed Explanations
Answer at least 12 questions to submit and verify answers.
16 Which cookie it is valid for single session only; it is removed each time when user closes the browser?
Correct answer:
Non-persistent cookie
17 Which object of HttpSession can be used to view and manipulate information about a session, such as the?
Correct answer:
All mentioned above
18 These methods doGet(),doPost(),doHead,doDelete(),deTrace() are used in?
Correct answer:
HttpServlets
19 Which type of ServletEngine is a server that includes built-in support for servlets?
Correct answer:
Standalone ServletEngine
20 Given a web application ShoopingCart with 2 files: ShoppingCart/customer/ addProductToCart.jsp ShoppingCart/customer/ showCart.jsp which of the following code are valid in addProductToCart.jsp to dispatch request to the showCart.jsp?
Correct answer:
RequestDispatcher nextView = request.getRequestDispatcher("/customer/showCart.jsp");
21 In the web.xml , session configuration is <session-config> <session-timeout>0<session-timeout> </session-config> What does this means?
Correct answer:
Session are never invalidated.
22 Which of the following statements are correct about the status of the Http response. Select the one correct answer.
Correct answer:
A status of 200 to 299 signifies that the request was successful.
23 Name the class that includes the getSession method that is used to get the HttpSession object.
Correct answer:
HttpServletRequest
24 Which HTTP method gets invoked when a user clicks on a link? Select the one correct answer.
Correct answer:
GET method
25 Which of the following are the methods defined in ServletContextListener interface?
Correct answer:
public void contextInitialized(ServletContextEvent event)
26 The sendRedirect method defined in the HttpServlet class is equivalent to invoking the setStatus method with the following parameter and a Location header in the URL. Select the one correct answer.
Correct answer:
SC_MOVED_TEMPORARILY
27 To send binary output in a response, the following method of HttpServletResponse may be used to get the appropriate Writer/Stream object. Select the one correct answer.
Correct answer:
getOutputStream
28 Name the method defined in the HttpServletResponse class that may be used to set the content type. Select the one correct answer.
Correct answer:
setContentType
29 The sendError method defined in the HttpServlet class is equivalent to invoking the setStatus method with the following parameter. Select the one correct answer.
Correct answer:
SC_NOT_FOUND
30 Which three digit error codes represent an error in request from client? Select the one correct answer.
Correct answer:
Codes starting from 400