Junior — Middle
Can the Servlet interface in Java be considered, rather than the class?
sobes.tech AI
Answer from AI
Yes, in Java Servlet — it is an interface, not a class. It is defined in the javax.servlet package and sets the basic contract for servlets that handle HTTP requests.
Specific servlets usually implement this interface or, more commonly, inherit from the HttpServlet class, which already implements the Servlet interface and provides convenient methods for working with HTTP.
Example:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Hello from MyServlet");
}
}