List and explain core classes and interfaces in the javax.servlet package.
7.A] List and explain core classes and interfaces in the javax.servlet package.
Answer:
INTERFACES
CLASSES
Servlet
ServletInputStream
ServletContext
ServletOutputStream
ServletConfig
ServletException
ServletRequest
UnavailableException
ServletResponse
GenericServlet
Interface Summary
Servlet
Defines methods that all servlets must implement.
ServletRequest
Defines an object to provide client request information to a servlet.
ServletResponse
Defines an object to assist a servlet in sending a response to the client.
ServletConfig
A servlet configuration object used by a servlet container to pass information to a servlet during initialization.
ServletContext
Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.
Class Summary
GenericServlet
Defines a generic, protocol-independent servlet.
ServletInputStream
Provides an input stream for reading binary data from a client request, including an efficient readLine method for reading data one line at a time.
ServletOutputStream
Provides an output stream for sending binary data to the client.
ServletException
Defines a general exception a servlet can throw when it encounters difficulty.
UnavailableException
Defines an exception that a servlet or filter throws to indicate that it is permanently or temporarily unavailable.
Core Interfaces in javax.servlet
1. Servlet Interface
Purpose: The fundamental interface that all servlets must implement. It provides the methods required for servlet lifecycle management and request handling.
Key Methods:
void init(ServletConfig config): Initializes the servlet with configuration parameters.
void service(ServletRequest request, ServletResponse response): Handles requests and generates responses.
void destroy(): Cleans up resources before the servlet is destroyed.
ServletConfig getServletConfig(): Returns the servlet configuration.
String getServletInfo(): Provides information about the servlet.
2. ServletRequest Interface
Purpose: Provides methods to obtain request data and metadata from the client request.
Key Methods:
Object getAttribute(String name): Retrieves an attribute from the request.
Enumeration<String> getAttributeNames(): Returns an enumeration of attribute names.
String getParameter(String name): Retrieves a parameter from the request.
Enumeration<String> getParameterNames(): Returns an enumeration of parameter names.
BufferedReader getReader(): Retrieves the request body as a BufferedReader.
3. ServletResponse Interface
Purpose: Defines methods for sending responses to the client.
Key Methods:
void setContentType(String type): Sets the MIME type of the response.
PrintWriter getWriter(): Retrieves a PrintWriter to write response data.
void setCharacterEncoding(String charset): Sets the character encoding for the response.
void setContentLength(int len): Sets the length of the response content.
4. ServletConfig Interface
Purpose: Provides configuration information to a servlet, such as initialization parameters.
Key Methods:
String getInitParameter(String name): Retrieves an initialization parameter.
Enumeration<String> getInitParameterNames(): Returns an enumeration of initialization parameter names.
ServletContext getServletContext(): Returns the servlet context.
5. ServletContext Interface
Purpose: Provides a servlet with information about its environment and allows interaction with other servlets and web resources.
Key Methods:
String getContextPath(): Returns the context path of the web application.
RequestDispatcher getRequestDispatcher(String path): Returns a RequestDispatcher for a given path.
InputStream getResourceAsStream(String path): Retrieves a resource as an InputStream.
void log(String msg): Writes a log message.
6. RequestDispatcher Interface
Purpose: Allows forwarding a request to another resource or including the content of another resource in the response.
Key Methods:
void forward(ServletRequest request, ServletResponse response): Forwards the request to another resource.
void include(ServletRequest request, ServletResponse response): Includes the content of another resource in the response.
Core Classes in javax.servlet
1. GenericServlet Class
Purpose: Provides default implementations for the Servlet interface methods. It simplifies the development of servlets by handling common functionality.
Key Methods:
void service(ServletRequest req, ServletResponse res): Must be overridden by subclasses to handle requests.
ServletConfig getServletConfig(): Returns the servlet configuration.
2. HttpServlet Class
Purpose: Extends GenericServlet and provides additional methods specific to HTTP requests, such as handling GET, POST, PUT, and DELETE requests.
Key Methods:
void doGet(HttpServletRequest req, HttpServletResponse resp): Handles GET requests.
void doPost(HttpServletRequest req, HttpServletResponse resp): Handles POST requests.
void doPut(HttpServletRequest req, HttpServletResponse resp): Handles PUT requests.