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:
- Servlet API — a class implementing the
HttpServletinterface, overriding methods likedoGet(),doPost(), etc.
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().write("Hello from Servlet");
}
}
- Spring MVC controllers — annotated classes with methods that handle requests.
@RestController
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello from Spring MVC";
}
}
- 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.