Sobes.tech
Junior — Middle

What methods do you use to implement request handlers in your application?

sobes.tech AI

Answer from AI

Request handlers in Java applications can be implemented in several ways:

  1. Servlet API — a class implementing the HttpServlet interface, overriding methods like doGet(), doPost(), etc.
public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().write("Hello from Servlet");
    }
}
  1. Spring MVC controllers — annotated classes with methods that handle requests.
@RestController
public class MyController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello from Spring MVC";
    }
}
  1. Java EE/Jakarta EE JAX-RS — RESTful services with annotations like @Path, @GET, etc.
@Path("/api")
public class MyResource {
    @GET
    @Path("/hello")
    public String hello() {
        return "Hello from JAX-RS";
    }
}

The choice of method depends on the framework and architecture of the application.