Open In App

Servlet – Request Interface

Last Updated : 14 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

When a Servlet accepts a call from a client, then it receives two objects, one is a ServletRequest and the other is the ServletResponse. ServletRequest encapsulates the Communications from the client to the server, while ServletResponse encapsulates the Communication from the Servlet back to the client. The Object of the ServletRequest is used to provide the client request information data to the Servlet as a content type, a content length, parameter names, and the values, header information and the attributes, etc.

ServletRequest allows the Servlet to access information such as:

  • Names of the parameters are passed by the client
  • The protocol [scheme] such as the HTIP POST and PUT methods being used by the client
  • The names of the remote host that are made the request
  • The server that received it
  • An input stream is for reading the binary data from the request body

The Subclasses of the ServletRequest allows the Servlet to retrieve more protocol-based-specific data. For example, HttpServletRequest contains the methods for accessing HTTP-specific based header Information.

ServletResponse allows Servlet

  • To settle up the content length and mime type of the reply
  • Provides an output stream and a Writer

Through ServletResponse, the Servlet can send the reply data. The Subclasses of ServletResponse provide the Servlet with more protocol-based-specific capabilities. For example, up ServletResponse can contain methods that allow the Servlet to Control the HTTP-specific header information.

Methods of ServletRequest Interface

Method

Description

public String getParameter(String name ) This method is used to obtain the value of a parameter by its name
public String[] getParameterValues(String name)   This methods returns an array of String containing all values of given parameter name. It is mainly used for to obtain values of a Multi selected listed box.
java.util.Enumeration getParameterNames() this java.util.Enumeration getParameterNames() returns an enumeration for all of the request parameter names
public int getContentLength() Returns the size of the requested entity for the data, or a -1 if not known.
public String getCharacterEncoding() Returns the characters set encoding for the input of this current request.
public String getContentType() Returns the Internet Media Type(IMT) of the requested entity of data, or null if not known to it

Example of ServletRequest to display the name of the user

In this example, we are displaying the name of the user in this servlet, And for this, we have used the getParameter method that returns the value for the given request parameter name. We have the use of Servlet – Request Interface in it And When we use it within this example and running will successfully establish the method.

newindex.html

HTML




<form action="welcome User" method="get">  
  Enter your name<input type="text" name="Name"><br>   
<input type="Submit" value="Login">  
</form>


MockServer.java

Java




import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MockServ extends HttpServlet {
    public void doGet(HttpServletRequest req,
                      HttpServletResponse resp)
        // Uses throws exception
        throws ServletException, IOException
    {
        resp.setContentType("text/html");
        
        // sets Content Type
        PrintWriter ab = resp.getWriter();
  
        String Name = req.getParameter("Name");
        
        // thiswill return value
        ab.println(" Welcome User " + Name);
        
        // this will print the Name Welcome User
        ab.close();
    }
}




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads