Servlet ServletRequest

In this tutorial you will learn about the Servlet ServletRequest and its application with practical example.

Servlet ServletRequest Interface

ServletRequest is an interface which provide the client request to a servlet. The servlet container creates a ServletRequest and passes it as an argument to the service method of servlet. It provides data to servlet which includes parameter name and values,attributes and input stream.

ServletRequest Interface Methods

There are some of ServletRequest methods are discussed below.

public String getParameter(String name) : This method is used to get the value of Request Parameter as a string, or null if the parameter not exist.

java.util.Enumeration getParameterNames(): This method is used to Returns an Enumeration of String objects containing the names of the parameters contained in this request.

public String[] getParameterValues(String name) : This is the method which returns array of String object or null if there is no parameter

public int getServerPort():This method is used to returns the port number to which the request was sent.

boolean isSecure() : This method is used tor eturns a boolean which indicate whether this request was made using a secure channel, such as HTTPS.

void removeAttribute(java.lang.String name) : This method is used to remove an attribute from this request.

void setAttribute(java.lang.String name, java.lang.Object o) : This method is used to store an attribute in this request.

public int getContentLength(): This method returns the length of request enitity data or
-1 if the length is not known.

public String getCharacterEncoding(): This method is used to returns the name of the character encoding which is used in the body of this request.

public String getContentType(): This is the method which returns the MIME type of the body of the request, or null if the type is not known.

public ServletInputStream getInputStream() throws IOException:This method returns the body of the request as binary data using a ServletInputStream.

public abstract String getServerName(): This method returns the host name of the server to which the request was sent .

Example :-

Let’s create an example to understand servlet request interface. Please read “Servlet Example with Netbeans” before creation this.

Recommended:-  Servlet HttpSession Login Logout

Step 1:-

We have to modify our index.html file like below for sending request to servlet. Check below code for this.

Here I have send 2 request parameter to servlet. One is username and second is siteName. In form action you have to put servlet name.

Step 2:-

The servlet code you can check in this step. By using request.getParamter() method we will get parameter value which is coming from index.html file. Check below code.

Output :-

The output of this code is shown in below two images.

Recommended:-  Servlet on Netbeans

Screen 1:-


Screen 2:-


In this tutorial we have learn about the Servlet ServletRequest and its application with practical example. I hope you will like this tutorial.