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 the ServletRequest Interface?

The client requests the server, the webserver 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.

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
  <form action="abc" method="get">
    <div>
      Enter Student Name: <input type="text" name="name"> <input
        type="submit" value="Submit">
    </div>
  </form>
</body>
</html>

ServletRequestDemo.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletRequestDemo
 */
@WebServlet("/ServletRequestDemo")
public class ServletRequestDemo extends HttpServlet implements Servlet {
  private static final long serialVersionUID = 1L;

  public ServletRequestDemo() {
    super();
    // TODO Auto-generated constructor stub
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    // response.getWriter().append("Served at:
    // ").append(request.getContextPath());
    PrintWriter out = response.getWriter();
    String name = request.getParameter("name");
    out.println("ServletRequest Interface.....!!");
    out.println("Welcome :" + name);

  }
}

Output:

In the next article, we will see another Interface of servlet i.e ServletContext Interface in detail.