Sobes.tech
Middle

What is the purpose of the ServletRequest interface?

sobes.tech AI

Answer from AI

The ServletRequest interface is used in the Java Servlet API to represent a request coming from a client to a server. It provides methods to obtain information about the request, such as parameters, attributes, input data, client information, etc.

Main features of ServletRequest:

  • Getting request parameters (getParameter, getParameterMap)
  • Getting request attributes (getAttribute, setAttribute)
  • Getting client information (IP address, port)
  • Reading the request body through input streams (getInputStream, getReader)

This interface is the base for HTTP requests and is extended by the HttpServletRequest interface, which adds methods specific to the HTTP protocol.

Example usage:

public void doGet(ServletRequest request, ServletResponse response) {
    String username = request.getParameter("username");
    // process parameter
}
What is the purpose of the ServletRequest interface… - sobes.tech