Servlet and JSP MCQ Test

20 random Servlet and JSP MCQs with answers: the servlet lifecycle, request and response, forms, sessions and cookies, filters, JSP, JSTL and MVC.

  • 20 random questions
  • 20 minutes
  • 24 in the question bank

No sign-up needed. Answers and explanations are shown at the end.

About this test

Servlets and JSP are still asked in Java web interviews because they show how the web works underneath Spring. This test covers the lifecycle, request handling, sessions, filters and JSP with JSTL.

It is a good check before you move on to Spring MVC.

Each attempt picks new questions at random from the question bank. You can go back and change answers until you finish or the timer runs out. At the end you see your score, the right answer and a short explanation for every question, and the lessons to revise for the ones you missed.

Sample questions with answers

1. What is a servlet container?

  1. A Docker container
  2. A Java collection
  3. Software like Tomcat that runs servlets and handles HTTP
Show answer

C. Software like Tomcat that runs servlets and handles HTTP — It maps URLs, manages threads, sessions and the servlet lifecycle.

2. Which packaging does a servlet web app use for Tomcat?

  1. jar
  2. zip
  3. war
Show answer

C. war — A Web ARchive is deployed into Tomcat's webapps folder.

3. How many times is init() called?

  1. Once per request
  2. Once
  3. Never
Show answer

B. Once — init runs once; service runs for every request.

4. Why redirect after a successful POST (PRG)?

  1. To make it faster
  2. Because POST can't return HTML
  3. So refreshing doesn't resubmit the form
Show answer

C. So refreshing doesn't resubmit the form — The browser then makes a harmless GET.

5. Where is HttpSession data stored?

  1. On the server, identified by the JSESSIONID cookie
  2. In the URL
  3. In the browser
Show answer

A. On the server, identified by the JSESSIONID cookie — The cookie is just the token; the data stays server-side.

6. How does a filter block a request?

  1. It doesn't call chain.doFilter and sends an error or redirect
  2. It throws away the servlet
  3. It calls destroy
Show answer

A. It doesn't call chain.doFilter and sends an error or redirect — Calling chain.doFilter passes the request on.

7. In servlet + JSP MVC, the JSP is the…

  1. Model
  2. View
  3. Controller
Show answer

B. View — The servlet controls, Java classes model, JSP displays.

8. Why use PreparedStatement with ? placeholders?

  1. It makes queries shorter
  2. It prevents SQL injection
  3. It's required by MySQL
Show answer

B. It prevents SQL injection — Never concatenate user input into SQL.