MCQs on Java Servlets and JSP | Web Development in Java

Master Java web development with these 30 MCQs focused on Java Servlets and JSP. These questions cover the core concepts of Servlets, JSP Architecture, Session Management, and communication between Servlets and JSP.


MCQs on Java Servlets and JSP

Introduction to Servlets

  1. What is a servlet in Java?
    • a) A database connection
    • b) A class that handles HTTP requests
    • c) A client-side component
    • d) A type of Java database
  2. Which package provides classes and interfaces for servlets?
    • a) java.net
    • b) javax.servlet
    • c) java.sql
    • d) java.io
  3. What method is used to handle HTTP GET requests in a servlet?
    • a) doPost()
    • b) doGet()
    • c) handleRequest()
    • d) execute()
  4. What is the purpose of the service() method in a servlet?
    • a) To initialize servlet parameters
    • b) To read input from the user
    • c) To handle HTTP requests and send a response
    • d) To store session information
  5. Which of the following is true about a servlet’s lifecycle?
    • a) It is initialized, then destroyed.
    • b) It is created once and can handle multiple requests.
    • c) It runs on the client-side.
    • d) It is automatically garbage collected.

JSP Architecture

  1. What does JSP stand for?
    • a) Java Standard Pages
    • b) Java Server Pages
    • c) Java Script Pages
    • d) Java Session Pages
  2. Which tag is used to embed Java code into a JSP page?
    • a) <% %>
    • b) <!DOCTYPE>
    • c) <jsp:script>
    • d) <java>
  3. How does JSP differ from Servlets?
    • a) JSP is more efficient
    • b) JSP separates HTML and Java code
    • c) Servlets can only handle HTTP, JSP cannot
    • d) JSP is only used on the client-side
  4. What is the file extension for a JSP file?
    • a) .jsp
    • b) .java
    • c) .html
    • d) .js
  5. Which component processes the JSP page and converts it into a servlet?
    • a) JSP Compiler
    • b) JSP Container
    • c) JSP Engine
    • d) JSP Servlet

Session Management

  1. What is session management used for in Java web applications?
    • a) Storing client-side data
    • b) Tracking user interactions across requests
    • c) Compiling the web application
    • d) Serving static content
  2. How is a session identified in Java?
    • a) By the URL
    • b) By a session ID stored in a cookie
    • c) By the user’s browser history
    • d) By the IP address
  3. Which method is used to get the session object in a servlet?
    • a) getSession()
    • b) getSession(true)
    • c) createSession()
    • d) getRequest()
  4. What is the default session timeout period in a servlet?
    • a) 30 minutes
    • b) 15 minutes
    • c) 60 minutes
    • d) 20 minutes
  5. Which object is used to store session data in a servlet?
    • a) HttpServletRequest
    • b) HttpSession
    • c) ServletConfig
    • d) ServletContext
  6. How can a session be invalidated in Java?
    • a) session.invalidate()
    • b) session.close()
    • c) session.terminate()
    • d) session.end()
  7. Which of the following is used to set the session timeout in a web.xml file?
    • a) <session-timeout>
    • b) <timeout-session>
    • c) <session>
    • d) <time-out>
  8. Which HTTP header is used to maintain a session in the browser?
    • a) Set-Cookie
    • b) Authorization
    • c) Location
    • d) Content-Type
  9. What is the purpose of the HttpServletRequest.getSession() method?
    • a) To get the current session object
    • b) To create a new session object
    • c) To check if the session is valid
    • d) To remove the session object
  10. In which scope are session attributes available?
    • a) Request scope
    • b) Application scope
    • c) Session scope
    • d) Page scope

Servlet and JSP Communication

  1. Which of the following is the correct way to forward a request from a servlet to a JSP?
    • a) RequestDispatcher.forward()
    • b) JSP.forward()
    • c) response.sendRedirect()
    • d) RequestDispatcher.sendRedirect()
  2. What does the <jsp:include> tag do in a JSP page?
    • a) It includes external Java code
    • b) It includes another JSP page at runtime
    • c) It compiles the JSP page
    • d) It sends an HTTP request
  3. How can a servlet pass data to a JSP page?
    • a) Using a session variable
    • b) Using request.setAttribute()
    • c) Using response.sendRedirect()
    • d) All of the above
  4. Which JSP tag is used to forward a request to another resource?
    • a) <jsp:forward>
    • b) <jsp:include>
    • c) <jsp:redirect>
    • d) <jsp:route>
  5. Which of the following is true when a servlet forwards a request to a JSP?
    • a) The request and response objects are not passed to the JSP.
    • b) The request and response objects are passed to the JSP.
    • c) The request is processed separately by the servlet.
    • d) The response is sent immediately without the JSP.
  6. How do you redirect from a servlet to a JSP page?
    • a) request.getRequestDispatcher("page.jsp").forward()
    • b) response.sendRedirect("page.jsp")
    • c) response.forward("page.jsp")
    • d) request.sendRedirect("page.jsp")
  7. Which scope allows you to share data between a servlet and a JSP page?
    • a) Request scope
    • b) Session scope
    • c) Application scope
    • d) All of the above
  8. What is the purpose of the response.sendRedirect() method in servlets?
    • a) To forward the request to a different servlet
    • b) To redirect the client to a new URL
    • c) To execute the servlet logic
    • d) To execute a JSP page directly
  9. What is the advantage of using the RequestDispatcher.forward() method in servlets?
    • a) It sends a new HTTP request to the client.
    • b) It forwards the request to another server.
    • c) It forwards the request and response within the same web application.
    • d) It processes the response directly.
  10. How do you include dynamic content in a JSP page from another resource?
    • a) response.sendRedirect()
    • b) <jsp:include> tag
    • c) RequestDispatcher.forward()
    • d) ServletContext.include()

Answer Table

QnoAnswer
1b) A class that handles HTTP requests
2b) javax.servlet
3b) doGet()
4c) To handle HTTP requests and send a response
5b) It is created once and can handle multiple requests
6b) Java Server Pages
7a) <% %>
8b) JSP separates HTML and Java code
9a) .jsp
10b) JSP Container
11b) Tracking user interactions across requests
12b) By a session ID stored in a cookie
13b) getSession(true)
14a) 30 minutes
15b) HttpSession
16a) session.invalidate()
17a) <session-timeout>
18a) Set-Cookie
19a) To get the current session object
20c) Session scope
21a) RequestDispatcher.forward()
22b) It includes another JSP page at runtime
23d) All of the above
24a) <jsp:forward>
25b) The request and response objects are passed to the JSP
26b) response.sendRedirect("page.jsp")
27d) All of the above
28b) To redirect the client to a new URL
29c) It forwards the request and response within the same web application
30b) <jsp:include> tag

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top