J2EE interview questions and answers for enterprise Java developers

J2EE interview questions test whether you understand how enterprise Java applications run inside a container: servlets and filters handling requests, sessions, EJBs with container-managed transactions, messaging with JMS, dependency injection with CDI, and how all of it is packaged and deployed to an application server. The platform has since been renamed Java EE and then Jakarta EE, so good answers mention the current names and the javax to jakarta package change. Expect concept questions first, then scenarios about threading, transactions and migrations.

By Published 7 min read

Where J2EE questions come up today

Many job posts still say J2EE, especially in banking, insurance, government and large service companies that run long-lived systems on application servers such as WebLogic, WebSphere, JBoss EAP or WildFly. The work is often maintenance, modernization or migration, so interviewers want to know you can read a web.xml, trace a transaction across EJBs and explain what the container does on your behalf. Newer teams on Spring Boot ask the same concepts in Spring terms, covered in Spring Boot interview questions; the persistence layer is covered in Hibernate interview questions.

  1. Platform basicsNaming history, tiers, containers, packaging and deployment.
  2. Web tierServlet lifecycle, filters, listeners, sessions, JSP and the request flow.
  3. Business tierEJBs, transactions, messaging and dependency injection.
  4. ScenariosA concurrency bug, a transaction that did not roll back, a migration plan.

Platform fundamentals

Opening questions for freshers and experienced candidates alike
QuestionWhat it testsWhat a strong answer shows
What is the difference between J2EE, Java EE and Jakarta EE?HistoryThe same enterprise platform under three names. Oracle handed Java EE to the Eclipse Foundation, where it continues as Jakarta EE. J2EE is the oldest name and survives mostly in job titles.
Why did javax packages become jakarta packages?NamespaceThe javax namespace could not be evolved under the new stewardship, so newer Jakarta EE releases moved the APIs to jakarta.*. Code and libraries must be updated together when upgrading.
What is a container?Core ideaThe runtime that hosts your components and provides lifecycle, threading, injection, security, transactions and naming, so application code does not build those services itself.
Servlet container versus full application server?RuntimesA servlet container such as Tomcat or Jetty runs the web tier. A full application server adds EJB, JMS, JTA and more. Many teams combine a light container with libraries instead.
What are the tiers of a typical enterprise application?ArchitectureClient, web tier (servlets, JSP, JSF, REST resources), business tier (EJBs or CDI beans) and the enterprise information tier (databases, queues, legacy systems).
JAR, WAR or EAR?PackagingA JAR holds classes and libraries. A WAR is a web module with WEB-INF/web.xml and classes. An EAR bundles several modules, such as WARs and EJB JARs, for one deployment.
What is a deployment descriptor?ConfigurationAn XML file such as web.xml that declares servlets, filters, security and resources. Annotations replaced much of it, but the descriptor can still override them.
What is JNDI used for?NamingLooking up container-managed resources such as data sources and JMS queues by name. Injection with @Resource often does the lookup for you.
Why use a container-managed connection pool?ResourcesOpening database connections is slow. The server keeps a pool, hands connections out per request and takes them back, and limits the total so the database is not overwhelmed.
What is the role of an application client or remote client?DistributionA separate Java program calling business components over the network, common in older systems that used remote EJB interfaces.
Opening questions for freshers and experienced candidates alike. Scroll sideways on a small screen.

Servlets, filters and JSP

  • Describe the servlet lifecycle. The container loads the class, creates one instance, calls init once, calls service (which dispatches to doGet, doPost and others) for each request on container threads, and calls destroy before unloading.
  • Why are servlet instance fields dangerous? One instance serves many concurrent requests, so a field shared between them causes data to leak from one user to another. Keep request data in local variables or request attributes.
  • forward versus sendRedirect? A forward happens inside the server; the browser URL stays the same and request attributes survive. A redirect sends a 3xx to the browser, which makes a new request, so attributes are lost and the URL changes.
  • What are filters used for? Code that wraps requests before and after the servlet: authentication checks, logging, compression, character encoding. They form a chain, and each calls chain.doFilter to pass the request on.
  • What are listeners? Callbacks for lifecycle events, such as the application starting or a session being created or destroyed. ServletContextListener is a common place to initialize shared resources.
  • How is a session tracked? Usually with a JSESSIONID cookie; URL rewriting is a fallback when cookies are disabled. Session data lives on the server, so clustering needs sticky sessions or replication.
  • What happens to a JSP at runtime? The container translates it into a servlet, compiles it and then runs it like any other servlet. Expression Language and JSTL tags replaced Java code in pages.
  • What is the Model 2 pattern? A controller servlet handles the request and prepares the model, then forwards to a JSP that only renders. It is the ancestor of most Java MVC frameworks.
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import java.io.IOException;

@WebServlet("/greet")
public class GreetServlet extends HttpServlet {
    // No instance fields with request data: one instance serves all threads.
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        resp.setContentType("text/plain;charset=UTF-8");
        resp.getWriter().println("Hello, " + (name == null ? "guest" : name));
    }
}
A thread-safe servlet that keeps request data local. Not executed here: it needs a servlet container and the Jakarta Servlet API on the classpath (older servers use javax.servlet).

EJB, transactions and messaging

Business tier questions for experienced candidates
QuestionWhat it testsWhat a strong answer shows
What kinds of session beans are there?EJB typesStateless (pooled, no client state), stateful (one per client conversation) and singleton (one per application, with container or bean-managed concurrency). Message-driven beans consume messages instead of method calls.
Container-managed versus bean-managed transactions?TransactionsWith container management, the server starts and commits transactions around business methods based on attributes. With bean management, code calls UserTransaction begin and commit itself.
Explain REQUIRED, REQUIRES_NEW and MANDATORY.AttributesREQUIRED joins the caller's transaction or starts one. REQUIRES_NEW always suspends the caller's and starts its own, useful for audit logs that must survive a rollback. MANDATORY fails if no transaction exists.
When does a container roll back automatically?Rollback rulesOn system exceptions such as unchecked exceptions. Checked application exceptions do not roll back unless marked to, or unless code calls setRollbackOnly. This trips up many candidates.
What is JTA, and when do you need XA?Distributed transactionsJTA is the transaction API. XA lets one transaction span several resources, such as a database and a message queue, with two-phase commit, at a cost in speed and operational complexity.
JMS queue versus topic?MessagingA queue delivers each message to one consumer, good for work distribution. A topic delivers to every subscriber; durable subscriptions keep messages for subscribers that are offline.
What makes a message-driven bean safe to retry?DeliveryMessages can be redelivered after a failure, so processing must be idempotent or deduplicated, with a dead-letter destination for messages that keep failing.
Business tier questions for experienced candidates. Scroll sideways on a small screen.

CDI, REST and newer APIs

  • What is CDI? Contexts and Dependency Injection: the standard way to inject beans with @Inject, give them scopes such as request, session and application, and add interceptors and events.
  • What are producer methods for? Creating injectable objects that need custom setup or come from outside the container, such as a configured client.
  • How do interceptors differ from filters? Filters wrap HTTP requests in the web tier. Interceptors wrap method calls on beans, for concerns such as logging, timing or security on business methods.
  • How do you build a REST endpoint in Jakarta EE? A JAX-RS resource class with @Path, methods annotated @GET or @POST, and @Produces and @Consumes for media types. Exception mappers turn errors into responses.
  • Where does Bean Validation fit? Constraints such as @NotNull and @Size on fields or parameters, checked automatically by JAX-RS, JPA and CDI integrations.
  • What is MicroProfile? A related set of specifications for microservices on enterprise Java, such as configuration, health checks and metrics, implemented by several runtimes.

Senior and scenario questions

  • Users occasionally see another user's name on a page. Where do you look first? Servlet or singleton instance fields, static caches keyed wrongly, and thread-local values not cleared at the end of a request on a pooled thread.
  • A failed order still wrote its audit record and half its data. Why? Probably a checked exception that did not trigger rollback, a method calling another on the same bean (so the container never applied the inner transaction attribute), or a resource outside the transaction.
  • After several redeploys the server runs out of memory. What is happening? A classloader leak: threads, drivers or static references from the old application keep its classes alive. Look at a heap dump for old classloaders and fix the references at shutdown.
  • How would you migrate an application from javax to jakarta packages? Inventory dependencies, upgrade the server and libraries together, use migration tooling to rewrite imports and descriptors, run the full test suite, and plan for libraries that have no jakarta version yet.
  • Would you move this system to Spring Boot or a newer Jakarta runtime? Weigh what the server provides today (transactions, messaging, security), the team's skills, operational tooling and the cost of a rewrite. A phased approach that extracts services one at a time is often safer.
  • How do you scale a stateful web application across servers? Sticky sessions at the load balancer, session replication or an external session store, and moving as much state as possible out of the session.

Fictional example

Answering "Why didn't this transaction roll back?"
Interviewer
placeOrder saves the order, then calls reserveStock on the same EJB, which throws InsufficientStockException, a checked exception. The order row is still in the database. Explain.
Candidate
Two things are going on. First, a checked application exception does not mark the transaction for rollback by default, so the container commits placeOrder's work when the exception passes through. I would either annotate the exception as an application exception with rollback set to true, or call setRollbackOnly before throwing. Second, if reserveStock has its own transaction attribute, such as REQUIRES_NEW, calling it on this rather than through the container proxy means the attribute is ignored, because the container only sees calls that come through the bean's proxy. I would inject the bean or move reserveStock to another bean, then add a test that asserts no order row exists after the failure.

J2EE practice plan

  • Day 1: write the platform timeline in your own words, then list the main specifications and what each provides.
  • Day 2: build a small web module with a servlet, a filter and a listener, and log the order in which they run for one request.
  • Day 3: write out every transaction attribute with a real use case, and the rollback rule for checked and unchecked exceptions.
  • Day 4: sketch an order flow using a JMS queue and a message-driven bean, including redelivery and a dead-letter queue.
  • Day 5: prepare a two-minute plan for migrating a javax application to jakarta packages and say what could go wrong.

For the language features used inside these components, read Java 8 interview questions. For build and test scripting on the same JVM, see Groovy interview questions, and for the full role, Java developer interview questions.

Enterprise Java answers depend on precise words: which exception, which attribute, which proxy. Natively, an AI interview assistant for Mac and Windows, can transcribe a practice round on your computer so you can check afterward whether you said "container-managed" where you meant it. As you rehearse, it can read a stack trace or a deployment descriptor from a screenshot and suggest what to explain, and with Pro or the free trial it can use the résumé and job description you add. Each session's transcript and notes are kept for review.

Questions people ask

What J2EE topics are asked in interviews?

The J2EE, Java EE and Jakarta EE naming, containers and application servers, WAR and EAR packaging, JNDI, the servlet lifecycle, filters, sessions, JSP, EJB types, transaction attributes, JMS and CDI.

Is J2EE the same as Jakarta EE?

They are the same platform at different points in its history. J2EE was renamed Java EE, and after moving to the Eclipse Foundation it became Jakarta EE, whose newer releases use jakarta.* package names.

Are servlets thread-safe?

Not automatically. The container usually creates one servlet instance and calls it from many threads at once, so request data must stay in local variables or request attributes, not instance fields.

Why does a checked exception not roll back an EJB transaction?

By default the container treats checked exceptions as application exceptions and commits. Mark the exception for rollback or call setRollbackOnly when the work should be undone.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP