Java Servlets MCQs: 12 Solved Request Handling Questions

Solve 12 Java Servlet MCQs with teacher-led explanations, an exact request trace, lifecycle checks, JSP translation, and shared-state pitfalls.

KnowledgeGate Team

Exam prep & CS education

Updated 19 Nov 20267 min read
Java Servlets MCQs: 12 Solved Request Handling Questions

Servlet MCQs mix three layers that can look almost identical in a short option: the container-managed lifecycle, the per-request work of HttpServletRequest and HttpServletResponse, and JSP code translated into a Servlet. First separate the browser and HTTP roles from the Java server role with Web Technologies for Teaching Exams: HTML and HTTP. The server-side role, Servlet contract, lifecycle, request and response flow, and JSP bridge are the boundaries to track. Choose each option before reading its explanation.

Questions 1, 2, 4, 5, 6 and 9 open the complete Servlet request-handling practice set; the other question headings open their individual solved pages.

Trace one Java Servlet request before solving the MCQs

The container creates and initialises a Servlet, then maps each HTTP request and calls service(). In HttpServlet, service() dispatches to doGet() or doPost(), which reads request data and writes the response. destroy() runs only at removal or shutdown.

java
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

@WebServlet("/score")
public class ScoreServlet extends HttpServlet {
    private final AtomicInteger totalRequests = new AtomicInteger();

    @Override
    public void init() {
        totalRequests.set(100);
    }

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        int requestNumber = totalRequests.incrementAndGet();
        response.setContentType("text/plain;charset=UTF-8");
        response.getWriter().printf("Hello %s, request #%d", name, requestNumber);
    }

    @Override
    public void destroy() {
        totalRequests.set(0);
    }
}

After init(), totalRequests is 100. GET /score?name=Asha reads Asha, increments to 101, sets text/plain;charset=UTF-8, and returns Hello Asha, request #101. Next, GET /score?name=Ravi returns Hello Ravi, request #102. On undeploy, destroy() resets the field to 0, not between requests. AtomicInteger keeps overlapping increments safe.

Flow diagram of ScoreServlet request handling, with totalRequests rising from 100 to 101 for Asha and 102 for Ravi before shutdown resets it to 0.

If the class, method, and object foundations need revision first, use Complete Java.

Java Servlets MCQs 1-3: server-side role and specification

Question 1

Which technology processes HTTP requests on the server side?

कौन-सी तकनीक सर्वर साइड पर HTTP रिक्वेस्ट को प्रोसेस करती है?

  • A. JavaBeans

  • B. Servlet

  • C. Swing

  • D. RMI

Answer: B. Servlet. A Servlet runs in a server-side container, receives HTTP request data, and produces the response. JavaBeans hold reusable state or behaviour, Swing builds desktop interfaces, and RMI invokes remote Java methods.

Question 2 (RPSC 2024)

Which of the following Java technology is used for server-side programming in Internet applications?

  • A. Java Servlet

  • B. Java Swing

  • C. JavaFX

  • D. Java Applets

  • E. Question not attempted

Answer: A. Java Servlet. It performs the server-side web job here. Swing and JavaFX are GUI toolkits, while an applet was client-side browser technology.

Question 3 (UPPSC Polytechnic Lecturer 2022)

The ______ specification defines an application programming interface for communication between the server and the application program.

  • A. Java Servlet

  • B. JDBC

  • C. Java Applet

  • D. Java Swing

Answer: A. Java Servlet. Its contract connects the container with Java web application code. JDBC serves database access, while Applet and Swing APIs do not define this request interface.

Memory line: HTTP request -> container mapping -> service() -> doGet()/doPost() -> HTTP response. For Asha, this ends with Hello Asha, request #101.

Java Servlets MCQs 4-6: lifecycle, interface and UI trap

Question 4

In the Servlet life cycle, which method is invoked only once when the servlet is first loaded?

  • A. service()

  • B. init()

  • C. destroy()

  • D. doGet()

Answer: B. init(). It performs one-time setup, setting the example's counter to 100. service() and doGet() handle requests, while destroy() performs cleanup at removal.

Question 5

Which interface must a class implement to become a Servlet?

  • A. java.net.ServerSocket

  • B. javax.servlet.Servlet

  • C. java.io.Serializable

  • D. javax.servlet.HttpServlet

Answer: B. javax.servlet.Servlet. The legacy option names the required Servlet interface. HttpServlet is a class, while ServerSocket and Serializable serve other purposes. The example uses the newer jakarta.servlet package.

Question 6 (UGC NET 2017)

Which of the following statement(s) is/are TRUE regarding Java Servelets ?

(a) A Java Servelet is a server-side component that runs on the web server and extends the capabilities of a server.

(b) A Servelet can use the user interface classes like AWT or Swing.

Code :

  • A. Only (a) is TRUE.

  • B. Only (b) is TRUE.

  • C. Both (a) and (b) are TRUE.

  • D. Neither (a) nor (b) is TRUE.

Answer: A. Only (a) is TRUE. Statement (a) gives the Servlet's server-side purpose. AWT and Swing build graphical interfaces, not a web Servlet's user-facing request and response mechanism, so (b) is false.

The interface-versus-class distinction becomes easier after revising Object Oriented Technology Explained.

Java Servlets MCQs 7-8: response setup and request dispatch

Question 7 (UGC NET 2015)

The Servlet Response interface enables a servlet to formulate a response for a client using the method _________ .

  • A. void log(Exception e, String s)

  • B. void destroy()

  • C. int get ServerPort()

  • D. void set ContextType(String Type)

Answer: D. void set ContextType(String Type). The option refers to the content-type setter, represented by response.setContentType("text/plain;charset=UTF-8") above. The stored spelling and spacing are not compilable Java, so do not copy them as code.

Question 8 (RPSC 2024)

What is the correct sequence of steps for handling a client in a Java servlet as per the lifecycle of a servlet?

  • A. Init method, Service method, doPost/doGet method

  • B. Init method, doPost/doGet method, Service method

  • C. Service method, doPost/doGet method, Destroy method

  • D. Init method, Service method, Destroy method

  • E. Question not attempted

Answer: D. Init method, Service method, Destroy method. The lifecycle is initialisation, request service, then destruction. In HttpServlet, service() dispatches internally to doGet() or doPost(), not to another lifecycle phase.

Value check: init() changes 0 -> 100 once. Asha and Ravi pass through service() and doGet(), producing 101 and 102. At shutdown, destroy() changes the counter to 0.

JSP and Servlet MCQs 9-12: implicit objects, translation and shared state

Question 9 (GATE 2004, Information Technology)

Which of the following objects can be used in expressions and scriplets in JSP (Java Server Pages) without explicitly declaring them?

  • A. session and request only

  • B. request and response only

  • C. response and session only

  • D. session, request and response

Answer: D. session, request and response. JSP supplies all three as implicit objects. They cover request input, response output, and session state; every other option omits one.

Question 10 (RPSC 2024)

Which of the following is true about JSP?

  • A. JSP pages are interpreted at runtime.

  • B. JSP pages are compiled into servlets.

  • C. JSP is not used to make interactive web pages.

  • D. JSP pages cannot use Java code.

  • E. Question not attempted

Answer: B. JSP pages are compiled into servlets. The container translates JSP into Servlet source and compiles it. Thus Servlet lifecycle and shared-instance reasoning also apply, while the other options misstate JSP.

Question 11 (UPPSC Polytechnic Lecturer 2022)

Which page direction should be used in JSP to generate a PDF page?

  • A. Generate Pdf

  • B. Content type

  • C. Type Pdf

  • D. Content Pdf

Answer: B. Content type. This intended page-directive setting identifies the response representation. A PDF response uses a PDF media type, not the invented phrases in A, C, or D.

Question 12 (RPSC 2024)

What will be the value of ‘val’ displayed when JSP page is accessed for the third time since server startup?

<%! int val = 0; %>

<% val++; %>

<html><body><p> val: <%= val %> </p></body></html>

  • A. 1

  • B. 2

  • C. 3

  • D. The value of val will remain 0

  • E. Question not attempted

Answer: C. 3. The declaration creates a translated-Servlet instance field, starting at 0 and incrementing on every access. Sequentially, it displays 1, 2, then 3. Such mutable instance fields are unsafe for concurrent request counting.

State trace comparing JSP values 1, 2, and 3 across three requests with AtomicInteger totalRequests rising from 100 to 101 for Asha and 102 for Ravi.

For structured exam revision that includes this subtopic, use NTA-UGC-NET Paper - 2.

Java Servlet MCQ traps and the 12-answer check

  • Separate server-side Java from desktop GUI APIs.

  • Remember init -> service -> destroy.

  • Treat doGet() and doPost() as dispatch targets inside HTTP servicing.

  • Read from request and write through response.

  • Distinguish the Servlet interface from the HttpServlet class.

  • Remember that JSP becomes a Servlet.

Check: init() sets 100, Asha gets 101, Ravi gets 102, and destroy() resets to 0. Question 12 moves 0 -> 1 -> 2 -> 3. Question 7 has a source typo, not reusable Java. Never repair quoted Servelets, scriplets, page direction, set ContextType, or javax.servlet.Servlet.

Answer key: 1 B, 2 A, 3 A, 4 B, 5 B, 6 A, 7 D, 8 D, 9 D, 10 B, 11 B, 12 C.

Java Servlets MCQs: the short version and next step

Three distinctions unlock this set: the container owns the lifecycle, each HTTP request passes through service() to a handler, and JSP is translated into Servlet code with the same shared-instance risks. Redraw Asha's request path from memory, then reproduce Hello Asha, request #101 and Hello Ravi, request #102. If Questions 5, 7, 8, or 12 were difficult, revisit interface versus class, response content type, lifecycle versus dispatch, and shared state before trying again. Browse Coding & DSA Courses for Placements for the wider learning path. Choose Complete Java when Java foundations need structure, or NTA-UGC-NET Paper 2 for exam-oriented Servlet and JSP revision.