ServletRequest Interface – Servlets

The ServletRequest Interface defines an object which is used to encapsulate information about the user’s request, including parameter name/value pairs, attributes, and an input stream.

The ServletRequest interface provides important methods which enables you to access information about the user. For example, the getParameterNames() method returns an Enumeration containing the parameter names for the current request. To get the value of each parameter, we can use the the getParameter() method of the ServletRequest interface.

Lets take a look on all the method of the ServletRequest Interface in the below example.

package com.javainterviewpoint;

import java.util.Enumeration;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class RequestInterfaceExample implements Servlet
    public void init(ServletConfig config) throws ServletException 
    {    }
    public void destroy() 
    {    }
    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, IOException 
        //Obtaining the Server Port
        System.out.println("Server Port: " + request.getServerPort());
        //Getting the Server Name
        System.out.println("Server Name: " + request.getServerName());
        //Getting the Protocol Used
        System.out.println("Protocol: " + request.getProtocol());
        //Character Encoding which is set
        System.out.println("Character Encoding: " + request.getCharacterEncoding());
        //Getting the Content Type
        System.out.println("Content Type: " + request.getContentType());
        //Getting the Remote Address
        System.out.println("Remote Address: " + request.getRemoteAddr());
        //Getting Remote Host
        System.out.println("Remote Host: " + request.getRemoteHost());
        //Obtaining the Scheme
        System.out.println("Scheme: " + request.getScheme());
        //Get all the parameters passed from index.jsp
        Enumeration parameters = request.getParameterNames();
        while (parameters.hasMoreElements()) {
        String parameterName = (String) parameters.nextElement();
        System.out.println("Parameter Name: " + parameterName);
        System.out.println("Parameter Value: " +
        //Setting the request attribute
        request.setAttribute("requestAttribute1", "requestAttribute1");
        request.setAttribute("requestAttribute2", "requestAttribute2");
        //Retrieving all the request attributes
        Enumeration attributes = request.getAttributeNames();
        while (attributes.hasMoreElements()) {
        String attribute = (String) attributes.nextElement();
        System.out.println("Attribute name: " + attribute);
        System.out.println("Attribute value: " +
    public String getServletInfo() 
        return null;
    public ServletConfig getServletConfig() 
        return null;


 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" 
id="WebApp_ID" version="2.5">


 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <form action="./RequestInterfaceExample" method = "POST">
            FirstName : <input type="text" name="firstName"><br>
            LastName : <input type="text" name="lastName"><br>
            <input type="submit" value="Submit Form">
            <input type="reset" value="Reset Form">


Hit on the Url : http://localhost:8080/ServletTutorial/index.jsp






Fill in the Form and hit on submit, in the console you will get the below output.

Server Port: 8080
Server Name: localhost
Protocol: HTTP/1.1
Character Encoding: null
Content Type: application/x-www-form-urlencoded
Remote Address: 0:0:0:0:0:0:0:1
Remote Host: 0:0:0:0:0:0:0:1
Scheme: http
Parameter Name: lastName
Parameter Value: InterviewPoint
Parameter Name: firstName
Parameter Value: Java
Attribute name: requestAttribute2
Attribute value: requestAttribute2
Attribute name: requestAttribute1
Attribute value: requestAttribute1

Leave a Reply

Your email address will not be published. Required fields are marked *