Learn. Practice. Feel prepared.
Servlet Objective Questions and Answers - Set 3
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 3 of the series.
Practice Servlet MCQs with Detailed Explanations
Answer at least 12 questions to submit and verify answers.
31 Name the location of compiled class files within a war file? Select the one correct answer.
Correct answer:
/WEB-INF/classes
32 I have a web application on tomcat. Here many servlets open their own separate connection to database. To optimize this application, I decided to ask all servlet developers to use same db connection. I need to initialize this connection, before any servlet / jsp will be accessed. I got following suggestions from my developers. Which is the best way to do it?
Correct answer:
Make the connection in a context listener and save it.
33 I have an attribute class Class User{ String name; String address; String phoneNo; boolean isLogged; } This class will be added to the session when user logs in and removed from the session when user logs out. I want its isLogged flag to be changed when user login and logs out. Using which listener I can do it?
Correct answer:
javax.servlet.http.HttpSessionBindingListener
34 Which of the following statement is correct. Select the one correct answer.
Correct answer:
The HttpServletResponse defines constants like SC_NOT_FOUND that may be used as a parameter to setStatus method
35 To send text 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:
getWriter
36 When using HTML forms which of the folowing is true for POST method? Select the one correct answer.
Correct answer:
POST method sends data in the body of the request.
37 HttpServlet extends
Correct answer:
javax.servlet.GenericServlet class
38 Which statement is false ?
Correct answer:
There is no restriction for name of the cookie tracking the session.
39 Which of the following code retrieves the Internet Protocol (IP) address of the client that sent the request?
Correct answer:
request.getRemoteAddr()
40 Which of the following code can be used to force any content in the buffer to be written to the client?
Correct answer:
response.flushBuffer()
41 Which element of web.xml is used to specify the error handler in servlets?
Correct answer:
error-page
42 Which of the following is true about javax.servlet.error.message?
Correct answer:
This attribute gives exact error message which can be stored and analysed after storing in a java.lang.String data type.
43 Which of the following code is used to set auto refresh of a page after 5 seconds?
Correct answer:
response.setIntHeader("Refresh", 5)
44 How can we get context init parameter and run some code before rest of the application can service a client?
Correct answer:
ServletContextListener
45 If you want to track active session or how many users are there? So which listener you should use.
Correct answer:
HttpSessionListener
46 Suppose you want to know each time request comes in, so that you can log it. Which listener is appropriate for that?
Correct answer:
ServletRequestListener
47 You have an attribute class, and you want object of this type should be notified when they are bound to or removed from a session. So which listener is appropriate one?
Correct answer:
HttpSessionBindingListener
48 Which of these protocols are supported by HttpServlet?
Correct answer:
http , https