Sample questions
Question 1Easy
Which HttpServletResponse method sends the browser to a different URL by telling it to make a new request?
- forward()
- include()
- setStatus(200)
- sendRedirect()
Answer: D. sendRedirect()
Explanation
sendRedirect returns a 302 style response with a Location header, so the browser issues a second request and the address bar changes.
Step-by-step solution
- Servlet calls
response.sendRedirect("/home"). - Container sends a redirect status and Location header.
- Browser requests the new URL.
Why the other options are wrong
- A.
forward is on RequestDispatcher and works server side. - B.
include also belongs to RequestDispatcher and adds output. - C.
setStatus(200) only sets the status code; there is no Location.
Question 2Easy
What is the difference between ServletContext and ServletConfig?
- Context is one per servlet; config is one per web application
- Both are created per request
- Config is shared across applications; context is private to a user
- Context is one per web application; config is one per servlet
Answer: D. Context is one per web application; config is one per servlet
Explanation
ServletConfig carries a servlet's own init parameters. ServletContext is the shared application scope used for context parameters and attributes.
Step-by-step solution
- Config object is passed to each servlet's init.
- Context object is shared by all servlets in the app.
Why the other options are wrong
- A. It is the reverse: context is application-wide.
- B. Neither is created per request.
- C. Neither is shared across web applications, and context is not per user.
Question 3Easy
Which annotation maps a servlet to a URL without web.xml?
- @Controller
- @WebFilter
- @WebServlet
- @RequestMapping
Answer: C. @WebServlet
Explanation
@WebServlet("/hello") registers the class as a servlet for that URL pattern in Servlet 3.0 and later.
Step-by-step solution
- Put @WebServlet on the class.
- Give a urlPatterns value.
- Container scans and registers it.
Why the other options are wrong
- A. @Controller belongs to Spring MVC.
- B. @WebFilter declares a filter, not a servlet.
- D. @RequestMapping is Spring MVC mapping.
Question 4Easy
In a servlet filter, which call passes the request on to the next filter or the target servlet?
- chain.forward(request, response)
- filterConfig.next(request, response)
- chain.doFilter(request, response)
- response.sendRedirect("next")
Answer: C. chain.doFilter(request, response)
Explanation
A Filter receives a FilterChain; calling doFilter on it hands control to the next element. If it is not called, the request stops at that filter.
Step-by-step solution
- The container calls
doFilter(req, res, chain). - Pre-process the request.
- Call
chain.doFilter(req, res) to continue. - Post-process the response after it returns.
Why the other options are wrong
- A.
FilterChain has no forward method; forwarding belongs to RequestDispatcher. - B.
FilterConfig only exposes init parameters and the context. - D.
sendRedirect makes the browser issue a new request; it does not continue the chain.
Tip: Forgetting doFilter silently blocks the request.
Question 5Medium
In a servlet filter, what happens if doFilter does not call chain.doFilter(request, response)?
- The request does not reach the next filter or the servlet
- The request still reaches the servlet by default after the filter returns
- The container calls the servlet itself once the filter method returns
- An exception is thrown at deployment time
Answer: A. The request does not reach the next filter or the servlet
Explanation
The filter decides whether to pass control. Skipping chain.doFilter blocks the request, which is how an authentication filter can reject a user.
Step-by-step solution
- Container invokes the first filter.
- Filter either calls chain.doFilter or writes its own response.
- If it does not call the chain, processing ends there.
Why the other options are wrong
- B. The chain only continues when the filter passes control on.
- C. The container does not call the servlet automatically.
- D. This is a legal pattern, so no deployment error occurs.
Tip: Block by not calling chain.doFilter.
Question 6Medium
How many instances of a given servlet class does a container normally create for one <servlet> declaration, and what does that imply?
- One instance; many threads call service() on it, so instance fields must be thread-safe
- One instance per request, so instance fields are always safe and never shared between users
- One instance per user session, so only session data is shared across the whole application
- One instance per thread, created and destroyed with each thread
Answer: A. One instance; many threads call service() on it, so instance fields must be thread-safe
Explanation
The container creates a single servlet instance and runs service() on it concurrently from several request threads. Mutable instance fields are therefore shared and need care.
Step-by-step solution
- Container loads the class once and calls
init() once. - Every request runs
service() on that same object in its own thread. - Shared instance fields can be corrupted by concurrent writes.
Why the other options are wrong
- B. Per-request instances would make
init() costly and is not how servlets work. - C. Sessions are separate objects; servlets are not per session.
- D. The container uses a thread pool but does not clone servlets per thread.
Tip: Keep per-request data in local variables.
Question 7Medium
Which annotation maps a servlet to the URL /hello without a web.xml entry?
- @RequestMapping("/hello")
- @Path("/hello")
- @ServletUrl("/hello")
- @WebServlet("/hello")
Answer: D. @WebServlet("/hello")
Explanation
@WebServlet (Servlet 3.0 and later) declares a servlet and its URL patterns in code. The container finds it by scanning classes at deploy time.
Step-by-step solution
- Annotate the class extending
HttpServlet. - Put the URL pattern in
value or urlPatterns. - The container registers it at startup.
Why the other options are wrong
- A.
@RequestMapping is a Spring MVC annotation. - B.
@Path is used by JAX-RS resources. - C.
@ServletUrl does not exist.
Question 8Medium
What does <load-on-startup>1</load-on-startup> do for a servlet?
- Creates and initialises it when the application starts instead of at the first request
- Makes the servlet run again every second like a timer task
- Gives it the highest priority when requests arrive concurrently
- Disables lazy session creation
Answer: A. Creates and initialises it when the application starts instead of at the first request
Explanation
A non-negative value tells the container to load the servlet at deployment. Lower numbers load first.
Step-by-step solution
- Container starts the app.
- Servlets with load-on-startup are initialised in numeric order.
- Others wait for the first request.
Why the other options are wrong
- B. It has no scheduling meaning.
- C. It affects load order, not request priority.
- D. It is unrelated to sessions.
Question 9Hard
A servlet declares an instance field int count and increments it in doGet. What is the main problem?
- Each request gets its own private copy of the servlet, so count is always 1
- One instance serves many threads, so the field is shared and unsafe without synchronisation
- Instance fields cannot be changed inside doGet after init has finished
- The field resets after every response
Answer: B. One instance serves many threads, so the field is shared and unsafe without synchronisation
Explanation
By default the container keeps a single servlet instance and runs service on many threads, so a mutable field creates a race condition. Use local variables or thread-safe types like AtomicInteger.
Step-by-step solution
- Container creates one instance.
- Several request threads enter doGet together.
- Unsynchronised
count++ can lose updates.
Why the other options are wrong
- A. There is no per-request copy of a servlet instance by default.
- C. Fields are modifiable; it is the sharing that is the issue.
- D. Nothing resets the field between requests.
Tip: Keep servlets stateless.
Question 10Hard
A servlet class has the field int count = 0; and doGet does count++ before printing it. Under heavy parallel traffic, which problem can occur?
- Lost updates, because the threads share one servlet instance
- A NullPointerException, because fields are cleared by the container after init() finishes
- Each user always sees their own separate count since fields are per request thread
- A compile error, since servlets cannot have fields
Answer: A. Lost updates, because the threads share one servlet instance
Explanation
count++ is read-modify-write on a shared field of a single servlet object, so concurrent threads can overwrite each other's increments. Use AtomicInteger or synchronisation.
Step-by-step solution
- One servlet instance serves all threads.
- Two threads read the same value.
- Both write back value+1, losing one increment.
Why the other options are wrong
- B. Fields are not cleared by the container.
- C. The count is shared by everyone, not per user.
- D. Servlets can have fields; the issue is thread safety.
Tip: Prefer AtomicInteger for simple shared counters.
Practise all 12 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →