Servlet versus JAX-RS
Replace verbose HttpServlet boilerplate with declarative JAX-RS resource classes.
Code Comparison
✕ Java EE
@WebServlet("/users")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
String id = req.getParameter("id");
res.setContentType("application/json");
res.getWriter().write("{\"id\":\"" + id + "\"}");
}
}
✓ Jakarta EE 8+
@Path("/users")
public class UserResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getUser(
@QueryParam("id") String id) {
return Response.ok(new User(id)).build();
}
}
Why the modern way wins
Declarative routing
Annotations define HTTP method, path, and content type instead of imperative if/else dispatch.
Automatic marshalling
Return POJOs directly; the runtime serialises them to JSON or XML based on @Produces.
Easier testing
Resource classes are plain Java objects, testable without a servlet container.
Old Approach
HttpServlet
Modern Approach
JAX-RS Resource
Since JDK
11
Difficulty
intermediate
JDK Support
Servlet versus JAX-RS
Available
Widely available since Jakarta EE 8 / Java 11
How it works
JAX-RS (Jakarta RESTful Web Services) lets you expose REST endpoints using simple annotations like @GET, @Path, and @Produces. No more manual parsing of request parameters or setting content types on the response — the runtime handles marshalling and routing automatically.
Related Documentation