ServletRequest Interface in Servlet

As of now, we have discussed the ServletContext Interface. Now, we will discuss the ServletRequest Interface in servlet. As we know the client requests the server to get the information. So, in a servlet, we use the ServletRequest Interface.

What is ServletRequest Interface?

The client requests the server, the web server creates the ServletRequest object and passes it as an argument to the Servlet service() method. Due to which it is able to access request information such as the header, the body of the request. So in a servlet, we use ServletRequest Interface to pass information such as parameter, name, values, attributes, etc.

Methods in ServletRequest Interface

  • public String getParameter(String name): This method is used to get the value of the parameter by name.
    • Parameter
      • name: it is a string that specifies the name of the parameter.
  • public String[] getParameterValues(String name): This method returns the array of string that contains parameter name.
  • java.util.Enumeration getParametersName():This method returns the Enumeration of parameters name.
  • public int getContentLength(): This method returns the size of the request.
  • public String getCharacterEncoding(): This method returns the Encoding of the input request.
  • public String getContentType():This method returns the MIME type of the request.
  • public ServletInputStream getInputStream():This method is used to read binary data from request.
  • public abstract String getServerName(): This method returns the name of the server on which request is received.
  • public int getServerPort(): This method returns the port number on which request is done.

Let’s see the Example for ServletRequest Interface.

In this example, we will see the use of the getParametere() method of ServeletRequest Interface.