Launch your tech mastery with us—your coding journey starts now!
Course Content
Advanced Java
ServletRequest Interface

An object of ServletRequest is used to provide the client request information to a servlet such as content type, content length, parameter names and values, header informations, attributes etc.

Methods of ServletRequest interface

There are many methods defined in the ServletRequest interface. Some of them are as follows:

Method

Description

public String getParameter(String name)

is used to obtain the value of a parameter by name.

public String[] getParameterValues(String name)

returns an array of String containing all values of given parameter name. It is mainly used to obtain values of a Multi select list box.

java.util.Enumeration getParameterNames()

returns an enumeration of all of the request parameter names.

public int getContentLength()

Returns the size of the request entity data, or -1 if not known.

public String getCharacterEncoding()

Returns the character set encoding for the input of this request.

public String getContentType()

Returns the Internet Media Type of the request entity data, or null if not known.

public ServletInputStream getInputStream() throws IOException

Returns an input stream for reading binary data in the request body.

public abstract String getServerName()

Returns the host name of the server that received the request.

public int getServerPort()

Returns the port number on which this request was received.

Example of ServletRequest to display the name of the user

In this example, we are displaying the name of the user in the servlet. For this purpose, we have used the getParameter method that returns the value for the given request parameter name.

index.html

<form action=”DemoServ” method=”get”>

Enter your name<input type=”text” name=”name”><br>

<input type=”submit” value=”login”>

</form>
DemoServ.java

import javax.servlet.http.*;  

import javax.servlet.*;  

import java.io.*;  

public class DemoServ extends HttpServlet{  

public void doGet(HttpServletRequest req,HttpServletResponse res)  

throws ServletException,IOException  

{  

res.setContentType(“text/html”);  

PrintWriter pw=res.getWriter();  

String name=req.getParameter(“name”);//will return value  

pw.println(“Welcome “+name);  

pw.close();  

}}  

READING FORM DATA FROM SERVLETS

Reading Single Values: getParameter 

To read a request (form) parameter, you simply call the getParameter method of HttpServletRequest, supplying the casesensitive parameter name as an argument. You supply the parameter name exactly as it appeared in the HTML source code, and you get the result exactly as the end user entered it; any

necessary URL-decoding is done automatically. An empty String is returned if the parameter exists but has no value (i.e., the user left the corresponding textfield empty when submitting the form), and

null is returned if there was no such parameter. Parameter names are case sensitive so, for example, request. Get Parameter (“Param1”) and request. get Parameter (“param1”) are not interchangeable.

 

Reading Multiple Values: getParameterValues

If the same parameter name might appear in the form data more than once, you should call getParameterValues (which returns an array of strings) instead of getParameter (which returns

a single string corresponding to the first occurrence of the parameter). The return value of  getParameterValues is null for nonexistent parameter names and is a one element array when the

parameter has only a single value. Now, if you are the author of the HTML form, it is usually best to ensure that each textfield, checkbox, or other user interface element has a unique name. That way, you can just stick with the simpler getParameter method and avoid getParameterValues altogether. Besides, multiselectable list boxes repeat the parameter name for each selected element in the list. So, you cannot always avoid multiple values.

Looking Up Parameter Names: getParameterNames

Use getParameterNames to get this list in the form of an Enumeration, each entry of which can be cast to a String and used in a getParameter or getParameterValues call. If there are no parameters in the current request, getParameterNames returns an empty Enumeration (not null). Note that Enumeration is an interface that merely guarantees that the actual class will have hasMoreElements and nextElement methods: there is no guarantee that any particular underlying data structure will be used. And, since

some common data structures (hash tables, in particular) scramble the order of the elements, you should not count on getParameterNames returning the parameters in the order in which they appeared in the HTML form.